forked from matelaszlo/advent-of-code-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.scala
288 lines (232 loc) · 11.3 KB
/
Day15.scala
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
package com.lmat.adventofcode.year2018
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.adventofcode.year2018.Day15Definitions._
import com.lmat.util.Files.readResource
import scala.annotation.tailrec
object Day15Definitions {
sealed trait Tile
case object Empty extends Tile
case object Wall extends Tile
sealed trait Character extends Tile {
def attack: Int
def hp: Int
}
case class Goblin(attack: Int, hp: Int) extends Character
case class Elf(attack: Int, hp: Int) extends Character
type Coordinates = (Int, Int)
case class CombatMap(map: Map[Coordinates, Tile]) {
val elves: Seq[(Coordinates, Elf)] = map.collect { case (c, elf@Elf(_, _)) => (c, elf) }.toSeq
val goblins: Seq[(Coordinates, Goblin)] = map.collect { case (c, goblin@Goblin(_, _)) => (c, goblin) }.toSeq
val characters: Seq[(Coordinates, Character)] = (elves ++ goblins).sortBy{case ((x, y), _) => (y, x)}
}
type CombatState = (CombatMap, Int, Boolean)
}
object Day15 extends SimpleCommonPuzzle[CombatMap, Int, Int] {
override def parse(resource: String): CombatMap = parseCombatState(readResource(resource))
def parseCombatState(string: String): CombatMap =
parseCombatState(string.split("\n").toIndexedSeq)
def parseCombatState(rows: Seq[String]): CombatMap = {
def parseRow(rowIndex: Int)(row: String): Seq[(Coordinates, Tile)] =
row.zipWithIndex.map { case (char, columnIndex) => ((columnIndex, rowIndex), parseTile(char)) }
def parseTile(char: Char): Tile = char match {
case '#' => Wall
case '.' => Empty
case 'G' => Goblin(3, 200)
case 'E' => Elf(3, 200)
case _ => throw new IllegalArgumentException(s"Cannot parse tile: $char")
}
CombatMap(rows.zipWithIndex.flatMap { case (row, index) => parseRow(index)(row) }.toMap)
}
override def part1(combatMap: CombatMap): Int = {
// printCombatState(combatMap, 0)
val result = simulateCombat(combatMap)
// printCombatState(result._1, result._2)
calculateScore(result)
}
def calculateScore(combatState: CombatState): Int = {
val (combat, round, isIncompleteLast) = combatState
if(isIncompleteLast) (round - 1) * winners(combat).map(_._2).map(_.hp).sum
else round * winners(combat).map(_._2).map(_.hp).sum
}
def simulateCombat(combatMap: CombatMap): CombatState =
LazyList.iterate((combatMap, 0, false))(simulateRound).find(isFinished).get
def simulateRound(combat: CombatState): CombatState = {
val (combatMap, round, _) = combat
val (nextCombatMap, isIncompleteLast) = simulateRound(combatMap)
// printCombatState(combatMap, round)
(nextCombatMap, round + 1, isIncompleteLast)
}
/**
* We need to identify when a round finishes early by eliminating the last opponent when there are still characters with unfinished turns
*/
def simulateRound(combat: CombatMap): (CombatMap, Boolean) = {
val characters = combat.characters
@tailrec
def iterate(combatMap: CombatMap, remaining: Seq[(Coordinates, Character)]): (CombatMap, Boolean) = remaining match {
case (coordinates, character) +: rest =>
val (nextCombatMap, eliminated) = simulateTurn(combatMap)(coordinates, character)
if(isFinished(nextCombatMap) && rest.nonEmpty) (nextCombatMap, true)
else iterate(nextCombatMap, rest.filterNot { case (c, _) => eliminated.contains(c) })
case _ => (combatMap, false)
}
iterate(combat, characters)
}
def simulateTurn(combatMap: CombatMap)(coordinates: Coordinates, character: Character): (CombatMap, Option[Coordinates]) = {
if (canAttack(combatMap)(coordinates, character)) attack(combatMap)(coordinates, character)
else if (canMove(combatMap)(coordinates, character)) {
val (updated, moved) = move(combatMap)(coordinates, character)
if (canAttack(updated)(moved, character)) attack(updated)(moved, character)
else (updated, None)
}
else (combatMap, None)
}
def canAttack(combatMap: CombatMap)(coordinates: Coordinates, character: Character): Boolean ={
neighbours(combatMap)(coordinates).map(_._2).exists(isEnemy(character))
}
def attack(combatMap: CombatMap)(coordinates: Coordinates, character: Character): (CombatMap, Option[Coordinates]) = {
val enemies = getEnemies(neighbours(combatMap)(coordinates))(character)
val (c, toAttack) = enemies.minBy{ case ((x, y), enemy) => (enemy.hp, y, x) }
val (attacked, survived) = attack(character, toAttack)
if(survived) (CombatMap(combatMap.map.updated(c, attacked)), None)
else (CombatMap(combatMap.map.updated(c, Empty)), Some(c))
}
def attack(attacker: Character, toAttack: Character): (Character, Boolean) = {
val attacked = toAttack match {
case Elf(a, hp) => Elf(a, hp - attacker.attack)
case Goblin(a, hp) => Goblin(a, hp - attacker.attack)
}
(attacked, attacked.hp > 0)
}
def getReachable(combatMap: CombatMap, from: Coordinates): Set[Coordinates] = {
@tailrec
def iterate(toTest: Seq[Coordinates], area: Set[Coordinates]): Set[Coordinates] = toTest match {
case h +: rest =>
val newTiles = neighbours(combatMap)(h).collect{ case (c, Empty) if !area.contains(c) => c}
iterate(rest ++ newTiles, area ++ newTiles)
case _ => area
}
iterate(Seq(from), Set())
}
def canMove(combatMap: CombatMap)(coordinates: Coordinates, character: Character): Boolean = {
val targets = getTargets(combatMap)(coordinates, character)
if(targets.isEmpty) false
else {
val reachable = getReachable(combatMap, coordinates)
targets.exists(reachable.contains)
}
}
def getTargets(combatMap: CombatMap)(coordinates: Coordinates, character: Character): Seq[Coordinates] =
getEnemies(combatMap.characters)(character).flatMap{ case (c, _) => neighbours(combatMap)(c)}.collect{ case (c, Empty) => c}
def move(combatMap: CombatMap)(coordinates: Coordinates, character: Character): (CombatMap, Coordinates) = {
val reachableCells = getReachable(combatMap, coordinates)
val reachable = getTargets(combatMap)(coordinates, character).filter(reachableCells.contains)
val goal = findGoal(combatMap, coordinates)(reachable.toSet)
val step = shortestPath(combatMap, coordinates)(goal).drop(1).head
(CombatMap(combatMap.map.updated(coordinates, Empty).updated(step, character)), step)
}
/**
* Notice how we pre collected all the reachable targets and iterate only until we found all the ones that are at most as far as the first found correct one
*/
def findGoal(combatMap: CombatMap, from: Coordinates)(targets: Set[Coordinates]): Coordinates = {
def streamSearch(initial: LazyList[(Coordinates, Int)], explored: Set[Coordinates]): LazyList[(Coordinates, Int)] = initial match {
case (c, i) #:: rest if targets.contains(c) =>
LazyList((c, i)) #::: streamSearch(rest, explored).takeWhile{case (_, i2) => i2 == i}
case (c, i) #:: rest =>
val more = emptyNewNeighbours(combatMap, explored)(c).map((_, i + 1))
streamSearch(rest #::: more.to(LazyList), explored ++ more.map(_._1))
case _ => LazyList()
}
streamSearch(LazyList((from, 0)), Set()).minBy { case ((x, y), d) => (d, y, x) }._1
}
/**
* As long as we generate the next possible coordinates in reading order we will find the correct solution even when there are multiple shortest paths
*/
def shortestPath(combatMap: CombatMap, from: Coordinates)(target: Coordinates): Seq[Coordinates] = {
def streamSearch(initial: LazyList[List[Coordinates]], explored: Set[Coordinates]): LazyList[List[Coordinates]] = initial match {
case (current :: path) #:: _ if current == target =>
LazyList(current :: path)
case (current :: path) #:: rest =>
val more = emptyNewNeighbours(combatMap, explored)(current).map(_ :: current :: path)
streamSearch(rest #::: more.to(LazyList), explored ++ more.flatten)
case _ => LazyList()
}
streamSearch(LazyList(List(from)), Set()).find(_.head == target).map(_.reverse).get
}
def emptyNewNeighbours(combatMap: CombatMap, explored: Set[Coordinates])(coordinates: Coordinates): List[Coordinates] =
neighbours(combatMap)(coordinates).collect{ case (c, Empty) if !explored.contains(c) => c}
def neighbours(combatMap: CombatMap)(coordinates: Coordinates): List[(Coordinates, Tile)] =
neighbours(coordinates).map(c => (c, combatMap.map(c)))
/**
* Returns the neighbours in reading order
*/
def neighbours(coordinates: Coordinates): List[Coordinates] = {
val (x, y) = coordinates
List(
(x, y - 1),
(x - 1, y),
(x + 1, y),
(x, y + 1),
)
}
def isEnemy(character: Character)(tile: Tile): Boolean = (character, tile) match {
case (Elf(_, _), Goblin(_, _)) => true
case (Goblin(_, _), Elf(_, _)) => true
case _ => false
}
def getEnemies(tiles: Seq[(Coordinates, Tile)])(character: Character): Seq[(Coordinates, Character)] =
tiles.filter { case (_, tile) => isEnemy(character)(tile) }.map { case (c, char) => (c, char.asInstanceOf[Character]) }
def isFinished(combat: CombatState): Boolean = isFinished(combat._1)
def isFinished(combatMap: CombatMap): Boolean =
combatMap.elves.isEmpty || combatMap.goblins.isEmpty
def winners(combat: CombatMap): Seq[(Coordinates, Character)] =
if(combat.elves.isEmpty) combat.goblins
else combat.elves
def asString(combatMap: CombatMap, withCharacters: Boolean = false): String = {
val coordinates = combatMap.map.keys
val xs = coordinates.map(_._1)
val ys = coordinates.map(_._2)
def char(tile: Tile): Char = tile match {
case Empty => '.'
case Wall => '#'
case Elf(_, _) => 'E'
case Goblin(_, _) => 'G'
}
def row(y: Int): String = {
val row = (for (x <- xs.min to xs.max) yield char(combatMap.map((x, y)))).mkString
if (withCharacters) {
val characters = (for (x <- xs.min to xs.max) yield combatMap.map((x, y)))
.collect {
case Goblin(_, hp) => s"G($hp)"
case Elf(_, hp) => s"E($hp)"
}.mkString(", ")
s"$row $characters"
}
else row
}
(for {y <- ys.min to ys.max} yield row(y)).mkString("\n")
}
def printCombatState(combatMap: CombatMap, round: Int, attack: Option[Int] = None): Unit = {
attack.foreach(_ => println(s"Attack #$attack"))
println(s"Round #$round")
println(asString(combatMap, true))
}
override def part2(combatMap: CombatMap): Int = {
printCombatState(combatMap, 0)
val elvesWinWithoutLosses = LazyList.from(3).map(attack => {
val result = simulateCombat(combatMap, attack)
printCombatState(result._1, result._2, Some(attack))
result
}).find(doElvesWinWithoutLosses(combatMap.elves.size)).get
calculateScore(elvesWinWithoutLosses)
}
def simulateCombat(combatMap: CombatMap, elvenAttack: Int): CombatState = {
val modified = updateElvenAttack(combatMap, elvenAttack)
simulateCombat(modified)
}
def updateElvenAttack(combat: CombatMap, attack: Int): CombatMap =
combat.elves.foldLeft(combat) { case (c, (coordinates, elf)) => CombatMap(c.map.updated(coordinates, elf.copy(attack = attack))) }
def doElvesWinWithoutLosses(elfSize: Int)(combatState: CombatState): Boolean = {
val (combat, _, _) = combatState
combat.elves.size == elfSize
}
}