generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.kt
198 lines (162 loc) · 4.73 KB
/
Day14.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package year2024.`14`
import readInput
private const val CURRENT_DAY = "14"
private data class Point(
val x: Int,
val y: Int,
) {
fun moveTop(): Point = copy(y = y - 1)
fun moveRight(): Point = copy(x = x + 1)
fun moveBottom(): Point = copy(y = y + 1)
fun moveLeft(): Point = copy(x = x - 1)
override fun toString(): String {
return "{$x,$y}"
}
}
private data class RobotInfo(
val position: Point,
val velocity: Point,
) {
override fun toString(): String = "[$position,$velocity]"
fun moveIn(
grid: Point,
): RobotInfo {
val buffNewX = (position.x + velocity.x) % grid.x
val newX = if (buffNewX < 0) buffNewX + grid.x else buffNewX
val buffNewY = (position.y + velocity.y) % grid.y
val newY = if (buffNewY < 0) buffNewY + grid.y else buffNewY
return copy(
position = Point(
x = newX,
y = newY,
),
)
}
}
// p=91,23 v=98,-65
private fun parseLineInto(
line: String,
): RobotInfo {
val items = line.split("p=", "v=")
.map { it.trim() }
.filter { it.contains(",") }
.map {
val numbers = it.split(",").map { it.toInt() }
Point(
numbers[0],
numbers[1],
)
}
return RobotInfo(
items[0],
items[1],
)
}
private fun List<RobotInfo>.printRobots(grid: Point) {
val roboMap = this.groupBy { it.position }
for (y in 0..grid.y - 1) {
for (x in 0..grid.x - 1) {
val count = roboMap[Point(x, y)]?.count()
if (count == null) {
print(".")
} else {
print("1")
}
}
println()
}
}
private fun walkUntilAllCategoriesAreNotFound(
map: Set<Point>,
initialPoint: Point,
): Set<Point> {
val visitedPoints = mutableSetOf<Point>()
val queue: ArrayDeque<Point> = ArrayDeque()
queue.add(initialPoint)
while (queue.isNotEmpty()) {
val currentItem = queue.removeFirst()
if (currentItem in visitedPoints) continue
visitedPoints.add(currentItem)
fun addIfNeeded(p: Point) {
if (p in map) {
queue.add(p)
}
}
val leftPoint = currentItem.moveLeft()
val rightPoint = currentItem.moveRight()
val topPoint = currentItem.moveTop()
val bottomPoint = currentItem.moveBottom()
addIfNeeded(leftPoint)
addIfNeeded(rightPoint)
addIfNeeded(topPoint)
addIfNeeded(bottomPoint)
}
return visitedPoints
}
private fun List<RobotInfo>.performMovementNTimes(n: Int, grid: Point): List<RobotInfo> {
var currentRobots = this
repeat(n) {
currentRobots = currentRobots.map { it.moveIn(grid) }
val set = currentRobots.map { it.position }.toSet()
val itemsWith10 = currentRobots.map { currRobot ->
walkUntilAllCategoriesAreNotFound(
set,
currRobot.position,
)
}.filter { it.size > 50 }
if (itemsWith10.isNotEmpty()) {
println("After ${it + 1} second:")
currentRobots.printRobots(grid)
println()
}
}
return currentRobots
}
private fun List<RobotInfo>.calculateValue(grid: Point): Int {
var q1 = 0
var q2 = 0
var q3 = 0
var q4 = 0
val middleX = grid.x / 2
val middleY = grid.y / 2
map { it.position }.forEach {
when {
it.x < middleX && it.y < middleY -> q1++
it.x > middleX && it.y < middleY -> q2++
it.x < middleX && it.y > middleY -> q3++
it.x > middleX && it.y > middleY -> q4++
}
}
return q1 * q2 * q3 * q4
}
fun main() {
fun part1(input: List<String>, grid: Point): Int {
val robots = input.map {
parseLineInto(it)
}
val result = robots.performMovementNTimes(100, grid)
println(robots)
return result.calculateValue(grid)
}
fun part2(input: List<String>, grid: Point): Int {
val robots = input.map {
parseLineInto(it)
}
val result = robots.performMovementNTimes(10000, grid)
println(robots)
return result.calculateValue(grid)
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput, Point(11, 7))
println(part1Test)
check(part1Test == 12)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input, Point(101, 103))
println(part1)
check(part1 == 221655456)
// Part 2
val part2 = part2(input, Point(101, 103))
println(part2)
}