@@ -3,9 +3,55 @@ package adventofcode2023
3
3
import java.io.File
4
4
5
5
object Day24 {
6
- private var inputs = File (" resources/adventofcode2023/Day24.txt" ).readLines()
6
+ private val inputs = File (" resources/adventofcode2023/Day24.txt" )
7
+ .readLines()
8
+ .map { line ->
9
+ val (position, velocity) = line.split(" @ " )
10
+ val (posX, posY, posZ) = position.split(" , " ).map { it.toLong() }
11
+ val (velX, velY, velZ) = velocity.split(" , " ).map { it.toLong() }
12
+ Hailstone (Vector3 (posX, posY, posZ), Vector3 (velX, velY, velZ))
13
+ }
7
14
8
- fun part1 () = println ()
15
+ private data class Vector2 (val x : Double , val y : Double )
16
+
17
+ private data class Vector3 (val x : Long , val y : Long , val z : Long )
18
+
19
+ private data class Hailstone (val position : Vector3 , val velocity : Vector3 )
20
+
21
+ private fun intersection2d (first : Hailstone , second : Hailstone ): Vector2 ? {
22
+ val pos1 = first.position
23
+ val pos2 = second.position
24
+ val slope1 = first.velocity.y.toDouble() / first.velocity.x
25
+ val slope2 = second.velocity.y.toDouble() / second.velocity.x
26
+
27
+ if (slope1 == slope2) return null
28
+
29
+ val xInterjection = (pos2.y - pos1.y + slope1 * pos1.x - slope2 * pos2.x) / (slope1 - slope2)
30
+ val yInterjection = slope1 * (xInterjection - pos1.x) + pos1.y
31
+
32
+ return Vector2 (xInterjection, yInterjection)
33
+ }
34
+
35
+ private fun inputsPairPermutations (): List <Pair <Hailstone , Hailstone >> =
36
+ inputs.flatMapIndexed { i, element1 ->
37
+ inputs.subList(i + 1 , inputs.size).mapNotNull { element2 ->
38
+ element1.takeUnless { it == = element2 }?.let { it to element2 }
39
+ }
40
+ }
41
+
42
+ fun part1 (min : Long = 200000000000000, max : Long = 400000000000000) = println (
43
+ inputsPairPermutations().count {
44
+ val intersection = intersection2d(it.first, it.second)
45
+
46
+ if (intersection == null ) false
47
+ else {
48
+ val t1 = (intersection.x - it.first.position.x) / it.first.velocity.x
49
+ val t2 = (intersection.x - it.second.position.x) / it.second.velocity.x
50
+ if (t1 < 0 || t2 < 0 ) false
51
+ else intersection.x >= min && intersection.x <= max && intersection.y >= min && intersection.y <= max
52
+ }
53
+ }
54
+ )
9
55
}
10
56
11
57
fun main () {
0 commit comments