Skip to content

Commit 44e287e

Browse files
jameslawlorJames
andauthored
d6pt2 working (#10)
Co-authored-by: James <james@Susans-MacBook-Air.local>
1 parent 28aa9ee commit 44e287e

File tree

8 files changed

+68
-37
lines changed

8 files changed

+68
-37
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ solutions:
1414
python3 src/aoc_2023/days/1.py --input_file inputs/1.txt --part 2
1515
python3 src/aoc_2023/days/2.py --input_file inputs/2.txt
1616
python3 src/aoc_2023/days/3.py --input_file inputs/3.txt
17-
python3 src/aoc_2023/days/4.py --input_file inputs/4.txt
17+
python3 src/aoc_2023/days/4.py --input_file inputs/4.txt
18+
python3 src/aoc_2023/days/6.py --input_file inputs/6.txt

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ docker run aoc-2023-app
1616
| 03 | ⭐️ | ⭐️ |
1717
| 04 | ⭐️ | ⭐️ |
1818
| 05 | todo | todo |
19-
| 06 | ⭐️ | todo |
19+
| 06 | ⭐️ | ⭐️ |
20+
| 07 | todo | todo |

src/aoc_2023/days/4.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ def main():
88
args = parse_args()
99
input = read_input(args.input_file)
1010
result_part_1, result_part_2 = solve_day_4(input)
11-
print(f"Day 4: "
12-
f" Total points for part 1 is {result_part_1}. "
13-
f" Total points for part 2 is {result_part_2}. ")
11+
print(
12+
f"Day 4: "
13+
f" Total points for part 1 is {result_part_1}. "
14+
f" Total points for part 2 is {result_part_2}. "
15+
)
1416

1517

1618
if __name__ == "__main__":

src/aoc_2023/days/6.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
def main():
88
args = parse_args()
99
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-
)
10+
result_part_1, result_part_2 = solve_day_6(input)
11+
print(
12+
f"Day 6: "
13+
f" Result for part 1 is {result_part_1}. "
14+
f" Result for part 2 is {result_part_2}. "
15+
)
1416

1517

1618
if __name__ == "__main__":

src/aoc_2023/solvers/day_4_solvers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def compute_copies(cards):
6464
n_matches_on_card = card.n_matches
6565
n_copies_of_card = card.n_copies
6666
copies_to_generate = n_copies_of_card
67-
for next_card in cards[card_number+1 : card_number + n_matches_on_card + 1]:
67+
for next_card in cards[card_number + 1 : card_number + n_matches_on_card + 1]:
6868
next_card.n_copies += copies_to_generate
6969
return cards
7070

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
import math
22
import re
33

4+
45
class Race:
56
def __init__(self, time, distance_to_beat):
67
self.time = int(time)
78
self.distance_to_beat = int(distance_to_beat)
89
self._compute_ways_to_win()
910

1011
def __str__(self):
11-
return (
12-
f"time: {self.time} | "
13-
f"distance_to_beat: {self.distance_to_beat} | "
14-
)
15-
12+
return f"time: {self.time} | " f"distance_to_beat: {self.distance_to_beat} | "
13+
1614
def _compute_ways_to_win(self):
17-
possible_speeds = range(self.time+1)
15+
possible_speeds = range(self.time + 1)
1816
self.ways_to_win = 0
1917
for possible_speed in possible_speeds:
2018
time_remaining = self.time - possible_speed
21-
distance_travelled = possible_speed*time_remaining
19+
distance_travelled = possible_speed * time_remaining
2220
if distance_travelled > self.distance_to_beat:
23-
self.ways_to_win+=1
24-
21+
self.ways_to_win += 1
22+
23+
2524
class Races:
2625
def __init__(self, *races):
2726
self.races = list(races)
2827

2928
def __str__(self):
30-
return ("".join([str(race) for race in self.races]))
31-
29+
return "".join([str(race) for race in self.races])
30+
3231
def n_races(self):
3332
return len(self.races)
3433

3534
def add_race(self, race):
3635
self.races.append(race)
37-
36+
3837
def solve(self):
3938
return math.prod([race.ways_to_win for race in self.races])
4039

4140

42-
def create_races(input):
43-
41+
def create_races(input, part=1):
4442
races = Races()
4543

4644
times = re.findall(r"(\d+)", input[0])
4745
distances = re.findall(r"(\d+)", input[1])
4846

49-
for time, distance in zip(times,distances):
50-
race = Race(time=time,distance_to_beat=distance)
47+
if part == 2:
48+
times = ["".join(times)]
49+
distances = ["".join(distances)]
50+
51+
for time, distance in zip(times, distances):
52+
race = Race(time=time, distance_to_beat=distance)
5153
races.add_race(race)
52-
54+
5355
return races
5456

5557

5658
def solve_day_6(input):
57-
races = create_races(input)
58-
return races.solve()
59+
races_part_1 = create_races(input, part=1)
60+
races_part_2 = create_races(input, part=2)
61+
return (races_part_1.solve(), races_part_2.solve())

tests/test_day_4_solvers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def test_solve_day_4(
4848
sum(day_4_expected_final_cards_count),
4949
)
5050

51-
def test_compute_copies(day_4_test_input,day_4_expected_final_cards_count):
51+
52+
def test_compute_copies(day_4_test_input, day_4_expected_final_cards_count):
5253
test_cards = create_cards(day_4_test_input)
5354
compute_total_score(test_cards)
5455
test_cards_with_copies = compute_copies(test_cards)
5556
test_n_scratchcards = [card.n_copies for card in test_cards_with_copies]
56-
assert test_n_scratchcards == day_4_expected_final_cards_count
57+
assert test_n_scratchcards == day_4_expected_final_cards_count

tests/test_day_6_solvers.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,79 @@
77
solve_day_6,
88
)
99

10+
1011
@pytest.fixture
1112
def day_6_test_input():
1213
return [
1314
"Time: 7 15 30",
1415
"Distance: 9 40 200",
1516
]
1617

18+
1719
@pytest.fixture
1820
def sample_race():
1921
return Race(time=7, distance_to_beat=9)
2022

2123

24+
@pytest.fixture
25+
def sample_race_part_2():
26+
return Race(time=71530, distance_to_beat=940200)
27+
28+
2229
@pytest.fixture
2330
def sample_races():
2431
return Races(
2532
Race(time=7, distance_to_beat=9),
2633
Race(time=15, distance_to_beat=40),
2734
Race(time=30, distance_to_beat=200),
2835
)
29-
36+
3037

3138
def test_race_initialization(sample_race):
3239
assert sample_race.time == 7
3340
assert sample_race.distance_to_beat == 9
3441
assert sample_race.ways_to_win == 4
3542

43+
3644
def test_race_compute_ways_to_win(sample_race):
3745
sample_race._compute_ways_to_win()
3846
assert sample_race.ways_to_win == 4
3947

48+
4049
def test_races_initialisation():
4150
races = Races()
4251
for race in races.races:
4352
print(race)
4453
assert races.races == []
4554

55+
4656
def test_races_add_race(sample_race):
4757
races = Races()
4858
races.add_race(sample_race)
4959
assert races.races == [sample_race]
5060

61+
5162
def test_races_solve(sample_races):
52-
assert sample_races.solve() == math.prod([4,8,9])
63+
assert sample_races.solve() == math.prod([4, 8, 9])
5364

5465

55-
def test_create_races(day_6_test_input, sample_races):
56-
test_races = create_races(day_6_test_input)
66+
def test_create_races_part_1(day_6_test_input, sample_races):
67+
test_races = create_races(day_6_test_input, part=1)
5768
assert test_races.n_races() == 3
58-
for race, expected_race in zip(test_races.races,sample_races.races):
69+
for race, expected_race in zip(test_races.races, sample_races.races):
5970
assert race.time == expected_race.time
6071
assert race.distance_to_beat == expected_race.distance_to_beat
6172
assert race.ways_to_win == expected_race.ways_to_win
6273

74+
75+
def test_create_races_part_2(day_6_test_input, sample_race_part_2):
76+
test_races = create_races(day_6_test_input, part=2)
77+
assert test_races.n_races() == 1
78+
test_race = test_races.races[0]
79+
assert test_race.time == sample_race_part_2.time
80+
assert test_race.distance_to_beat == sample_race_part_2.distance_to_beat
81+
assert test_race.ways_to_win == sample_race_part_2.ways_to_win
82+
83+
6384
def test_solve_day_6(day_6_test_input):
64-
assert solve_day_6(day_6_test_input) == 288
85+
assert solve_day_6(day_6_test_input) == (288, 71503)

0 commit comments

Comments
 (0)