-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay2CubeGame.py
More file actions
40 lines (36 loc) · 1.07 KB
/
Day2CubeGame.py
File metadata and controls
40 lines (36 loc) · 1.07 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
import sys, os
import re
import numpy as np
cube_count = {'red':12,
'green':13,
'blue': 14}
colors = ['red','green','blue']
def is_possible(string):
split1 = string.split(': ')
id_num = int(re.findall(r"\d+",split1[0])[0])
split2 = split1[1].split('; ')
max_shown = {'red':0,
'green':0,
'blue': 0}
for s2 in split2:
colors = re.findall(r"red|blue|green",s2)
n_shown = re.findall(r"\d+",s2)
for i,col in enumerate(colors):
max_shown[col] = max(int(n_shown[i]),max_shown[col])
is_possible = True
for key in max_shown.keys():
if max_shown[key] > cube_count[key]:
is_possible = False
if is_possible:
return id_num
else:
return 0
if __name__ == '__main__':
input_file = 'Day2Input.txt'
sum = 0
with open(input_file,'r') as f:
curr_line = f.readline()
while curr_line:
sum += is_possible(curr_line)
curr_line = f.readline()
print(sum)