diff --git a/README.md b/README.md index 63f36eb6..dbe93b38 100644 --- a/README.md +++ b/README.md @@ -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)|| diff --git a/py/aoc2023/day6.py b/py/aoc2023/day6.py new file mode 100644 index 00000000..000badba --- /dev/null +++ b/py/aoc2023/day6.py @@ -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) diff --git a/py/pyproject.toml b/py/pyproject.toml index d439af8f..04b1ef3b 100644 --- a/py/pyproject.toml +++ b/py/pyproject.toml @@ -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"]