-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.kt
139 lines (130 loc) · 4.19 KB
/
Day16.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package Year2024
import Day
import Direction
import Point
import Position
import charGrid
import firstPosition
import get
import moveTo
import point
import turnLeft
import turnRight
import java.util.PriorityQueue
private object Day16 : Day {
private fun Position.next(): List<Pair<Position, Int>> {
val (point, direction) = this
return listOf(
(point to direction.turnLeft()) to 1000,
((point moveTo direction) to direction) to 1,
(point to direction.turnRight()) to 1000,
)
}
override fun part1(input: List<String>): Any {
val (map) = input.charGrid()
val start = map.firstPosition { it == 'S' }
val end = map.firstPosition { it == 'E' }
val toVisit = PriorityQueue(compareBy(Pair<Position, Int>::second))
.apply { add((start to Direction.Right) to 0) }
val visited = mutableSetOf<Position>()
val scores = mutableMapOf<Position, Int>()
while (toVisit.isNotEmpty()) {
val (position, score) = toVisit.poll()
if (position.first == end) {
return score
} else if (visited.contains(position)) {
continue
} else {
visited.add(position)
position.next()
.filter { (p) -> p !in visited }
.filter { (p) -> map[p.point] != '#' }
.forEach { (next, cost) ->
val nextScore = score + cost
if (nextScore < scores.getOrDefault(next, Int.MAX_VALUE)) {
scores[next] = nextScore
toVisit.add(next to nextScore)
}
}
}
}
error("No path found")
}
override fun part2(input: List<String>): Any {
val (map) = input.charGrid()
val start = map.firstPosition { it == 'S' }
val end = map.firstPosition { it == 'E' }
val toVisit = PriorityQueue(compareBy(Pair<List<Position>, Int>::second))
.apply { add(listOf(start to Direction.Right) to 0) }
val visited = mutableSetOf<Position>()
val scores = mutableMapOf<Position, Int>()
val result = mutableSetOf<Point>()
var min = Int.MAX_VALUE
while (toVisit.isNotEmpty()) {
val (path, score) = toVisit.poll()
val position = path.last()
if (position.point == end) {
if (score <= min) {
min = score
result.addAll(path.map(Position::point))
}
} else {
position.next()
.filter { (p) -> p !in visited }
.filter { (p) -> map[p.point] != '#' }
.forEach { (next, cost) ->
val nextScore = score + cost
if (nextScore <= scores.getOrDefault(next, Int.MAX_VALUE)) {
scores[next] = nextScore
toVisit.add(path + next to nextScore)
}
}
}
}
return result.size
}
}
fun main() = with(Day16) {
test1(
input = """
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
""".trimIndent(),
expected = 7036,
)
result1()
test2(
input = """
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
""".trimIndent(),
expected = 45,
)
result2()
}