-
Notifications
You must be signed in to change notification settings - Fork 1
/
a15.nim
180 lines (134 loc) · 6.06 KB
/
a15.nim
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
import sequtils, strutils, heapqueue, sugar, algorithm, math, lists
type Position = tuple[x,y:int]
# --- Unit ---
type Unit = ref object of RootObj
pos:Position
hitPoints:int
attackPower:int
value:char
# --- Node ---
type Node = ref object
value:char
dist:int
neighbours:seq[Node]
pos:Position
proc addNeighbour(a,b:Node) =
a.neighbours.add(b)
b.neighbours.add(a)
proc readingOrder(a,b:Node|Unit):int =
result = cmp(a.pos.y, b.pos.y)
if result == 0: result = cmp(a.pos.x, b.pos.x)
# Needed for HeapQueue
proc `<`(a,b:Node):bool = a.dist < b.dist
# --- DistanceMap ---
type DistanceMap = seq[seq[Node]]
proc `[]`(self:DistanceMap, pos:Position):Node = self[pos.y][pos.x]
proc `[]`(self:DistanceMap, unit:Unit):Node = self[unit.pos.y][unit.pos.x]
proc init(map:seq[string]): DistanceMap =
result = newSeqWith(map.len, newSeq[Node](map[0].len))
# Create nodes
for y in 0..<map.len:
for x in 0..<map[y].len:
result[y][x] = Node(value: map[y][x], pos: (x,y), dist: int.high)
# Connect neighbours
for y in 0..<map.len-1:
for x in 0..<map[y].len-1:
addNeighbour(result[y][x], result[y][x+1])
addNeighbour(result[y][x], result[y+1][x])
proc newDistanceMap(map:seq[string], start:Position):DistanceMap =
result = init(map)
# Djikstra
var q = newHeapQueue[Node]()
result[start].dist = 0
q.push(result[start])
while q.len > 0:
let curr = q.pop()
for n in curr.neighbours:
if n.dist > curr.dist + 1 and n.value != '#':
n.dist = curr.dist + 1
if n.value == '.':
q.push(n)
# --- Map ---
type Map = seq[string]
template `[]=`(self:Map, pos:Position, c:char) = self[pos.y][pos.x] = c
iterator squares(self:Map):tuple[value:char, pos:Position] =
for y in 0..<self.len:
for x in 0..<self[y].len:
yield (self[y][x], (x,y))
# --- Part 1 ---
proc play(filename:string, elfAttackPower:int = 3):tuple[rounds, remains:int, winners:char, killedElves:int] =
var units : seq[Unit]
# Load map
var map = toSeq(lines(filename))
for square in map.squares:
if square.value == 'E':
units.add(Unit(hitPoints:200, attackPower: elfAttackPower, value: square.value, pos: square.pos))
elif square.value == 'G':
units.add(Unit(hitPoints:200, attackPower: 3, value: square.value, pos: square.pos))
var rounds = 0
var done = false
while not done:
for unit in units.sorted(readingOrder):
if unit.hitPoints <= 0: continue
# Map of distances to current unit
var dist = newDistanceMap(map, unit.pos)
# Get list of all enemies that are still alive
var enemyValue = if unit.value == 'E': 'G' else:'E'
let enemies = units.filterIt(it.value == enemyValue).filterIt(it.hitPoints > 0)
# No enemies left? Game over.
if enemies.len == 0:
done = true
break
# < MOVE >
let shouldMove = enemies.filterIt(dist[it].dist == 1).len == 0
if shouldMove:
# 1. Get all empty squares next to all enemies
let inRangeSquares = enemies.mapIt(dist[it].neighbours).concat.filterIt(it.value == '.')
# 1. Filter out those which are unreachable
let reachableSquares = inRangeSquares.filterIt(it.dist < int.high)
if reachableSquares.len > 0:
# 3. Get the closest squares
let minDist = reachableSquares.mapIt(it.dist).min
let nearestSquares = reachableSquares.filterIt(it.dist == minDist)
if(nearestSquares.len > 0):
# 4. Sort by reading order and choose the first one
let choosenSquare = nearestSquares.sorted(readingOrder)[0]
# 5. Turn it all around. Get all paths to choosen square and see which neighbour is closest
let dist2 = newDistanceMap(map, choosenSquare.pos)
let unitNeighbourNodes = dist2[unit].neighbours.filterIt(it.value == '.')
let minDist = unitNeighbourNodes.mapIt(it.dist).min
let nearestPaths = unitNeighbourNodes.filterIt(it.dist == minDist)
let moveToPos = nearestPaths.sorted(readingOrder)[0].pos
# Make the move
map[unit.pos] = '.'
unit.pos = moveToPos
map[unit.pos] = unit.value
dist = newDistanceMap(map, unit.pos)
# < ATTACK >
var toAttack = enemies.filterIt(dist[it].dist == 1)
if toAttack.len > 0:
let minHitPoints = toAttack.mapIt(it.hitPoints).min
let targets = toAttack.filterIt(it.hitPoints == minHitPoints).sorted(readingOrder)
let target = targets[0]
target.hitPoints -= unit.attackPower
if target.hitPoints <= 0:
map[target.pos] = '.'
if not done:
inc(rounds)
# Finish up
let remains = units.filterIt(it.hitPoints > 0).mapIt(it.hitPoints).sum
let killedElves = units.filterIt(it.value == 'E' and it.hitPoints <= 0).len
let winners = units.filterIt(it.hitPoints > 0)[0].value
result = (rounds, remains, winners, killedElves)
# --- Part 1 ---
let outcome = play("15_input.txt")
echo "Part 1: ", outcome.rounds * outcome.remains
# --- Part 2 ---
var power = 4
var done = false
var outcomePart2:tuple[rounds, remains:int, winners:char, killedElves:int]
while not done:
inc(power)
outcomePart2 = play("15_input.txt", power)
done = outcomePart2.winners == 'E' and outcomePart2.killedElves == 0
echo "Part 2: ", outcomePart2.rounds * outcomePart2.remains