-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.rb
288 lines (250 loc) · 6.82 KB
/
day13.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require_relative 'toolbox'
class Coordinate
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def next_in_direction(direction)
case direction
when :north
return Coordinate.new(self.x, self.y - 1)
when :east
return Coordinate.new(self.x + 1, self.y)
when :south
return Coordinate.new(self.x, self.y + 1)
when :west
return Coordinate.new(self.x - 1, self.y)
else
puts "next_in_direction called incorrectly: #{self.inspect}"
return Coordinate.new(-1,-1)
end
end
def to_s
"#{x},#{y}"
end
end
class Cart
attr_reader :coordinate
attr_writer :track_type
INTERSECTION_MOVEMENTS = [:left, :straight, :right]
def initialize(initial_position, direction)
@coordinate = initial_position
@direction = direction
@track_type = (@direction == :north || @direction == :south) ? :vertical : :horizontal
@intersection_move = 0
end
def tick
if @track_type == :curve_right
case @direction
when :north
@direction = :east
when :east
@direction = :north
when :south
@direction = :west
when :west
@direction = :south
else
puts "next_coordinate called incorrectly: #{self.inspect}"
end
elsif @track_type == :curve_left
case @direction
when :north
@direction = :west
when :east
@direction = :south
when :south
@direction = :east
when :west
@direction = :north
else
puts "next_coordinate called incorrectly: #{self.inspect}"
end
elsif @track_type == :intersection
_direction_at_intersection
end
@coordinate = @coordinate.next_in_direction(@direction)
end
def to_s
"#{self.coordinate.x},#{self.coordinate.y} #{@direction} #{@track_type}"
end
private
# :straight is no change in direction
def _direction_at_intersection
movement = _intersection_decision
case movement
when :left
case @direction
when :north
@direction = :west
when :east
@direction = :north
when :south
@direction = :east
when :west
@direction = :south
else
puts "_direction_at_intersection called incorrectly: #{self.inspect}"
end
when :right
case @direction
when :north
@direction = :east
when :east
@direction = :south
when :south
@direction = :west
when :west
@direction = :north
else
end
end
end
def _intersection_decision
movement = INTERSECTION_MOVEMENTS[@intersection_move]
if @intersection_move == 0
@intersection_move = 1
elsif @intersection_move == 1
@intersection_move = 2
elsif @intersection_move == 2
@intersection_move = 0
else
puts "we lose big time"
end
movement
end
end
class TrackSystem
attr_reader :grid
def initialize(system_map)
@carts = []
@collided = []
@grid = Array.new(system_map.first.size) { Array.new(system_map.size, nil) }
# populate the grid
system_map.each_with_index do |row, y|
prev_actual_track = nil
row.chars.each_with_index do |track_piece, x|
actual_track = nil
# handle carts
if _is_cart?(track_piece)
cart_direction = _parse_direction(track_piece)
@carts << Cart.new(Coordinate.new(x,y), cart_direction)
# we have a cart. inspect the previous piece to derive underlying track
if prev_actual_track.nil?
# we were on leftmost edge or had empty spot. must be vertical
actual_track = :vertical
elsif prev_actual_track == :vertical
actual_track = :vertical
else
actual_track = :horizontal
end
else
actual_track = _parse(track_piece)
end
# insert actual_track into grid
@grid[x][y] = actual_track
prev_actual_track = actual_track
end
end
end
def tick
@carts.sort_by! {|c| [c.coordinate.y, c.coordinate.x] }
([email protected]).each do |i|
next if @carts[i].nil?
@carts[i].tick
@carts[i].track_type = @grid[@carts[i].coordinate.x][@carts[i].coordinate.y]
# remove any collisions
([email protected]).each do |j|
next if @carts[i].nil?
next if @carts[j].nil?
next if @carts[i] == @carts[j]
next unless @carts[i].coordinate.x == @carts[j].coordinate.x
next unless @carts[i].coordinate.y == @carts[j].coordinate.y
@collided << @carts[i].dup
@collided << @carts[j].dup
@carts[i] = nil
@carts[j] = nil
end
end
@carts.compact!
end
def first_collision_coordinate
loop do
self.tick
break unless @collided.empty?
end
@collided.first.coordinate
end
def last_cart_standing_coordinate
loop do
self.tick
break if @carts.size == 1
end
@carts.first.coordinate
end
private
def _is_cart?(track_piece)
track_piece == '^' || track_piece == 'v' || track_piece == '<' || track_piece == '>'
end
def _parse_direction(track_piece)
case track_piece
when '^'
return :north
when 'v'
return :south
when '<'
return :west
when '>'
return :east
else
puts "_parse_direction called incorrectly with #{track_piece}"
end
end
def _parse(track_piece)
case track_piece
when '|'
return :vertical
when '-'
return :horizontal
when '/'
return :curve_right
when ' \ '.strip # work around emacs ruby-mode formatting bug
return :curve_left
when '+'
return :intersection
when ' '
return nil
else
puts "_parse called with: #{track_piece}"
end
end
end
def run_tests
puts "testing:"
system_map = ['|','v','|','|','|','^','|']
ts = TrackSystem.new(system_map)
test('0,3', ts.first_collision_coordinate.to_s)
system_map = raw_lines('data/day13_test_track_1.txt').select {|l| 0 < l.size }
ts = TrackSystem.new(system_map)
test('7,3', ts.first_collision_coordinate.to_s)
system_map = raw_lines('data/day13_test_track_2.txt').select {|l| 0 < l.size }
ts = TrackSystem.new(system_map)
test('6,4', ts.last_cart_standing_coordinate.to_s)
puts
end
def part1
system_map = raw_lines('data/day13_production_track.txt').select {|l| 0 < l.size }
ts = TrackSystem.new(system_map)
ts.first_collision_coordinate
end
def part2
system_map = raw_lines('data/day13_production_track.txt').select {|l| 0 < l.size }
ts = TrackSystem.new(system_map)
ts.last_cart_standing_coordinate
end
run_tests
puts "Part 1: To help prevent crashes, you'd like to know the location of the first crash."
puts "Answer: #{part1}"
puts "Part 2: What is the location of the last cart at the end of the first tick where it is the only cart left?"
puts "Answer: #{part2}"