Skip to content

Commit

Permalink
Day 14: Restroom Redoubt
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 14, 2024
1 parent 4b6b749 commit 5ae3381
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Development occurs in language-specific directories:
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)|[day11.py](py/aoc2024/day11.py)|[day11.rs](rs/src/day11.rs)|
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day12.kt)|[day12.py](py/aoc2024/day12.py)||
|[Day13.hs](hs/src/Day13.hs)|[Day13.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day13.kt)|[day13.py](py/aoc2024/day13.py)||
|[Day14.hs](hs/src/Day14.hs)||||
|[Day14.hs](hs/src/Day14.hs)|[Day14.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day14.kt)|||
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.ephemient.aoc2024.exe

import com.github.ephemient.aoc2024.Day14
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State

@State(Scope.Benchmark)
class Day14Bench {
private lateinit var input: String

@Setup
fun setup() {
input = getDayInput(14)
}

@Benchmark
fun part1() = Day14(input).part1()

@Benchmark
fun part2() = Day14(input).part2()

@Benchmark
fun solve(bh: Blackhole) {
val day14 = Day14(input)
bh.consume(day14.part1())
bh.consume(day14.part2())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.ephemient.aoc2024

class Day14(input: String) {
private val robots = input.lineSequence().mapNotNull {
val (x, y, vx, vy) = pattern.matchEntire(it)?.destructured ?: return@mapNotNull null
Robot(x.toInt(), y.toInt(), vx.toInt(), vy.toInt())
}.toList()

fun part1(width: Int = WIDTH, height: Int = HEIGHT): Int {
val quadrants = IntArray(4)
for (robot in robots) {
val x = (robot.x + robot.vx * 100).mod(width)
val y = (robot.y + robot.vy * 100).mod(height)
quadrants[
(if (x < width / 2) 0 else if (x > width / 2) 1 else continue) or
if (y < height / 2) 0 else if (y > height / 2) 2 else continue
]++
}
println(quadrants.toList())
return quadrants.fold(1, Int::times)
}

fun part2() = (0..<WIDTH * HEIGHT).map { t ->
val positions = robots.mapTo(mutableSetOf()) { robot ->
(robot.x + robot.vx * t).mod(WIDTH) to (robot.y + robot.vy * t).mod(HEIGHT)
}.sortedWith(compareBy(IntPair::second, IntPair::first))
var consecutive = 1
var maxConsecutive = 0
for ((i, pos) in positions.withIndex()) {
if (pos.first + 1 to pos.second == positions.getOrNull(i + 1)) {
consecutive += 1
} else {
maxConsecutive = maxOf(consecutive, maxConsecutive)
consecutive = 1
}
}
t to maxConsecutive
}.maxBy(IntPair::second).also(::println).first

private data class Robot(val x: Int, val y: Int, val vx: Int, val vy: Int)

companion object {
private const val WIDTH = 101
private const val HEIGHT = 103
private val pattern = """p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)""".toRegex()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ val days: List<Day> = listOf(
Day(11, ::Day11, Day11::part1, Day11::part2),
Day(12, ::Day12, Day12::part1, Day12::part2),
Day(13, ::Day13, Day13::part1, Day13::part2),
Day(14, ::Day14, Day14::part1, Day14::part2),
)

data class Day(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.ephemient.aoc2024

import kotlin.test.Test
import kotlin.test.assertEquals

class Day14Test {
@Test
fun part1() {
assertEquals(12, Day14(example).part1(11, 7))
}

companion object {
val example =
"""
|p=0,4 v=3,-3
|p=6,3 v=-1,-3
|p=10,3 v=-1,2
|p=2,0 v=2,-1
|p=0,0 v=1,3
|p=3,0 v=-2,-2
|p=7,6 v=-1,-3
|p=3,0 v=-1,-2
|p=9,3 v=2,3
|p=7,3 v=-1,2
|p=2,4 v=2,-3
|p=9,5 v=-3,-3
|""".trimMargin()
}
}

0 comments on commit 5ae3381

Please sign in to comment.