|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +input = File.read('10.input').lines.map(&:strip).map(&:chars) |
| 4 | +height = input.size |
| 5 | +width = input[0].size |
| 6 | + |
| 7 | +distances = Array.new height |
| 8 | +distances = distances.map { |_| Array.new(width, -1) } |
| 9 | + |
| 10 | +pipes = Array.new height |
| 11 | +pipes = pipes.map { |_| Array.new width } |
| 12 | + |
| 13 | +start_y = -1 |
| 14 | +start_x = -1 |
| 15 | +height.times do |y| |
| 16 | + width.times do |x| |
| 17 | + pipes[y][x] = case input[y][x] |
| 18 | + when 'S' |
| 19 | + distances[y][x] = 0 |
| 20 | + start_y = y |
| 21 | + start_x = x |
| 22 | + [] |
| 23 | + when '.' |
| 24 | + [] |
| 25 | + when '|' |
| 26 | + [:north, :south] |
| 27 | + when '-' |
| 28 | + [:east, :west] |
| 29 | + when 'L' |
| 30 | + [:north, :east] |
| 31 | + when 'J' |
| 32 | + [:north, :west] |
| 33 | + when '7' |
| 34 | + [:south, :west] |
| 35 | + when 'F' |
| 36 | + [:south, :east] |
| 37 | + end |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +if start_y >= 1 and pipes[start_y - 1][start_x].include? :south |
| 42 | + pipes[start_y][start_x] << :north |
| 43 | +end |
| 44 | +if start_y < height - 1 and pipes[start_y + 1][start_x].include? :north |
| 45 | + pipes[start_y][start_x] << :south |
| 46 | +end |
| 47 | +if start_x >= 1 and pipes[start_y][start_x - 1].include? :east |
| 48 | + pipes[start_y][start_x] << :west |
| 49 | +end |
| 50 | +if start_x < width - 1 and pipes[start_y][start_x + 1].include? :west |
| 51 | + pipes[start_y][start_x] << :east |
| 52 | +end |
| 53 | + |
| 54 | +cur_dist = -1 |
| 55 | +cur_y = start_y |
| 56 | +cur_x = start_x |
| 57 | +from = nil |
| 58 | + |
| 59 | +loop do |
| 60 | + cur_dist = distances[cur_y][cur_x] |
| 61 | + if from != :north and cur_y > 0 and pipes[cur_y][cur_x].include? :north and pipes[cur_y - 1][cur_x].include? :south |
| 62 | + distances[cur_y - 1][cur_x] = cur_dist + 1 unless distances[cur_y - 1][cur_x] == 0 |
| 63 | + from = :south |
| 64 | + cur_y -= 1 |
| 65 | + elsif from != :east and cur_x < width - 1 and pipes[cur_y][cur_x].include? :east and pipes[cur_y][cur_x + 1].include? :west |
| 66 | + distances[cur_y][cur_x + 1] = cur_dist + 1 unless distances[cur_y - 1][cur_x] == 0 |
| 67 | + from = :west |
| 68 | + cur_x += 1 |
| 69 | + elsif from != :south and cur_y < height - 1 and pipes[cur_y][cur_x].include? :south and pipes[cur_y + 1][cur_x].include? :north |
| 70 | + distances[cur_y + 1][cur_x] = cur_dist + 1 unless distances[cur_y - 1][cur_x] == 0 |
| 71 | + from = :north |
| 72 | + cur_y += 1 |
| 73 | + elsif from != :west and cur_x > 0 and pipes[cur_y][cur_x].include? :west and pipes[cur_y][cur_x - 1].include? :east |
| 74 | + distances[cur_y][cur_x - 1] = cur_dist + 1 unless distances[cur_y - 1][cur_x] == 0 |
| 75 | + from = :east |
| 76 | + cur_x -= 1 |
| 77 | + end |
| 78 | + |
| 79 | + break if cur_y == start_y and cur_x == start_x |
| 80 | +end |
| 81 | + |
| 82 | +print (cur_dist + 1) / 2, "\n" |
0 commit comments