-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.rb
39 lines (34 loc) · 1007 Bytes
/
10.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true
input = IO.readlines("#{File.dirname(__FILE__)}/input.txt", chomp: true)
position = input.each_with_index.filter_map do
[_2, _1.index('S')] if _1.scan(/S/).any?
end.first
previous_position = nil
steps = 0
$valid_directions = {
N: %w[7 | F],
E: %w[7 - J],
S: %w[L | J],
W: %w[F - L]
}
$wheel = {
N: [-1, 0],
E: [0, 1],
S: [1, 0],
W: [0, -1]
}
def opposite_direction(d)
keys = $valid_directions.keys
keys[(keys.index(d) + 2) % keys.size]
end
# queue = [[position, previous_position, steps]]
until input[position[0]][position[1]] == 'S' && previous_position
wheel = $wheel.reject { _1 == previous_position }
.transform_values{ [position[0] + _1,position[1] + _2] }
.filter { $valid_directions[_1].include? input[_2[0]][_2[1]] }
break if wheel.keys.empty?
next_direction = wheel.keys.last
previous_position, position = [opposite_direction(next_direction), wheel[next_direction]]
steps += 1
end
puts steps / 2 + 1