-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
executable file
·51 lines (45 loc) · 1.44 KB
/
day2.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
#!/usr/bin/env python3
# [Day 2: Cube Conundrum](https://adventofcode.com/2023/day/2)
import re
import sys
from pathlib import Path
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
lines = data.splitlines()
result = 0
for line in lines:
id, sets = re.match(r"^Game (\d+): (.+)$", line).groups()
ko = False
for subset in sets.split(";"):
for cube in subset.split(","):
cube, color = cube.strip().split(maxsplit=1)
cube = int(cube)
if color == "red" and cube > 12:
ko = True
elif color == "green" and cube > 13:
ko = True
elif color == "blue" and cube > 14:
ko = True
if ko:
break
if ko:
break
if not ko:
result += int(id)
print(result)
result = 0
for line in lines:
id, sets = re.match(r"^Game (\d+): (.+)$", line).groups()
red, green, blue = 0, 0, 0
for subset in sets.split(";"):
for cube in subset.split(","):
cube, color = cube.strip().split(maxsplit=1)
cube = int(cube)
if color == "red":
red = max(red, cube)
elif color == "green":
green = max(green, cube)
elif color == "blue":
blue = max(blue, cube)
result += red * green * blue
print(result)