-
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 #120 from ephemient/kt/day18
Day 18: RAM Run
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 deletions.
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
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 |
---|---|---|
|
@@ -28,4 +28,4 @@ class Day11Bench { | |
bh.consume(day11.part1()) | ||
bh.consume(day11.part2()) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day18Bench.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.Day18 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Blackhole | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day18Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(18) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day18(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day18(input).part2() | ||
|
||
@Benchmark | ||
fun solve(bh: Blackhole) { | ||
val day18 = Day18(input) | ||
bh.consume(day18.part1()) | ||
bh.consume(day18.part2()) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day18.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,39 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day18(input: String, private val size: Int = 70) { | ||
private val coords = input.lineSequence().mapNotNull { line -> | ||
val x = line.substringBefore(',').toIntOrNull() ?: return@mapNotNull null | ||
val y = line.substringAfter(',').toIntOrNull() ?: return@mapNotNull null | ||
x to y | ||
}.toList() | ||
|
||
private fun findPath(obstacles: Iterable<IntPair>): Set<IntPair>? { | ||
val visited = obstacles.toMutableSet() | ||
val queue = ArrayDeque<Node>() | ||
queue.add(Node(0, 0)) | ||
while (queue.isNotEmpty()) { | ||
val node = queue.removeFirst() | ||
val (x, y) = node | ||
if (x == size && y == size) return generateSequence(node) { it.next }.map { it.x to it.y }.toSet() | ||
if (!visited.add(x to y)) continue | ||
if (x > 0) queue.addLast(Node(x - 1, y, node)) | ||
if (y > 0) queue.addLast(Node(x, y - 1, node)) | ||
if (y < size) queue.addLast(Node(x, y + 1, node)) | ||
if (x < size) queue.addLast(Node(x + 1, y, node)) | ||
} | ||
return null | ||
} | ||
|
||
fun part1(limit: Int = 1024): Int? = findPath(coords.subList(0, limit))?.size?.minus(1) | ||
|
||
fun part2(): String? { | ||
var i = 0 | ||
while (i < coords.size) { | ||
val path = findPath(coords.subList(0, i + 1)) ?: return coords[i].let { (x, y) -> "$x,$y" } | ||
do i++ while (i < coords.size && coords[i] !in path) | ||
} | ||
return null | ||
} | ||
|
||
private data class Node(val x: Int, val y: Int, val next: Node? = null) | ||
} |
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
47 changes: 47 additions & 0 deletions
47
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day18Test.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 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day18Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(22, Day18(example, 6).part1(12)) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals("6,1", Day18(example, 6).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|5,4 | ||
|4,2 | ||
|4,5 | ||
|3,0 | ||
|2,1 | ||
|6,3 | ||
|2,4 | ||
|1,5 | ||
|0,6 | ||
|3,3 | ||
|2,6 | ||
|5,1 | ||
|1,2 | ||
|5,5 | ||
|2,5 | ||
|6,5 | ||
|1,4 | ||
|0,4 | ||
|6,4 | ||
|1,1 | ||
|6,1 | ||
|1,0 | ||
|0,5 | ||
|1,6 | ||
|2,0 | ||
|""".trimMargin() | ||
} | ||
} |