-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.sc
38 lines (31 loc) · 1.25 KB
/
day02.sc
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
// scala 2.13.6
import ammonite.ops._
import $file.runner
case class Position(hoz: Int = 0, depth: Int = 0)
case class WithAim(aim: Int = 0, pos: Position = Position())
runner.exec[Int]("day02") { (fileName, assert) =>
val entries = read.lines(pwd / fileName)
val finalPos = entries.foldLeft(Position()) { (pos, command) =>
command match {
case s"forward $step" => pos.copy(hoz = pos.hoz + step.toInt)
case s"down $step" => pos.copy(depth = pos.depth + step.toInt)
case s"up $step" => pos.copy(depth = pos.depth - step.toInt)
}
}
assert("final position", 150, 1383564)(finalPos.hoz * finalPos.depth)
val withAim = entries.foldLeft(WithAim()) { (pos, command) =>
command match {
case s"forward $step" =>
val newPosition = pos.pos.copy(
hoz = pos.pos.hoz + step.toInt,
depth = pos.pos.depth + pos.aim * step.toInt
)
pos.copy(pos = newPosition)
case s"down $step" => pos.copy(aim = pos.aim + step.toInt)
case s"up $step" => pos.copy(aim = pos.aim - step.toInt)
}
}
val hoz = assert("hoz with aim", 15, 1911)(withAim.pos.hoz)
val depth = assert("depth with aim", 60, 778813)(withAim.pos.depth)
assert("position with aim", 900, 1488311643)(hoz * depth)
}