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 24, 2023
1 parent 923bf88 commit 5cffd9a
Show file tree
Hide file tree
Showing 3 changed files with 62 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.hs](hs/src/Day24.hs) ½||[day24.py](py/aoc2023/day24.py) ½||
60 changes: 60 additions & 0 deletions py/aoc2023/day24.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Day 24: Never Tell Me The Odds
"""

SAMPLE_INPUT = """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
"""


def _parse(data):
for line in data.splitlines():
if not line:
continue
pos, vel = line.split("@")
x, y, z = (int(num.strip()) for num in pos.split(","))
vx, vy, vz = (int(num.strip()) for num in vel.split(","))
yield (x, y, z), (vx, vy, vz)


# pylint: disable=too-many-locals
def part1(data, lo=200000000000000, hi=400000000000000):
"""
>>> part1(SAMPLE_INPUT, lo=7, hi=27)
2
"""
lines, acc = [], 0
for point in _parse(data):
(x0, y0, _), (vx0, vy0, _) = point
assert vx0
m0 = vy0 / vx0
b0 = y0 - x0 * vy0 / vx0
for m1, b1, x1, vx1 in lines:
if m0 == m1:
assert b0 != b1
continue
x = (b0 - b1) / (m1 - m0)
if (
lo <= x <= hi
and (x - x0) * vx0 > 0
and (x - x1) * vx1 > 0
and lo <= m0 * x + b0 <= hi
):
acc += 1
lines.append((m0, b0, x0, vx0))
return acc


def part2(data):
"""
>>> part2(SAMPLE_INPUT) # doctest: +SKIP
47
"""
_parse(data)
raise NotImplementedError()


parts = (part1,)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ day19 = "aoc2023.day19:parts"
day20 = "aoc2023.day20:parts"
day21 = "aoc2023.day21:parts"
day22 = "aoc2023.day22:parts"
day24 = "aoc2023.day24:parts"

[tool.black]
target_version = ["py312"]
Expand Down

0 comments on commit 5cffd9a

Please sign in to comment.