Skip to content

Commit 28aa9ee

Browse files
jameslawlorJames
andauthored
Day 6 part 1 (#9)
* day 6 part 1 solution * update readme --------- Co-authored-by: James <james@Susans-MacBook-Air.local>
1 parent 5aa6533 commit 28aa9ee

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ docker run aoc-2023-app
1616
| 03 | ⭐️ | ⭐️ |
1717
| 04 | ⭐️ | ⭐️ |
1818
| 05 | todo | todo |
19+
| 06 | ⭐️ | todo |

src/aoc_2023/days/6.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from aoc_2023.solvers.day_6_solvers import (
2+
solve_day_6,
3+
)
4+
from aoc_2023.utils.input_handling import read_input, parse_args
5+
6+
7+
def main():
8+
args = parse_args()
9+
input = read_input(args.input_file)
10+
result_part_1 = solve_day_6(input)
11+
print(f"Day 6: "
12+
f" Result for part 1 is {result_part_1}. "
13+
)
14+
15+
16+
if __name__ == "__main__":
17+
main()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import math
2+
import re
3+
4+
class Race:
5+
def __init__(self, time, distance_to_beat):
6+
self.time = int(time)
7+
self.distance_to_beat = int(distance_to_beat)
8+
self._compute_ways_to_win()
9+
10+
def __str__(self):
11+
return (
12+
f"time: {self.time} | "
13+
f"distance_to_beat: {self.distance_to_beat} | "
14+
)
15+
16+
def _compute_ways_to_win(self):
17+
possible_speeds = range(self.time+1)
18+
self.ways_to_win = 0
19+
for possible_speed in possible_speeds:
20+
time_remaining = self.time - possible_speed
21+
distance_travelled = possible_speed*time_remaining
22+
if distance_travelled > self.distance_to_beat:
23+
self.ways_to_win+=1
24+
25+
class Races:
26+
def __init__(self, *races):
27+
self.races = list(races)
28+
29+
def __str__(self):
30+
return ("".join([str(race) for race in self.races]))
31+
32+
def n_races(self):
33+
return len(self.races)
34+
35+
def add_race(self, race):
36+
self.races.append(race)
37+
38+
def solve(self):
39+
return math.prod([race.ways_to_win for race in self.races])
40+
41+
42+
def create_races(input):
43+
44+
races = Races()
45+
46+
times = re.findall(r"(\d+)", input[0])
47+
distances = re.findall(r"(\d+)", input[1])
48+
49+
for time, distance in zip(times,distances):
50+
race = Race(time=time,distance_to_beat=distance)
51+
races.add_race(race)
52+
53+
return races
54+
55+
56+
def solve_day_6(input):
57+
races = create_races(input)
58+
return races.solve()

tests/test_day_6_solvers.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pytest
2+
import math
3+
from aoc_2023.solvers.day_6_solvers import (
4+
Race,
5+
Races,
6+
create_races,
7+
solve_day_6,
8+
)
9+
10+
@pytest.fixture
11+
def day_6_test_input():
12+
return [
13+
"Time: 7 15 30",
14+
"Distance: 9 40 200",
15+
]
16+
17+
@pytest.fixture
18+
def sample_race():
19+
return Race(time=7, distance_to_beat=9)
20+
21+
22+
@pytest.fixture
23+
def sample_races():
24+
return Races(
25+
Race(time=7, distance_to_beat=9),
26+
Race(time=15, distance_to_beat=40),
27+
Race(time=30, distance_to_beat=200),
28+
)
29+
30+
31+
def test_race_initialization(sample_race):
32+
assert sample_race.time == 7
33+
assert sample_race.distance_to_beat == 9
34+
assert sample_race.ways_to_win == 4
35+
36+
def test_race_compute_ways_to_win(sample_race):
37+
sample_race._compute_ways_to_win()
38+
assert sample_race.ways_to_win == 4
39+
40+
def test_races_initialisation():
41+
races = Races()
42+
for race in races.races:
43+
print(race)
44+
assert races.races == []
45+
46+
def test_races_add_race(sample_race):
47+
races = Races()
48+
races.add_race(sample_race)
49+
assert races.races == [sample_race]
50+
51+
def test_races_solve(sample_races):
52+
assert sample_races.solve() == math.prod([4,8,9])
53+
54+
55+
def test_create_races(day_6_test_input, sample_races):
56+
test_races = create_races(day_6_test_input)
57+
assert test_races.n_races() == 3
58+
for race, expected_race in zip(test_races.races,sample_races.races):
59+
assert race.time == expected_race.time
60+
assert race.distance_to_beat == expected_race.distance_to_beat
61+
assert race.ways_to_win == expected_race.ways_to_win
62+
63+
def test_solve_day_6(day_6_test_input):
64+
assert solve_day_6(day_6_test_input) == 288

0 commit comments

Comments
 (0)