-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.kt
136 lines (112 loc) · 5.08 KB
/
Day22.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
private interface StringRepresentable {
val stringRepresentation: String
}
private inline fun <reified T> String.intoEnum(): T?
where T : StringRepresentable, T : Enum<T> =
T::class.java.enumConstants.find { it.stringRepresentation == this }
private enum class Cell(override val stringRepresentation: String) : StringRepresentable {
Close(" "),
Open("."),
Wall("#"),
}
private sealed interface PathEntity
private enum class Rotation(override val stringRepresentation: String) : StringRepresentable, PathEntity {
Clockwise("R"),
Counterclockwise("L"),
}
private data class Distance(val distance: Int) : PathEntity
private fun slalom(
slope: List<List<Cell>>,
pathEntities: List<PathEntity>,
wrappingRule: (Point, Direction) -> Pair<Point, Direction>,
): Int {
var currentPoint = Point(0, slope[0].indexOf(Cell.Open))
val facings = listOf(Direction.Up, Direction.Right, Direction.Down, Direction.Left)
var currentFacing = facings.first()
pathEntities.forEach { entity ->
when (entity) {
is Rotation -> {
val ordinalDifference = when (entity) {
Rotation.Clockwise -> 1
Rotation.Counterclockwise -> -1
}
currentFacing = facings.getModulo(currentFacing.ordinal + ordinalDifference)
}
is Distance -> repeat(entity.distance) {
val newPoint = currentPoint + currentFacing.point
when (slope.getOrNull(newPoint)) {
Cell.Wall -> {}
Cell.Open -> currentPoint = newPoint
null, Cell.Close -> wrappingRule(currentPoint, currentFacing).let {
if (slope[it.first] != Cell.Wall) {
currentPoint = it.first
currentFacing = it.second
}
}
}
}
}
}
return 1000 * (currentPoint.x + 1) + 4 * (currentPoint.y + 1) + currentFacing.ordinal
}
private fun part1(slope: List<List<Cell>>, pathEntities: List<PathEntity>): Int =
slalom(slope, pathEntities) { currentPoint, currentFacing ->
val localFacing = currentFacing.point * -1
val localPoint = currentPoint.copy()
while (slope.getOrNull(localPoint).let { it != null && it != Cell.Close }) {
localPoint += localFacing
}
(localPoint + currentFacing.point) to currentFacing
}
private const val SIDE = 50
private fun part2(slope: List<List<Cell>>, pathEntities: List<PathEntity>): Int {
fun Point.getQuadrant(): Int = (x / SIDE) * 3 + y / SIDE
fun Point.quadrantize(quadrant: Int) = Point(x + quadrant / 3 * SIDE, y + quadrant % 3 * SIDE)
fun Point.dequadrantize() = quadrantize(-getQuadrant())
// .12
// .4.
// 67.
// 9..
return slalom(slope, pathEntities) { currentPoint, currentFacing ->
val (x, y) = currentPoint.dequadrantize()
when (currentPoint.getQuadrant() to currentFacing) {
1 to Direction.Down -> Point(SIDE - 1 - x, 0).quadrantize(6) to Direction.Up
1 to Direction.Left -> Point(y, 0).quadrantize(9) to Direction.Up
2 to Direction.Up -> Point(SIDE - 1 - x, SIDE - 1).quadrantize(7) to Direction.Down
2 to Direction.Right -> Point(y, SIDE - 1).quadrantize(4) to Direction.Down
2 to Direction.Left -> Point(SIDE - 1, y).quadrantize(9) to Direction.Left
4 to Direction.Down -> Point(0, x).quadrantize(6) to Direction.Right
4 to Direction.Up -> Point(SIDE - 1, x).quadrantize(2) to Direction.Left
6 to Direction.Left -> Point(y, 0).quadrantize(4) to Direction.Up
6 to Direction.Down -> Point(SIDE - 1 - x, 0).quadrantize(1) to Direction.Up
7 to Direction.Up -> Point(SIDE - 1 - x, SIDE - 1).quadrantize(2) to Direction.Down
7 to Direction.Right -> Point(y, SIDE - 1).quadrantize(9) to Direction.Down
9 to Direction.Down -> Point(0, x).quadrantize(1) to Direction.Right
9 to Direction.Right -> Point(0, y).quadrantize(2) to Direction.Right
9 to Direction.Up -> Point(SIDE - 1, x).quadrantize(7) to Direction.Left
else -> expect()
}
}
}
fun main() {
val (slope, path) = mapBlocks { it }.let { (slopeBlock, pathBlock) ->
val slope = slopeBlock.map { line -> line.map { it.toString().intoEnum<Cell>()!! } }
val path = pathBlock.single().let {
val q = it.toCollection(ArrayDeque())
buildList {
while (q.isNotEmpty()) {
val current = buildString {
val number = q.first().isDigit()
while (q.isNotEmpty() && q.first().isDigit() == number) {
append(q.removeFirst())
}
}
add(current.toIntOrNull()?.let(::Distance) ?: current.intoEnum<Rotation>()!!)
}
}
}
slope to path
}
println(part1(slope, path))
println(part2(slope, path))
}