-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day17Simplified.kt
78 lines (62 loc) · 2.37 KB
/
Day17Simplified.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package io.dmitrijs.aoc2023
import io.dmitrijs.aoc2023.Direction.DOWN
import io.dmitrijs.aoc2023.Direction.LEFT
import io.dmitrijs.aoc2023.Direction.RIGHT
import io.dmitrijs.aoc2023.Direction.UP
import java.util.PriorityQueue
class Day17Simplified(private val input: List<String>) {
private val maxY = input.size
private val maxX = input.first().length
private val finish = Point(maxX - 1, maxY - 1)
fun puzzle1() = solve(minRepeat = 1, maxRepeat = 3)
fun puzzle2() = solve(minRepeat = 4, maxRepeat = 10)
private fun solve(minRepeat: Int, maxRepeat: Int): Int {
val queue = PriorityQueue<Block>(compareBy { it.distance }).apply {
add(Block(Point(1, 0), RIGHT, 1, 0))
add(Block(Point(0, 1), DOWN, 1, 0))
}
val visits = hashMapOf<Triple<Point, Direction, Int>, Int>()
while (queue.isNotEmpty()) {
val (node, direction, repeat, distance) = queue.poll()
val newDistance = distance + node.weight
if (node == finish && repeat >= minRepeat) {
return newDistance
}
val visit = Triple(node, direction, repeat)
if (newDistance >= visits.getOrDefault(visit, Int.MAX_VALUE)) {
continue
}
visits[visit] = newDistance
if (repeat >= minRepeat) {
val l = direction.toLeft()
val r = direction.toRight()
(node + l).takeIf { it.valid }?.let { queue.add(Block(it, l, 1, newDistance)) }
(node + r).takeIf { it.valid }?.let { queue.add(Block(it, r, 1, newDistance)) }
}
if (repeat < maxRepeat) {
(node + direction).takeIf { it.valid }?.let { queue.add(Block(it, direction, repeat + 1, newDistance)) }
}
}
return -1
}
private val Point.weight get() = input[y][x].digitToInt()
private val Point.valid get() = x in 0..<maxX && y in 0..<maxY
private fun Direction.toRight() = when (this) {
UP -> RIGHT
RIGHT -> DOWN
DOWN -> LEFT
LEFT -> UP
}
private fun Direction.toLeft() = when (this) {
UP -> LEFT
LEFT -> DOWN
DOWN -> RIGHT
RIGHT -> UP
}
private data class Block(
val point: Point,
val direction: Direction,
val repeat: Int,
val distance: Int,
)
}