This commit is contained in:
Alexander Gehrke 2021-12-02 23:35:52 +01:00
parent a9ec0217b1
commit 67f1187525
3 changed files with 1029 additions and 1 deletions

View file

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

1000
input/2021/day2.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -3,4 +3,26 @@ package de.qwertyuiop.aoc.`2021`
import de.qwertyuiop.aoc.lib.* import de.qwertyuiop.aoc.lib.*
import cats.*, cats.implicits.given import cats.*, cats.implicits.given
def day2(using InputSource): Unit = ??? import Vectors.*
def day2(using InputSource): Unit =
val commands = input()
println(simpleCommands(commands))
println(aimCommands(commands))
def simpleCommands(commands: List[String]) =
val finalPos = commands.map{
case s"forward $x" => Vec2D(x.toInt, 0)
case s"down $y" => Vec2D(0, y.toInt)
case s"up $y" => Vec2D(0, - y.toInt)
}.combineAll
s"(1) Final position: ${finalPos}\nResult: ${finalPos.x * finalPos.y}"
def aimCommands(commands: List[String]) =
val finalPos = commands.map{
case s"forward $x" => Vec3D(x.toInt, x.toInt, 0)
case s"down $aim" => Vec3D(0, 0, aim.toInt)
case s"up $aim" => Vec3D(0, 0, - aim.toInt)
}.foldLeft(Vec3D(0,0,0))((pos, command) => Vec3D(pos.x + command.x, pos.y + pos.z * command.y, pos.z + command.z))
s"(2) Final position: ${finalPos}\nResult: ${finalPos.x * finalPos.y}"