Skip to content

Commit 1b654b9

Browse files
committed
Day 18 with 'good enough' DFS
1 parent 7fcbf14 commit 1b654b9

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

aoc-secret

src/main/kotlin/com/cluddles/aoc/y2024/All2024.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fun main() {
2323
Day15,
2424
Day16,
2525
Day17,
26+
Day18,
2627
)
2728

2829
measureTime {

src/main/kotlin/com/cluddles/aoc/y2024/Day16.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object Day16: Solver<Grid<Char>, Int> {
2121
return CharGrid(src.lines().toList())
2222
}
2323

24-
/** Populate all cell [costs] with a simple flood fill */
24+
/** Populate all cell [costs] with a simple depth first search (flood fill) */
2525
private fun populateCosts(costs: MutableGrid<Int>, input: Grid<Char>, x: Int, y: Int, dir: Dir4, cost: Int) {
2626
val bestCost = costs[x, y]
2727
if (bestCost >= 0 && bestCost < cost) return
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.cluddles.aoc.y2024
2+
3+
import com.cluddles.aoc.core.Harness
4+
import com.cluddles.aoc.core.Solver
5+
import com.cluddles.aoc.core.SolverInput
6+
import com.cluddles.aoc.util.Dir4
7+
import com.cluddles.aoc.util.Grid
8+
import com.cluddles.aoc.util.IntGrid
9+
import com.cluddles.aoc.util.MutableGrid
10+
11+
/** RAM Run */
12+
object Day18: Solver<Grid<Int>, String> {
13+
14+
override fun prepareInput(src: SolverInput): Grid<Int> {
15+
return prepareInput(src, 71, 71)
16+
}
17+
18+
fun prepareInput(src: SolverInput, width: Int, height: Int): Grid<Int> {
19+
val result = IntGrid(width, height, Int.MAX_VALUE)
20+
for (l in src.lines().withIndex()) {
21+
val (x, y) = l.value.split(",").map { it.toInt() }
22+
result[x, y] = l.index
23+
}
24+
return result
25+
}
26+
27+
// Quick and dirty DFS
28+
private fun populateCosts(input: Grid<Int>, costs: MutableGrid<Int>, x: Int, y: Int, cost: Int, bytes: Int) {
29+
costs[x, y] = cost
30+
val newCost = cost + 1
31+
for (d in Dir4.entries) {
32+
val x = x + d.x
33+
val y = y + d.y
34+
if (input.getIfInBounds(x, y) { 0 } >= bytes && newCost < costs[x, y]) {
35+
populateCosts(input, costs, x, y, newCost, bytes)
36+
}
37+
}
38+
}
39+
40+
fun solvePart1(input: Grid<Int>, bytes: Int): Int {
41+
val costs = IntGrid(input.width, input.height, Int.MAX_VALUE)
42+
populateCosts(input, costs, 0, 0, 0, bytes)
43+
return costs[input.width-1, input.height-1]
44+
}
45+
46+
override fun solvePart1(input: Grid<Int>): String {
47+
return solvePart1(input, 1024).toString()
48+
}
49+
50+
override fun solvePart2(input: Grid<Int>): String {
51+
// It should be brute forceable
52+
// It's slow, but probably less so than me implementing a quicker algo
53+
// I'll make nice later
54+
for (i in 1..input.max()) {
55+
val result = solvePart1(input, i)
56+
if (result == Int.MAX_VALUE) {
57+
return with (input.iterableWithPos().first { it.data == i-1 }) { "$x,$y" }
58+
}
59+
}
60+
error("No solution")
61+
}
62+
63+
}
64+
65+
fun main() {
66+
Harness.run(Day18)
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.cluddles.aoc.y2024
2+
3+
import com.cluddles.aoc.core.SolverInput
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
class Day18Test {
8+
9+
val solver = Day18
10+
11+
val input = solver.prepareInput(SolverInput.fromPath(solver.examplePath), 7, 7)
12+
13+
@Test fun part1() {
14+
assertThat(solver.solvePart1(input, 12)).isEqualTo(22)
15+
}
16+
17+
@Test fun part2() {
18+
assertThat(solver.solvePart2(input)).isEqualTo("6,1")
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
5,4
2+
4,2
3+
4,5
4+
3,0
5+
2,1
6+
6,3
7+
2,4
8+
1,5
9+
0,6
10+
3,3
11+
2,6
12+
5,1
13+
1,2
14+
5,5
15+
2,5
16+
6,5
17+
1,4
18+
0,4
19+
6,4
20+
1,1
21+
6,1
22+
1,0
23+
0,5
24+
1,6
25+
2,0

0 commit comments

Comments
 (0)