generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2024D14.kt
139 lines (117 loc) · 4.91 KB
/
Y2024D14.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 aockt.y2024
import aockt.util.parse
import aockt.util.spacial.Area
import aockt.util.spacial.Direction
import aockt.util.spacial.Point
import aockt.util.spacial.move
import io.github.jadarma.aockt.core.Solution
object Y2024D14 : Solution {
/** Info about the [position] and [velocity] of a restroom security robot. */
private data class Robot(val position: Point, val velocity: Point) {
/** Compute the robot state after moving for one second, teleporting in order to stay within a bounded [area]. */
fun moveAndTeleport(area: Area): Robot {
val nextX = position.x + velocity.x
val nextY = position.y + velocity.y
val x = when (nextX) {
in area.xRange -> nextX
in Int.MIN_VALUE..<area.xRange.first -> nextX + area.width
else -> nextX - area.width
}
val y = when (nextY) {
in area.yRange -> nextY
in Int.MIN_VALUE..<area.yRange.first -> nextY + area.height
else -> nextY - area.height
}
return copy(position = Point(x, y))
}
}
/** Simulate robot movements in one second increments. First element is the [initial] state. */
private fun simulate(initial: List<Robot>): Sequence<List<Robot>> = sequence {
val totalRobots = initial.size
val area = if (totalRobots < 100) Area(11, 7) else Area(101, 103)
val queue = ArrayDeque(initial)
while (true) {
yield(queue)
repeat(totalRobots) {
queue
.removeFirst()
.moveAndTeleport(area)
.let(queue::addLast)
}
}
}
/**
* **Bonus Tree Finder Heuristic!** - Slower but more generally correct.
* The Easter egg occurs when _"most of the robots"_ arrange themselves to an image.
* We use the same technique as in [Y2024D12] to calculate size of contiguous groups based on robot locations.
* The tree image consists of a tree, and a border, that do not touch each-other.
* Therefore, we have found it when the largest two distinct groups' size exceed half the total robots.
*/
@Suppress("UNUSED")
private fun checkForTree(robots: List<Robot>): Boolean {
if (robots.isEmpty()) return false
val byLocation = robots
.groupBy { it.position }
.mapValues { it.value.isNotEmpty() }
val groups = mutableListOf<Int>()
val seen = mutableSetOf<Point>()
for (robot in robots) {
var count = 0
val queue = ArrayDeque<Point>().apply { add(robot.position) }
while (queue.isNotEmpty()) {
val current = queue
.removeFirst()
.takeUnless { it in seen }
?.also(seen::add)
?.also { count++ }
?: continue
Direction.all
.asSequence()
.map(current::move)
.filter { it in byLocation }
.forEach(queue::add)
groups.add(count)
}
}
return groups.sortedDescending().take(2).sum() >= robots.size / 2
}
/** Parse the [input] and return the list of [Robot]s. */
private fun parseInput(input: String): List<Robot> = parse {
val regex = Regex("""^p=(\d+),(\d+) v=(-?\d+),(-?\d+)$""")
input
.lineSequence()
.map(regex::matchEntire)
.map { it!!.destructured }
.map { (px, py, vx, vy) ->
Robot(
position = Point(px.toInt(), py.toInt()),
velocity = Point(vx.toInt(), vy.toInt()),
)
}
.toList()
}
override fun partOne(input: String): Int {
val robots = parseInput(input)
val area = if (robots.size < 100) Area(11, 7) else Area(101, 103)
val halfX = area.width / 2
val halfY = area.height / 2
val quads = setOf(
Area(xRange = 0..<halfX, yRange = 0..<halfY),
Area(xRange = halfX + 1..<area.width, yRange = 0..<halfY),
Area(xRange = halfX + 1..<area.width, yRange = halfY + 1..<area.height),
Area(xRange = 0..<halfX, yRange = halfY + 1..<area.height),
)
return simulate(robots)
.drop(100).first()
.groupingBy { robot -> quads.firstOrNull { robot.position in it } }
.eachCount()
.filterKeys { it != null }
.values.reduce(Int::times)
}
override fun partTwo(input: String): Int =
parseInput(input)
.let(::simulate)
.take(100_000) // Sanity stop condition to prevent endless loop.
//.indexOfFirst(::checkForTree) // <- More thorough alternative for detecting.
.indexOfFirst { it.distinctBy(Robot::position).size == it.size }
}