Skip to content

Commit 5aa6533

Browse files
jameslawlorJames
andauthored
d4p2 solution done (#8)
Co-authored-by: James <james@Susans-MacBook-Air.local>
1 parent cb69126 commit 5aa6533

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ docker run aoc-2023-app
1414
| 01 | ⭐️ | ⭐️ |
1515
| 02 | ⭐️ | ⭐️ |
1616
| 03 | ⭐️ | ⭐️ |
17-
| 04 | ⭐️ | todo |
17+
| 04 | ⭐️ | ⭐️ |
18+
| 05 | todo | todo |

src/aoc_2023/days/4.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
def main():
88
args = parse_args()
99
input = read_input(args.input_file)
10-
result_part_1 = solve_day_4(input)
11-
print(f"Day 4. Total points for part 1 is {result_part_1}. ")
10+
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}. ")
1214

1315

1416
if __name__ == "__main__":

src/aoc_2023/solvers/day_4_solvers.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@ def __init__(self, card_number, winning_numbers, numbers_i_have):
66
self.card_number = card_number
77
self.winning_numbers = winning_numbers
88
self.numbers_i_have = numbers_i_have
9-
self.n_winners = None
9+
self.n_matches = None
1010
self.points = None
11+
self.n_copies = 1
1112

1213
def __str__(self):
1314
return (
1415
f"Card {self.card_number}: {self.winning_numbers} | "
1516
f"{self.numbers_i_have} | "
16-
f"n_winners: {self.n_winners} | points: {self.points}"
17+
f"n_matches: {self.n_matches} | "
18+
f"points: {self.points} | "
19+
f"n_copies: {self.n_copies}"
1720
)
1821

19-
def find_n_winners(self):
20-
self.n_winners = sum(
22+
def find_n_matches(self):
23+
self.n_matches = sum(
2124
[1 for n in self.numbers_i_have if n in self.winning_numbers]
2225
)
2326

2427
def compute_points(self):
25-
if self.n_winners > 0:
26-
self.points = 2 ** (self.n_winners - 1)
28+
if self.n_matches > 0:
29+
self.points = 2 ** (self.n_matches - 1)
2730
else:
2831
self.points = 0
2932

@@ -50,13 +53,25 @@ def create_cards(input_txt):
5053
def compute_total_score(cards):
5154
total_score = 0
5255
for card in cards:
53-
card.find_n_winners()
56+
card.find_n_matches()
5457
card.compute_points()
5558
total_score += card.points
5659
return total_score
5760

5861

62+
def compute_copies(cards):
63+
for card_number, card in enumerate(cards):
64+
n_matches_on_card = card.n_matches
65+
n_copies_of_card = card.n_copies
66+
copies_to_generate = n_copies_of_card
67+
for next_card in cards[card_number+1 : card_number + n_matches_on_card + 1]:
68+
next_card.n_copies += copies_to_generate
69+
return cards
70+
71+
5972
def solve_day_4(input) -> int:
6073
cards = create_cards(input)
6174
total_score = compute_total_score(cards)
62-
return total_score
75+
cards_with_copies = compute_copies(cards)
76+
n_scratchcards = sum([card.n_copies for card in cards_with_copies])
77+
return (total_score, n_scratchcards)

tests/test_day_4_solvers.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import pytest
2-
from aoc_2023.solvers.day_4_solvers import solve_day_4
2+
from aoc_2023.solvers.day_4_solvers import (
3+
solve_day_4,
4+
compute_copies,
5+
create_cards,
6+
compute_total_score,
7+
)
38

49

510
@pytest.fixture
@@ -26,5 +31,26 @@ def day_4_expected_scores():
2631
]
2732

2833

29-
def test_solve_day_4(day_4_test_input, day_4_expected_scores):
30-
assert solve_day_4(day_4_test_input) == sum(day_4_expected_scores)
34+
@pytest.fixture
35+
def day_4_expected_final_cards_count():
36+
return [1, 2, 4, 8, 14, 1]
37+
38+
39+
def test_day_4_expected_final_cards_count(day_4_expected_final_cards_count):
40+
assert sum(day_4_expected_final_cards_count) == 30
41+
42+
43+
def test_solve_day_4(
44+
day_4_test_input, day_4_expected_scores, day_4_expected_final_cards_count
45+
):
46+
assert solve_day_4(day_4_test_input) == (
47+
sum(day_4_expected_scores),
48+
sum(day_4_expected_final_cards_count),
49+
)
50+
51+
def test_compute_copies(day_4_test_input,day_4_expected_final_cards_count):
52+
test_cards = create_cards(day_4_test_input)
53+
compute_total_score(test_cards)
54+
test_cards_with_copies = compute_copies(test_cards)
55+
test_n_scratchcards = [card.n_copies for card in test_cards_with_copies]
56+
assert test_n_scratchcards == day_4_expected_final_cards_count

0 commit comments

Comments
 (0)