Skip to content

Commit

Permalink
2024 - Day 16 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 16, 2024
1 parent d340ddf commit 8d4fd32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
43 changes: 35 additions & 8 deletions src/main/kotlin/no/rodland/advent_2024/Day16.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
package no.rodland.advent_2024

import no.rodland.advent.Cave
import no.rodland.advent.Day
import no.rodland.advent.toCave
import no.rodland.advent.*
import java.util.*

// template generated: 15/12/2024
// Fredrik Rødland 2024
data class State(val pos: Pos, val dir: Direction, val cost: Int)

class Day16(val input: List<String>) : Day<Long, Long, Array<CharArray>> {
class Day16(val input: List<String>) : Day<Int, Int, Cave> {

private val maze = input.parse()
private val parsed = input.parse()
private val maze = parsed

override fun partOne(): Long {
return 2
override fun partOne(): Int {
val shortestPath = dijkstra(maze)
return shortestPath
}

override fun partTwo(): Long {
override fun partTwo(): Int {
return 2
}

private fun dijkstra(maze: Cave): Int {
val start = Pos(1, maze.size - 2) // Starting position (S)
val end = Pos(maze[0].size - 2, 1) // Ending position (E)

val pq = PriorityQueue<State>(compareBy { it.cost })
val visited = mutableSetOf<Pair<Pos, Direction>>() // Track visited states by Pos and Direction

pq.add(State(start, Direction.EAST, 0))

while (pq.isNotEmpty()) {
val current = pq.poll()
if (!visited.add(Pair(current.pos, current.dir))) continue
if (current.pos == end) return current.cost

val move = current.pos.next(current.dir)
if (move in maze && maze[move] != '#') {
pq.add(State(move, current.dir, current.cost + 1))
}
pq.add(State(current.pos, current.dir.left(), current.cost + 1000))
pq.add(State(current.pos, current.dir.right(), current.cost + 1000))
}
return -1 // If no path is found
}

override fun List<String>.parse(): Cave {
return this.toCave()
}

override val day = "16".toInt()
}

10 changes: 6 additions & 4 deletions src/test/kotlin/no/rodland/advent_2024/Day16Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal class Day16Test {
private val data16 = "2024/input_16.txt".readFile()
private val test16 = "2024/input_16_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultTwo = 2L
private val resultTestOne = 7036
private val resultTestTwo = 2
private val resultOne = 99460
private val resultTwo = 2

val test = defaultTestSuiteParseOnInit(
Day16(data16),
Expand All @@ -29,6 +29,8 @@ internal class Day16Test {
resultTwo,
{ Day16(data16) },
{ Day16(test16) },
numTestPart1 = 1,
numTestPart2 = 1,
)

@Nested
Expand Down

0 comments on commit 8d4fd32

Please sign in to comment.