-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
56 lines (45 loc) · 1.26 KB
/
day2.py
File metadata and controls
56 lines (45 loc) · 1.26 KB
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
import operator
from collections import defaultdict
from functools import reduce
file = open("input.txt")
lines = file.readlines()
REQUIRED_CUBES = {
"red": 12,
"green": 13,
"blue": 14,
}
def parse(line: str):
game, cubes = line.strip().split(": ")
game_id = int(game.split(" ")[1])
cubes_sub = cubes.split("; ")
cubes_res = []
for cube_sub in cubes_sub:
cube_amounts = defaultdict(int)
cubes = cube_sub.split(", ")
for cube in cubes:
amount, color = cube.split(" ")
cube_amounts[color] = int(amount)
cubes_res.append(cube_amounts)
return game_id, cubes_res
def part1():
_sum = 0
for line in lines:
game, cubes = parse(line)
for cube in cubes:
if any(cube[color] > REQUIRED_CUBES[color] for color in REQUIRED_CUBES):
break
else:
_sum += game
return _sum
def part2():
power = 0
for line in lines:
_, cubes = parse(line)
# get max amount of each color
minimals = [max(cube[color] for cube in cubes) for color in REQUIRED_CUBES]
# multiply them
power += reduce(operator.mul, minimals, 1)
return power
print("Part 1:", part1())
print("Part 2:", part2())
file.close()