diff --git a/src/main/kotlin/no/rodland/advent_2024/Day14.kt b/src/main/kotlin/no/rodland/advent_2024/Day14.kt index a9b0268d..5e3c356e 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day14.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day14.kt @@ -2,32 +2,59 @@ package no.rodland.advent_2024 import no.rodland.advent.Day import no.rodland.advent.Pos +import product // template generated: 14/12/2024 // Fredrik Rødland 2024 -class Day14(val input: List) : Day> { +class Day14(val input: List, val width: Int, val height: Int) : Day> { private val robots = input.parse() override fun partOne(): Long { - return 2 + return robots.map { it.move(100, width, height) } + .groupBy { it.quadrant(width, height) } + .toSortedMap() + .filterKeys { i: Int -> i != 0 } // middle + .values + .map { it.size } + .product() } override fun partTwo(): Long { return 2 } + data class Robot(val pos: Pos, val vel: Pos) { + fun move(iterations: Int, width: Int, height: Int): Pos { + val x = (pos.x + (((iterations) * vel.x) % width) + width) % width + val y = (pos.y + (((iterations) * vel.y) % height) + height) % height + return Pos(x, y) + } + } + + private fun Pos.quadrant(width: Int, height: Int): Int { + val firstX = this.x < (width / 2) + val lastX = this.x >= ((width / 2) + 1) + val firstY = this.y < (height / 2) + val lastY = this.y >= ((height / 2) + 1) + return when { + firstX && firstY -> 1 + lastX && firstY -> 2 + firstX && lastY -> 3 + lastX && lastY -> 4 + else -> 0 + } + } + override fun List.parse(): List { return map { line -> - // p=0,4 v=3,-3 val (px, py) = line.substringAfter("p=").substringBefore(" v=").split(",").map { it.toInt() } val (vx, vy) = line.substringAfter("v=").split(",").map { it.toInt() } Robot(Pos(px, py), Pos(vx, vy)) } } - data class Robot(val pos: Pos, val vel: Pos) - override val day = "14".toInt() } + diff --git a/src/test/kotlin/no/rodland/advent_2024/Day14Test.kt b/src/test/kotlin/no/rodland/advent_2024/Day14Test.kt index e40ab5c6..fe1fe49b 100644 --- a/src/test/kotlin/no/rodland/advent_2024/Day14Test.kt +++ b/src/test/kotlin/no/rodland/advent_2024/Day14Test.kt @@ -15,20 +15,24 @@ internal class Day14Test { private val data14 = "2024/input_14.txt".readFile() private val test14 = "2024/input_14_test.txt".readFile() - private val resultTestOne = 2L + private val resultTestOne = 12L private val resultTestTwo = 2L - private val resultOne = 2L + private val resultOne = 221142636L private val resultTwo = 2L +// private val width = 101 +// private val height = 103 +// private val widthExample = 11 +// private val heightExample = 7 val test = defaultTestSuiteParseOnInit( - Day14(data14), - Day14(test14), + Day14(data14, 101, 103), + Day14(test14, 11, 7), resultTestOne, resultOne, resultTestTwo, resultTwo, - { Day14(data14) }, - { Day14(test14) }, + { Day14(data14, 101, 103) }, + { Day14(test14, 11, 7) }, ) @Nested