-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
62 lines (43 loc) · 1.51 KB
/
day02.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
"""Advent of Code 2023 - Day 02."""
import sys
from typing import TextIO
RGB = tuple[int, int, int]
Game = tuple[int, list[RGB]]
def parse_rgb(rgb: str) -> RGB:
r, g, b = 0, 0, 0
for value in rgb.split(", "):
n, color = value.split(" ")
match color:
case "red":
r = int(n)
case "green":
g = int(n)
case "blue":
b = int(n)
return r, g, b
def parse_game(line: str) -> Game:
id, subsets = line.split(": ")
return int(id.split(" ")[-1]), [parse_rgb(x) for x in subsets.split("; ")]
def part_one(file: TextIO) -> int:
def is_possible(game: Game) -> bool:
return all(r <= 12 and g <= 13 and b <= 14 for r, g, b in game[1])
games = list(parse_game(line.strip()) for line in file)
return sum(game[0] for game in games if is_possible(game))
def part_two(file: TextIO) -> int:
def power(game: Game) -> int:
r_max, g_max, b_max = 0, 0, 0
for r, g, b in game[1]:
r_max = max(r_max, r)
g_max = max(g_max, g)
b_max = max(b_max, b)
return r_max * g_max * b_max
games = list(parse_game(line.strip()) for line in file)
return sum(map(power, games))
def main():
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file))
if __name__ == "__main__":
main()