Skip to content

Commit

Permalink
Merge pull request #120 from ephemient/kt/day18
Browse files Browse the repository at this point in the history
Day 18: RAM Run
  • Loading branch information
ephemient authored Dec 18, 2024
2 parents 27e6596 + 709e803 commit 740436c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Development occurs in language-specific directories:
|[Day15.hs](hs/src/Day15.hs)|[Day15.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day15.kt)|[day15.py](py/aoc2024/day15.py)|[day15.rs](rs/src/day15.rs)|
|[Day16.hs](hs/src/Day16.hs)|[Day16.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day16.kt)|[day16.py](py/aoc2024/day16.py)|[day16.rs](rs/src/day16.rs)|
|[Day17.hs](hs/src/Day17.hs)|[Day17.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day17.kt)|[day17.py](py/aoc2024/day17.py)|[day17.rs](rs/src/day17.rs)|
|[Day18.hs](hs/src/Day18.hs)||||
|[Day18.hs](hs/src/Day18.hs)|[Day18.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day18.kt)|||
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ class Day11Bench {
bh.consume(day11.part1())
bh.consume(day11.part2())
}
}
}
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())
}
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ val days: List<Day> = listOf(
Day(15, ::Day15, Day15::part1, Day15::part2),
Day(16, ::Day16, Day16::part1, Day16::part2),
Day(17, ::Day17, Day17::part1, Day17::part2),
Day(18, ::Day18, Day18::part1, Day18::part2),
)

data class Day(
Expand Down
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()
}
}

0 comments on commit 740436c

Please sign in to comment.