Skip to content

Commit

Permalink
Day 24: Never Tell Me The Odds (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 25, 2023
1 parent 840fbfc commit d645279
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Development occurs in language-specific directories:
|[Day21.hs](hs/src/Day21.hs)|[Day21.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day21.kt)|[day21.py](py/aoc2023/day21.py)|[day21.rs](rs/src/day21.rs)|
|[Day22.hs](hs/src/Day22.hs)|[Day22.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day22.kt)|[day22.py](py/aoc2023/day22.py)|[day22.rs](rs/src/day22.rs)|
||[Day23.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day23.kt)|||
|[Day24.hs](hs/src/Day24.hs) ½||[day24.py](py/aoc2023/day24.py) ½||
|[Day24.hs](hs/src/Day24.hs) ½|[Day24.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day24.kt) ½|[day24.py](py/aoc2023/day24.py) ½||
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.ephemient.aoc2023.exe

import com.github.ephemient.aoc2023.Day24
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State

@State(Scope.Benchmark)
class Day24Bench {
private lateinit var input: String

@Setup
fun setup() {
input = getDayInput(24)
}

@Benchmark
fun part1() = Day24(input).part1()

@Benchmark
fun part2() = Day24(input).part2()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.ephemient.aoc2023

class Day24(input: String) {
private val hailstones = input.lines().mapNotNull { line ->
val (pos, vel) = line.split('@', limit = 2).takeIf { it.size == 2 } ?: return@mapNotNull null
val (x, y, z) = pos.split(',', limit = 3).takeIf { it.size == 3 } ?: return@mapNotNull null
val (vx, vy, vz) = vel.split(',', limit = 3).takeIf { it.size == 3 } ?: return@mapNotNull null
Hailstone(
x = x.trim().toLongOrNull() ?: return@mapNotNull null,
y = y.trim().toLongOrNull() ?: return@mapNotNull null,
z = z.trim().toLongOrNull() ?: return@mapNotNull null,
vx = vx.trim().toLongOrNull() ?: return@mapNotNull null,
vy = vy.trim().toLongOrNull() ?: return@mapNotNull null,
vz = vz.trim().toLongOrNull() ?: return@mapNotNull null,
)
}

fun part1(lo: Double = 200000000000000.0, hi: Double = 400000000000000.0): Int {
val lines = mutableListOf<Line>()
return hailstones.sumOf {
val m0 = it.vy / it.vx.toDouble()
val b0 = it.y - m0 * it.x
val range0 = if (it.vx < 0) {
Double.NEGATIVE_INFINITY..it.x.toDouble()
} else {
it.x.toDouble()..Double.POSITIVE_INFINITY
}
lines.count { (m1, b1, range1) ->
if (m0 == m1) return@count false
val x = (b0 - b1) / (m1 - m0)
x in lo..hi && m0 * x + b0 in lo..hi && x in range0 && x in range1
}.also { lines.add(Line(m0, b0, range0)) }
}
}

fun part2(): Long {
TODO()
}

private data class Hailstone(val x: Long, val y: Long, val z: Long, val vx: Long, val vy: Long, val vz: Long)

private data class Line(val m: Double, val b: Double, val range: ClosedFloatingPointRange<Double>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ val days: List<Day> = listOf(
Day(21, ::Day21, Day21::part1, Day21::part2),
Day(22, ::Day22, Day22::part1, Day22::part2),
Day(23, ::Day23, Day23::part1, Day23::part2, skipByDefault = !isJvm),
Day(24, ::Day24, Day24::part1),
)

data class Day(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.ephemient.aoc2023

import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

class Day24Test {
@Test
fun part1() {
assertEquals(2, Day24(example).part1(lo = 7.0, hi = 27.0))
}

@Test
@Ignore
fun part2() {
assertEquals(47, Day24(example).part2())
}

companion object {
private val example =
"""
|19, 13, 30 @ -2, 1, -2
|18, 19, 22 @ -1, -1, -2
|20, 25, 34 @ -2, -2, -4
|12, 31, 28 @ -1, -2, -1
|20, 19, 15 @ 1, -5, -3
|""".trimMargin()
}
}

0 comments on commit d645279

Please sign in to comment.