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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
require "set"
require "minitest"
require "minitest/pride"
class EndCombat < StandardError; end
class Combat
def initialize(map, elf_attack)
@map = map
@attack = {
Elf => elf_attack,
Goblin => 3,
}
end
def fight
return enum_for(__method__) unless block_given?
yield @map
loop do
turn_order.each do |pos|
next if @map[pos].nil? # don't take a turn for units that just died
turn = Turn.new(@map, pos)
if move = turn.move
unit = @map.delete(pos)
@map[move] = unit
turn.pos = move
end
if attack = turn.attack
unit = @map.fetch(turn.pos)
target = @map.fetch(attack)
target.hp -= @attack.fetch(unit.class)
@map.delete(attack) if target.hp <= 0
end
end
yield @map
end
rescue EndCombat
# yield @map
end
def turn_order
@map.units.keys.sort
end
end
class TestCombat < Minitest::Test
def test_move
map = Map.parse(<<~MAP)
#########
#G..G..G#
#.......#
#.......#
#G..E..G#
#.......#
#.......#
#G..G..G#
#########
MAP
combat = Combat.new(map, 3)
rounds = combat.fight
rounds.next # skip the first round
assert_equal <<~MAP.chomp, rounds.next.to_s
#########
#.G...G.#
#...G...#
#...E..G#
#.G.....#
#.......#
#G..G..G#
#.......#
#########
MAP
assert_equal <<~MAP.chomp, rounds.next.to_s
#########
#..G.G..#
#...G...#
#.G.E.G.#
#.......#
#G..G..G#
#.......#
#.......#
#########
MAP
assert_equal <<~MAP.chomp, rounds.next.to_s
#########
#.......#
#..GGG..#
#..GEG..#
#G..G...#
#......G#
#.......#
#.......#
#########
MAP
end
def test_fight
map = Map.parse(<<~MAP)
#######
#.G...#
#...EG#
#.#.#G#
#..G#E#
#.....#
#######
MAP
combat = Combat.new(map, 3)
rounds = combat.fight
rounds.next # skip the initial state
rounds.next # skip the first found
map = rounds.next # second round
assert_equal 188, map.fetch(Position[2,4]).hp
20.times { rounds.next }
assert_equal <<~MAP.chomp, rounds.next.to_s
#######
#...G.#
#..G.G#
#.#.#G#
#...#E#
#.....#
#######
MAP
end
def test_turn_order
map = Map.parse(<<~MAP)
#######
#.G.E.#
#E.G.E#
#.G.E.#
#######
MAP
combat = Combat.new(map, 3)
expected = [[1,2], [1,4], [2,1], [2,3], [2,5], [3,2], [3,4]].map {|pos| Position[*pos]}
assert_equal expected, combat.turn_order
end
end
class Turn
attr_accessor :pos
def initialize(map, pos)
@map, @pos = map, pos
@unit = @map.fetch(@pos)
end
def move
raise EndCombat if targets.empty?
return if can_attack?
return if nearest.empty?
chosen = nearest.min
haystack = @map.in_range(@pos)
dijkstra(chosen)
.select {|pos,_| haystack.include?(pos) }
.min_by {|pos,distance| [distance, pos] }
.first
end
def attack
attackable.min_by {|pos, target| [target.hp, pos] }&.first
end
def targets
@map.units.select {|_, other| other.is_a?(@unit.enemy) }
end
def attackable
targets.select {|pos, _| @pos.adjacent.include?(pos) }
end
def can_attack?
!attackable.empty?
end
def in_range
targets.flat_map {|pos, _| @map.in_range(pos) }.uniq
end
def reachable
dijkstra(@pos).select {|pos,_| in_range.include?(pos) }
end
def nearest
nearest = Hash.new {|h,k| h[k] = [] }
reachable.each do |pos, distance|
nearest[distance] << pos
end
return [] if nearest.empty?
nearest.min_by(&:first).last
end
private
def dijkstra(pos)
distances = { pos => 0 }
queue = [ pos ]
while pos = queue.shift
distance = distances.fetch(pos) + 1
@map.in_range(pos).reject {|pos| distances.has_key?(pos) }.each do |pos|
distances[pos] = distance
queue << pos
end
end
distances
end
end
class TestTurn < Minitest::Test
def setup
map = Map.parse(<<~MAP)
#######
#E..G.#
#...#.#
#.G.#G#
#######
MAP
@turn = Turn.new(map, Position[1,1])
end
def test_targets
assert_equal [[1,4], [3,2], [3,5]].to_set, @turn.targets.keys.map(&:to_a).to_set
end
def test_in_range
assert_equal [[1,3], [1,5], [2,2], [2,5], [3,1], [3,3]].to_set, @turn.in_range.map(&:to_a).to_set
end
def test_reachable
assert_equal [[1,3], [2,2], [3,1], [3,3]].to_set, @turn.reachable.keys.map(&:to_a).to_set
end
def test_nearest
assert_equal [[1,3], [2,2], [3,1]].to_set, @turn.nearest.map(&:to_a).to_set
end
def test_move
assert_equal Position[1,2], @turn.move
turn = Turn.new(Map.parse(<<~MAP), Position[1,2])
#######
#.E...#
#.....#
#...G.#
#######
MAP
assert_equal Position[1,3], turn.move
end
end
class Map
def self.parse(s)
occupied = {}
s.chomp.lines.each.with_index do |row, y|
row.chomp.chars.each.with_index do |square, x|
case square
when ?#
occupied[Position[y, x]] = Wall.new
when ?E
occupied[Position[y, x]] = Elf.new
when ?G
occupied[Position[y, x]] = Goblin.new
when ?.
else
fail "Invalid character: #{square.inspect}"
end
end
end
self.new(occupied)
end
def initialize(occupied)
@occupied = occupied
end
def [](pos)
@occupied[pos]
end
def []=(pos, square)
@occupied[pos] = square
end
def fetch(pos)
@occupied.fetch(pos)
end
def delete(pos)
@occupied.delete(pos)
end
def units
@occupied.select {|_, sq| sq.is_a?(Unit) }
end
def in_range(pos)
pos
.adjacent
.reject {|pos| @occupied.has_key?(pos) }
end
def to_s
max_y = @occupied.keys.map(&:y).max
max_x = @occupied.keys.map(&:x).max
(0..max_y).map {|y|
(0..max_x).map {|x|
case @occupied[Position[y, x]]
when Wall
?#
when Elf
?E
when Goblin
?G
when nil
?.
else
fail "Unexpected object: #{@occupied[Position[y, x]]}"
end
}.join
}.join(?\n)
end
end
class TestMap < Minitest::Test
def test_parse
map = Map.parse(<<~MAP)
#######
#E..G.#
#...#.#
#.G.#G#
#######
MAP
assert_instance_of Wall, map[Position[0,0]]
assert_instance_of Wall, map[Position[2,4]]
assert_instance_of Wall, map[Position[4,6]]
assert_instance_of Elf, map[Position[1,1]]
assert_instance_of Goblin, map[Position[1,4]]
assert_instance_of Goblin, map[Position[3,5]]
assert_nil map[Position[1,2]]
assert_nil map[Position[2,5]]
end
def test_units
map = Map.parse(<<~MAP)
#######
#E..G.#
#...#.#
#.G.#G#
#######
MAP
assert_equal 4, map.units.length
end
end
Position = Struct.new(:y, :x) do
def self.[](y, x)
self.new(y, x)
end
def <=>(other)
[self.y, self.x] <=> [other.y, other.x]
end
def adjacent
[-1, 1]
.flat_map {|d| [[y, x+d], [y+d, x]] }
.map {|pos| Position[*pos] }
end
def to_a
[y, x]
end
def to_s
to_a.to_s
end
end
class Unit
attr_accessor :hp
def initialize
@hp = 200
end
def to_s
"#{self.class.name.chars.first}(#{hp})"
end
end
class Wall
end
class Elf < Unit
def enemy
Goblin
end
end
class Goblin < Unit
def enemy
Elf
end
end
def solve(input)
(3..Float::INFINITY).each do |elf_attack|
map = Map.parse(input)
outcome = simulate(map, elf_attack)
next if outcome.nil?
return outcome
end
end
def simulate(map, elf_attack)
elf_count = map.units.count {|_, unit| unit.is_a?(Elf) }
combat = Combat.new(map, elf_attack)
map, count = combat.fight.map.with_index
.inject(nil) {|last,(map,count)|
return if map.units.count {|_, unit| unit.is_a?(Elf) } < elf_count
# puts map, map.units.values.map(&:to_s).inspect
[map, count]
}
return if map.units.count {|_, unit| unit.is_a?(Elf) } < elf_count
map.units.values.map(&:hp).sum * count
end
class TestSolve < Minitest::Test
def test_solve
# assert_equal 27730, solve(<<~INPUT)
assert_equal 4988, solve(<<~INPUT)
#######
#.G...#
#...EG#
#.#.#G#
#..G#E#
#.....#
#######
INPUT
# assert_equal 36334, solve(<<~INPUT)
########
##G..#E#
##E#E.E#
##G.##.#
##...#E#
##...E.#
########
#INPUT
# assert_equal 39514, solve(<<~INPUT)
assert_equal 31284, solve(<<~INPUT)
#######
#E..EG#
#.#G.E#
#E.##E#
#G..#.#
#..E#.#
#######
INPUT
# assert_equal 27755, solve(<<~INPUT)
assert_equal 3478, solve(<<~INPUT)
#######
#E.G#.#
#.#G..#
#G.#.G#
#G..#.#
#...E.#
#######
INPUT
# assert_equal 28944, solve(<<~INPUT)
assert_equal 6474, solve(<<~INPUT)
#######
#.E...#
#.#..G#
#.###.#
#E#G#G#
#...#G#
#######
INPUT
# assert_equal 18740, solve(<<~INPUT)
assert_equal 1140, solve(<<~INPUT)
#########
#G......#
#.E.#...#
#..##..G#
#...##..#
#...#...#
#.G...G.#
#.....G.#
#########
INPUT
end
end
if __FILE__ == $0
require "minitest/autorun" and exit if ENV["TEST"]
puts solve(ARGF.read)
end