-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22-1.rb
executable file
·193 lines (179 loc) · 3.85 KB
/
22-1.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env ruby
require 'numo/narray'
require 'ostruct'
class Map
def initialize(input)
@instructions = input.pop.scan(/[[:digit:]]+|L|R/).map { |i| /[[:digit:]]+/.match(i) ? i.to_i : i }
input.pop
input = input.map(&:chars)
@height = input.size + 2
@width = input.map(&:size).max + 2
@map = Numo::UInt8.zeros(@height, @width)
input.each_index do |row|
input[row].each_index do |col|
case input[row][col]
when ' '
@map[row + 1, col + 1] = 0
when '.'
@map[row + 1, col + 1] = 1
when '#'
@map[row + 1, col + 1] = 2
end
end
end
@pos = OpenStruct.new
@pos.row = 1
@width.times do |x|
if @map[@pos.row, x] == 1
@pos.col = x
break
end
end
@dir = '>'
end
def step!
inst = @instructions.shift
case inst
when 'R'
@dir = case @dir
when '>'
'v'
when 'v'
'<'
when '<'
'^'
when '^'
'>'
end
when 'L'
@dir = case @dir
when '>'
'^'
when '^'
'<'
when '<'
'v'
when 'v'
'>'
end
else
case @dir
when '>'
inst.times do |step|
case @map[@pos.row, @pos.col + 1]
when 0
@width.times do |x|
case @map[@pos.row, x + 1]
when 1
@pos.col = x + 1
break
when 2
break
end
end
when 1
@pos.col += 1
when 2
break
end
end
when 'v'
inst.times do
case @map[@pos.row + 1, @pos.col]
when 0
@height.times do |y|
case @map[y + 1, @pos.col]
when 1
@pos.row = y + 1
break
when 2
break
end
end
when 1
@pos.row += 1
when 2
break
end
end
when '<'
inst.times do
case @map[@pos.row, @pos.col - 1]
when 0
@width.times do |x|
case @map[@pos.row, @width - 1 - x]
when 1
@pos.col = @width - 1 - x
break
when 2
break
end
end
when 1
@pos.col -= 1
when 2
break
end
end
when '^'
inst.times do
case @map[@pos.row - 1, @pos.col]
when 0
@height.times do |y|
case @map[@height - 1 - y, @pos.col]
when 1
@pos.row = @height - 1 - y
break
when 2
break
end
end
when 1
@pos.row -= 1
when 2
break
end
end
end
end
end
def solve!
until @instructions.empty? do
step!
end
end
def password
@pos.row * 1_000 +
@pos.col * 4 +
['>', 'v', '<', '^'].index(@dir)
end
def to_s
s = "<#{self.class}:\n"
s += "Position: #{@pos}\n"
@height.times do |row|
@width.times do |col|
s += if row == @pos.row and col == @pos.col
@dir
else
case @map[row, col]
when 0
' '
when 1
'.'
when 2
'#'
end
end
end
s += "\n"
end
s += @instructions.to_s
s += '>'
s
end
alias inspect to_s
end
input = File.read('22.input').lines.map(&:rstrip)
map = Map.new(input)
map.solve!
print map.password, "\n"