-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
102 lines (82 loc) · 2.26 KB
/
2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from collections import defaultdict
from functools import reduce
from utils import get_input
DAY = "2"
YEAR = "2023"
def parse(puzzle_input: str):
"""Parse input"""
return [
{
"id": int(line[: line.index(":")].split()[1]),
"games": [
[colour.split() for colour in game.strip().split(", ")]
for game in line[line.index(":") + 1 :].split(";")
],
}
for line in puzzle_input.strip().splitlines()
]
def part1(data):
"""Solve part 1"""
max_cubes = {
"red": 12,
"green": 13,
"blue": 14,
}
game_ids = []
for line in data:
id = line["id"]
games = line["games"]
invalid = False
for game in games:
for colour in game:
[num, colour_name] = colour
if int(num) > max_cubes[colour_name]:
invalid = True
break
if invalid:
break
if invalid:
break
if not invalid:
game_ids.append(id)
invalid = False
return sum(game_ids)
def part2(data):
"""Solve part 2"""
max_cubes = {
"red": 12,
"green": 13,
"blue": 14,
}
powers = []
for line in data:
games = line["games"]
max_cubes = defaultdict(int)
for game in games:
for colour in game:
[num, colour_name] = colour
num = int(num)
max_cubes[colour_name] = max(max_cubes[colour_name], num)
powers.append(reduce(lambda x, y: x * y, max_cubes.values()))
return sum(powers)
def solve(puzzle_input):
"""Solve the puzzle for the given input"""
data = parse(puzzle_input)
solution1 = part1(data)
solution2 = part2(data)
return solution1, solution2
if __name__ == "__main__":
puzzle_input, test_input = get_input(DAY, YEAR)
test_solutions = solve(test_input)
assert test_solutions[0] == 8
assert test_solutions[1] == 2286
solutions = solve(puzzle_input)
assert solutions[0] == 2239
# assert solutions[1] ==
print("\nSolutions:")
print(
f"""
Part 1: {solutions[0]}
Part 2: {solutions[1]}
"""
)