diff --git a/README.md b/README.md index 4ba7ac7..c063d0f 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ | 2021 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | ★ | ★ | | | | | | | | | | | | 26 | | 2022 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | | | | | | | ☆ | | | | ☆ | 28 | | 2023 | ★ | ★ | ★ | ★ | ☆ | ★ | ☆ | | | | | | | | | | | | | | | | | | | 12 | -| 2024 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ☆ | | | | | | | | | | | | 27 | +| 2024 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | | | | | | | | | | | | 28 | ## 🛷 How to run @@ -177,7 +177,7 @@ e.g. `HandyHaversacks`)* | | 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [[Code](src/main/kotlin/adventofcode/year2024/Day11PlutonianPebbles.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day11PlutonianPebblesSpec.kt)] | `194482` | `232454623677743` | | | 12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [[Code](src/main/kotlin/adventofcode/year2024/Day12GardenGroups.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day12GardenGroupsSpec.kt)] | `1573474` | `966476` | | | 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [[Code](src/main/kotlin/adventofcode/year2024/Day13ClawContraption.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day13ClawContraptionSpec.kt)] | `28138` | `108394825772874` | -| | 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [[Code](src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt)] | `224357412` | | +| | 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [[Code](src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt)] | `224357412` | `7083` | ## 🕯️ Useful commands diff --git a/src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt b/src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt index a1af446..d498672 100644 --- a/src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt +++ b/src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt @@ -32,6 +32,11 @@ class Day14RestroomRedoubt(customInput: PuzzleInput? = null) : Puzzle(customInpu .product() } + override fun partTwo() = + generateSequence(robots to 0) { (previous, seconds) -> previous.map(Robot::move) to seconds + 1 } + .first { (robots) -> robots.groupingBy(Robot::position).eachCount().all { (_, count) -> count == 1 } } + .second + companion object { private val ROBOT_REGEX = """p=(\d+),(\d+) v=(-?\d+),(-?\d+)""".toRegex() @@ -44,22 +49,8 @@ class Day14RestroomRedoubt(customInput: PuzzleInput? = null) : Puzzle(customInpu val velocity: Point2d, ) { fun move(): Robot { - val dx = position.x + velocity.x - val dy = position.y + velocity.y - - val x = - when { - dx < 0 -> BATHROOM_WIDTH + dx - dx >= BATHROOM_WIDTH -> dx - BATHROOM_WIDTH - else -> dx - } - - val y = - when { - dy < 0 -> BATHROOM_HEIGHT + dy - dy >= BATHROOM_HEIGHT -> dy - BATHROOM_HEIGHT - else -> dy - } + val x = (position.x + velocity.x).mod(BATHROOM_WIDTH) + val y = (position.y + velocity.y).mod(BATHROOM_HEIGHT) return Robot(Point2d(x, y), velocity) } diff --git a/src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt b/src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt index 35d1fdc..2ceaa92 100644 --- a/src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt +++ b/src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt @@ -2,6 +2,6 @@ package adventofcode.year2024 import adventofcode.PuzzleBaseSpec -// The example for part 1 uses a different grid size than the actual input, +// The example for part 1 uses a different grid size than the actual input and there is no example for part 2, // so this test runs the code on my actual input. -class Day14RestroomRedoubtSpec : PuzzleBaseSpec(224357412) +class Day14RestroomRedoubtSpec : PuzzleBaseSpec(224357412, 7083)