Skip to content

Commit d1c0988

Browse files
committed
aoc-2024 - Day14 - part 1
1 parent 5764e69 commit d1c0988

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

advent-of-code-2024/src/Day14.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import util.Point
2+
import util.Vector
3+
import kotlin.math.abs
4+
5+
// https://adventofcode.com/2024/day/14
6+
7+
fun main() {
8+
9+
data class Robot(
10+
val position: Point,
11+
val vector: Vector
12+
)
13+
14+
val regex = "p=(.*),(.*) v=(.*),(.*)".toRegex()
15+
16+
fun List<String>.robots() = this.map {
17+
val (px, py, vx, vy) = regex.find(it)!!.destructured
18+
Robot(
19+
position = Point(
20+
x = px.toInt(),
21+
y = py.toInt(),
22+
),
23+
vector = Vector(
24+
x = vx.toInt(),
25+
y = vy.toInt(),
26+
),
27+
)
28+
}
29+
30+
fun part1(input: List<String>, width: Int, height: Int): Int {
31+
32+
val maxX = width
33+
val maxY = height
34+
35+
var q1 = 0
36+
var q2 = 0
37+
var q3 = 0
38+
var q4 = 0
39+
40+
input.robots()
41+
.forEach { robot: Robot ->
42+
var pos = robot.position
43+
println(pos)
44+
repeat(100) {
45+
pos += robot.vector
46+
pos = Point((pos.x + maxX) % maxX, (pos.y + maxY) % maxY)
47+
println(pos)
48+
}
49+
when {
50+
pos.x < (maxX / 2) && pos.y < (maxY / 2) -> q1++
51+
pos.x > (maxX / 2) && pos.y < (maxY / 2) -> q2++
52+
pos.x < (maxX / 2) && pos.y > (maxY / 2) -> q3++
53+
pos.x > (maxX / 2) && pos.y > (maxY / 2) -> q4++
54+
}
55+
}
56+
57+
println("$q1 $q2 $q3 $q4")
58+
return q1 * q2 * q3 * q4
59+
}
60+
61+
fun part2(input: List<String>): Int {
62+
return TODO()
63+
}
64+
65+
val testInput = readInput("Day14_test")
66+
val input = readInput("Day14")
67+
68+
test(12) { part1(testInput, 11, 7) }
69+
exec { part1(input, 101, 103) }
70+
test(TODO()) { part2(testInput) }
71+
exec { part2(input) }
72+
}

0 commit comments

Comments
 (0)