Skip to content

Commit a4e761f

Browse files
authored
Merge pull request #31 from palinkiewicz/2023_d24
2023 Day 24 (Part 1 only)
2 parents 4479c48 + d931a5f commit a4e761f

File tree

3 files changed

+360
-0
lines changed

3 files changed

+360
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ I started taking part in the event in 2023 but may also consider doing tasks fro
3030
| [Day 21: Step Counter](https://adventofcode.com/2023/day/21) | [Solution (Part 1 only)](/adventofcode2023/Day21.kt) | [Inputs](/resources/adventofcode2023/Day21.txt) |
3131
| [Day 22: Sand Slabs](https://adventofcode.com/2023/day/22) | [Solution](/adventofcode2023/Day22.kt) | [Inputs](/resources/adventofcode2023/Day22.txt) |
3232
| [Day 23: A Long Walk](https://adventofcode.com/2023/day/23) | [Solution](/adventofcode2023/Day23.kt) | [Inputs](/resources/adventofcode2023/Day23.txt) |
33+
| [Day 24: Never Tell Me The Odds](https://adventofcode.com/2023/day/24) | [Solution(Part 1 only)](/adventofcode2023/Day24.kt) | [Inputs](/resources/adventofcode2023/Day24.txt) |

adventofcode2023/Day24.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package adventofcode2023
2+
3+
import java.io.File
4+
5+
object Day24 {
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+
}
14+
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+
)
55+
}
56+
57+
fun main() {
58+
Day24.part1()
59+
}

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