Skip to content

Commit cb69126

Browse files
jameslawlorJames
andauthored
day 4 part 1 solution (#7)
Co-authored-by: James <james@Susans-MacBook-Air.local>
1 parent b233afd commit cb69126

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ solutions:
1313
python3 src/aoc_2023/days/1.py --input_file inputs/1.txt --part 1
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
16-
python3 src/aoc_2023/days/3.py --input_file inputs/3.txt
16+
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

README.md

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

src/aoc_2023/days/4.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from aoc_2023.solvers.day_4_solvers import (
2+
solve_day_4,
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_4(input)
11+
print(f"Day 4. Total points for part 1 is {result_part_1}. ")
12+
13+
14+
if __name__ == "__main__":
15+
main()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import re
2+
3+
4+
class Card:
5+
def __init__(self, card_number, winning_numbers, numbers_i_have):
6+
self.card_number = card_number
7+
self.winning_numbers = winning_numbers
8+
self.numbers_i_have = numbers_i_have
9+
self.n_winners = None
10+
self.points = None
11+
12+
def __str__(self):
13+
return (
14+
f"Card {self.card_number}: {self.winning_numbers} | "
15+
f"{self.numbers_i_have} | "
16+
f"n_winners: {self.n_winners} | points: {self.points}"
17+
)
18+
19+
def find_n_winners(self):
20+
self.n_winners = sum(
21+
[1 for n in self.numbers_i_have if n in self.winning_numbers]
22+
)
23+
24+
def compute_points(self):
25+
if self.n_winners > 0:
26+
self.points = 2 ** (self.n_winners - 1)
27+
else:
28+
self.points = 0
29+
30+
31+
def parse_input_line(line):
32+
card_number = re.findall(r"(\d+)", line.split(":")[0])[0]
33+
all_numbers = line.split(":")[1]
34+
[winning_numbers, numbers_i_have] = all_numbers.split("|")
35+
winning_numbers = re.findall(r"(\d+)", winning_numbers)
36+
numbers_i_have = re.findall(r"(\d+)", numbers_i_have)
37+
38+
return (card_number, winning_numbers, numbers_i_have)
39+
40+
41+
def create_cards(input_txt):
42+
cards = []
43+
for line in input_txt:
44+
(card_number, winning_numbers, numbers_i_have) = parse_input_line(line)
45+
card = Card(card_number, winning_numbers, numbers_i_have)
46+
cards.append(card)
47+
return cards
48+
49+
50+
def compute_total_score(cards):
51+
total_score = 0
52+
for card in cards:
53+
card.find_n_winners()
54+
card.compute_points()
55+
total_score += card.points
56+
return total_score
57+
58+
59+
def solve_day_4(input) -> int:
60+
cards = create_cards(input)
61+
total_score = compute_total_score(cards)
62+
return total_score

tests/test_day_4_solvers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from aoc_2023.solvers.day_4_solvers import solve_day_4
3+
4+
5+
@pytest.fixture
6+
def day_4_test_input():
7+
return [
8+
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53",
9+
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19",
10+
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1",
11+
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83",
12+
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36",
13+
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11",
14+
]
15+
16+
17+
@pytest.fixture
18+
def day_4_expected_scores():
19+
return [
20+
8,
21+
2,
22+
2,
23+
1,
24+
0,
25+
0,
26+
]
27+
28+
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)

0 commit comments

Comments
 (0)