-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.py
69 lines (51 loc) · 1.45 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
63
64
65
66
67
68
69
"""
cadybaltz
12/02/2023
AoC 2023 Day 2
Part 1: 1081, 00:09:21
Part 2: 1090, 00:13:02
"""
import sys
import re
def solution(file):
lines = file.readlines()
result = 0
for line_num in range(len(lines)):
line = lines[line_num]
rounds = line.split(';')
red = 12
green = 13
blue = 14
possible = True
min_r = 0
min_g = 0
min_b = 0
for round in rounds:
pattern = re.compile(r'(\d+) red|(\d+) green|(\d+) blue')
matches = pattern.findall(round)
green_sum = 0
red_sum = 0
blue_sum = 0
for match in matches:
red_sum += int(match[0]) if match[0] else 0
green_sum += int(match[1]) if match[1] else 0
blue_sum += int(match[2]) if match[2] else 0
# part 1 logic
if green_sum > green or blue_sum > blue or red_sum > red:
possible = False
# part 2 logic
min_r = max(red_sum, min_r)
min_g = max(green_sum, min_g)
min_b = max(blue_sum, min_b)
# part 1 calculation
# if possible:
# result += line_num + 1
# part 2 calculation
result += min_r * min_g * min_b
return result
if __name__ == '__main__':
if sys.argv[1] == 't':
input = open("test.txt", "r")
else:
input = open("input.txt", "r")
print(solution(input))