Skip to content

Commit

Permalink
[2024/14] Restroom Redoubt (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 14, 2024
1 parent 73a3c88 commit dc18df1
Show file tree
Hide file tree
Showing 5 changed files with 585 additions and 2 deletions.
3 changes: 2 additions & 1 deletion 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 |||||||||||||| | | | | | | | | | | | | 26 |
| 2024 |||||||||||||| | | | | | | | | | | | | 27 |

## 🛷 How to run

Expand Down Expand Up @@ -177,6 +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` | |

## 🕯️ Useful commands

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/adventofcode/common/spatial/Point2d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class Point2d(
operator fun minus(other: Point2d): Point2d = Point2d(x - other.x, y - other.y)

/**
* Returns a set of neighboring Point2d's. Can include negative coordinates.
* Returns a set of neighboring points. Can include negative coordinates.
*
* Excluding diagonals: Including diagonals:
* _ N _ N N N
Expand Down
75 changes: 75 additions & 0 deletions src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package adventofcode.year2024

import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.product
import adventofcode.common.spatial.Point2d

class Day14RestroomRedoubt(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val robots by lazy { input.lines().map(Robot::invoke) }

override fun partOne(): Int {
val robotCount =
generateSequence(robots) { previous -> previous.map(Robot::move) }
.drop(1)
.take(SECONDS)
.last()
.groupingBy(Robot::position)
.eachCount()

val leftHalf = 0 until BATHROOM_WIDTH / 2
val rightHalf = BATHROOM_WIDTH / 2 + 1 until BATHROOM_WIDTH
val topHalf = 0 until BATHROOM_HEIGHT / 2
val bottomHalf = BATHROOM_HEIGHT / 2 + 1 until BATHROOM_HEIGHT

return setOf(
leftHalf to topHalf,
rightHalf to topHalf,
leftHalf to bottomHalf,
rightHalf to bottomHalf,
)
.map { (qx, qy) -> robotCount.filterKeys { point -> point.x in qx && point.y in qy }.values.sum() }
.product()
}

companion object {
private val ROBOT_REGEX = """p=(\d+),(\d+) v=(-?\d+),(-?\d+)""".toRegex()

private const val SECONDS = 100
private const val BATHROOM_WIDTH = 101
private const val BATHROOM_HEIGHT = 103

private data class Robot(
val position: Point2d,
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
}

return Robot(Point2d(x, y), velocity)
}

companion object {
operator fun invoke(input: String): Robot {
val (px, py, vx, vy) = ROBOT_REGEX.find(input)!!.destructured
return Robot(Point2d(px.toInt(), py.toInt()), Point2d(vx.toInt(), vy.toInt()))
}
}
}
}
}
Loading

0 comments on commit dc18df1

Please sign in to comment.