-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from ephemient/kt/day14
Day 14: Restroom Redoubt
- Loading branch information
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day14Bench.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day14.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day14Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |