day2
This commit is contained in:
parent
a9ec0217b1
commit
67f1187525
6
input/2021/day2-sample1.txt
Normal file
6
input/2021/day2-sample1.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
forward 5
|
||||||
|
down 5
|
||||||
|
forward 8
|
||||||
|
up 3
|
||||||
|
down 8
|
||||||
|
forward 2
|
1000
input/2021/day2.txt
Normal file
1000
input/2021/day2.txt
Normal file
File diff suppressed because it is too large
Load diff
|
@ -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}"
|
||||||
|
|
Loading…
Reference in a new issue