Skip to content

Commit

Permalink
Day 6: Wait For It
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 6, 2023
1 parent 20a78d2 commit 9270f62
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development occurs in language-specific directories:
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day3.kt)|[day3.py](py/aoc2023/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt)|[day4.py](py/aoc2023/day4.py)|[day4.rs](rs/src/day4.rs)|
|[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day5.kt)|[day5.py](py/aoc2023/day5.py)|[day5.rs](rs/src/day5.rs)|
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day6.kt)|||
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day6.kt)|[day6.py](py/aoc2023/day6.py)||
45 changes: 45 additions & 0 deletions py/aoc2023/day6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Day 6: Wait For It
"""

import re
from math import ceil, floor, prod, sqrt

SAMPLE_INPUT = """
Time: 7 15 30
Distance: 9 40 200
"""

NUMBER = re.compile(r"\d+")


def _wincount(time, distance):
b = time / 2
d = sqrt(b * b - distance)
return ceil(b + d - 1) - floor(b - d + 1) + 1


def part1(data):
"""
>>> part1(SAMPLE_INPUT)
288
"""
line1, line2 = filter(None, data.splitlines())
return prod(
_wincount(int(time.group()), int(distance.group()))
for time, distance in zip(NUMBER.finditer(line1), NUMBER.finditer(line2))
)


def part2(data):
"""
>>> part2(SAMPLE_INPUT)
71503
"""
line1, line2 = filter(None, data.splitlines())
return _wincount(
int("".join(NUMBER.findall(line1))), int("".join(NUMBER.findall(line2)))
)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ day2 = "aoc2023.day2:parts"
day3 = "aoc2023.day3:parts"
day4 = "aoc2023.day4:parts"
day5 = "aoc2023.day5:parts"
day6 = "aoc2023.day6:parts"

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

0 comments on commit 9270f62

Please sign in to comment.