Skip to content

Commit 57da1fe

Browse files
committed
Add 2023 Day 24 Part 1 solution
1 parent b3cb5a2 commit 57da1fe

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

adventofcode2023/Day24.kt

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,55 @@ package adventofcode2023
33
import java.io.File
44

55
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+
}
714

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+
)
955
}
1056

1157
fun main() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy