Skip to content

Commit

Permalink
[2024/14] Restroom Redoubt (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 14, 2024
1 parent dc18df1 commit 43a846d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| 2021 |||||||||||| ||| | | | | | | | | | | | 26 |
| 2022 |||||||||||||| | | | | | | || | | || 28 |
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
| 2024 |||||||||||||| | | | | | | | | | | | | 27 |
| 2024 |||||||||||||| | | | | | | | | | | | | 28 |

## 🛷 How to run

Expand Down Expand Up @@ -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

Expand Down
23 changes: 7 additions & 16 deletions src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 43a846d

Please sign in to comment.