-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2.py
83 lines (63 loc) · 1.96 KB
/
day_2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
N_RED = 12
N_GREEN = 13
N_BLUE = 14
def question_1():
with open("./day_2_input.txt") as file:
total = 0
for i, line in enumerate(file):
is_ok = True
line = line.replace(";", ",")
line = line.replace(":", ",")
line = line.strip("\n")
line = line.split(", ")
is_ok = checker(line[1:])
if is_ok:
print(i+1)
total += i+1
print(i+1, line[1:])
print(total)
def checker(games):
is_ok = True
for string in games:
if "red" in string:
string = string.strip(" red")
if int(string) > N_RED:
is_ok = False
if "green" in string:
string = string.strip(" green")
if int(string) > N_GREEN:
is_ok = False
if "blue" in string:
string = string.strip(" blue")
if int(string) > N_BLUE:
is_ok = False
return is_ok
def question_2():
with open("./day_2_input.txt") as file:
power = 0
for i, line in enumerate(file):
is_ok = True
line = line.replace(";", ",")
line = line.replace(":", ",")
line = line.strip("\n")
line = line.split(", ")
power += calculate_power(line[1:])
print(power)
def calculate_power(line):
red_power = 0
green_power = 0
blue_power = 0
for string in line:
if "red" in string:
string = string.strip(" red")
if int(string) > red_power:
red_power = int(string)
if "green" in string:
string = string.strip(" green")
if int(string) > green_power:
green_power = int(string)
if "blue" in string:
string = string.strip(" blue")
if int(string) > blue_power:
blue_power = int(string)
return red_power * green_power * blue_power