Skip to content

Commit 9aae386

Browse files
committed
y2023 d6
1 parent 9ccc265 commit 9aae386

File tree

5 files changed

+140
-50
lines changed

5 files changed

+140
-50
lines changed

poetry.lock

Lines changed: 44 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ python = ">=3.9, <4"
99
advent-of-code-data = "^2.0.1"
1010

1111
[tool.poetry.group.dev.dependencies]
12-
pytest = "^6.2.5"
12+
pytest = "^7"
1313
black = "^22.3.0"
1414
isort = "^5.10.1"
1515
flake8 = "^4.0.1"
@@ -18,6 +18,7 @@ poethepoet = "^0.11.0"
1818
types-setuptools = "^57.4.4"
1919
pyright = "^1.1.338"
2020
typer = "^0.9.0"
21+
pytest-subtests = "^0.11.0"
2122

2223
[tool.isort]
2324
profile = "black"

src/advent_of_code/y2023/d6.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from dataclasses import dataclass
2+
from math import ceil, floor, prod, sqrt
3+
4+
from advent_of_code.util import format_solution, puzzle_input
5+
6+
7+
@dataclass
8+
class Race:
9+
time: int
10+
record: int
11+
12+
def max_windup_time(self):
13+
return floor((self.time + sqrt(self.time**2 - 4 * (self.record + 1))) / 2)
14+
15+
def min_windup_time(self):
16+
return ceil((self.time - sqrt(self.time**2 - 4 * (self.record + 1))) / 2)
17+
18+
19+
def parse_input_multi(races_input: list[str]) -> list[Race]:
20+
times = [int(t) for t in races_input[0].split("Time: ")[1].split()]
21+
records = [int(d) for d in races_input[1].split("Distance: ")[1].split()]
22+
23+
result = []
24+
for time, record in zip(times, records):
25+
result.append(Race(time=time, record=record))
26+
27+
return result
28+
29+
30+
def parse_input_single(races_input: list[str]) -> Race:
31+
time = int("".join(t for t in races_input[0].split("Time: ")[1].split()))
32+
record = int("".join(d for d in races_input[1].split("Distance: ")[1].split()))
33+
34+
return Race(time=time, record=record)
35+
36+
37+
def part_1(races_input: list[str]) -> int:
38+
races = parse_input_multi(races_input)
39+
40+
return prod(1 + race.max_windup_time() - race.min_windup_time() for race in races)
41+
42+
43+
def part_2(races_input: list[str]) -> int:
44+
race = parse_input_single(races_input)
45+
46+
return 1 + race.max_windup_time() - race.min_windup_time()
47+
48+
49+
if __name__ == "__main__":
50+
puzzle = puzzle_input(2023, 6)
51+
52+
print(
53+
format_solution(
54+
solver_p1=lambda: part_1(puzzle),
55+
solver_p2=lambda: part_2(puzzle),
56+
)
57+
)

src/advent_of_code/y2023/data/day6

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 38 94 79 70
2+
Distance: 241 1549 1074 1091

tests/test_y2023/test_d6.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
from pytest_subtests import SubTests
3+
4+
from advent_of_code.y2023.d6 import Race, part_1, part_2
5+
6+
TEST_INPUT = [
7+
"Time: 7 15 30",
8+
"Distance: 9 40 200",
9+
]
10+
11+
12+
@pytest.mark.parametrize(
13+
"time, record, expected_min, expected_max",
14+
[(7, 9, 2, 5), (15, 40, 4, 11), (30, 200, 11, 19)],
15+
)
16+
def test_windup_times(
17+
time: int,
18+
record: int,
19+
expected_min: int,
20+
expected_max: int,
21+
subtests: SubTests,
22+
):
23+
race = Race(time=time, record=record)
24+
with subtests.test("Min"):
25+
assert race.min_windup_time() == expected_min
26+
with subtests.test("Max"):
27+
assert race.max_windup_time() == expected_max
28+
29+
30+
def test_part_1():
31+
assert part_1(TEST_INPUT) == 288
32+
33+
34+
def test_part_2():
35+
assert part_2(TEST_INPUT) == 71503

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