-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.py
73 lines (52 loc) · 1.82 KB
/
day_03.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
from typing import List
def parse(data: List[str]) -> List[List[int]]:
results: List[List[int]] = []
for line in data:
for i, item in enumerate(line.strip()):
if len(results) < i + 1:
results.append([])
results[i].append(int(item))
return results
def gamma_epsilon(data: List[List[int]]) -> int:
gamma = 0
epsilon = 0
lenght = len(data)
for i, row in enumerate(data):
total = sum(row)
above = total > len(row) // 2
power = pow(2, lenght - i - 1)
gamma += power * above
epsilon += power * (not above)
return gamma * epsilon
def oxygen(data: List[List[int]]) -> int:
def count(row, mask):
res = {0: [], 1: []}
for i in mask:
res[row[i]].append(i)
return res
def preference(filtered, strategy):
if strategy == "oxygen":
return filtered[1] if len(filtered[1]) >= len(filtered[0]) else filtered[0]
return filtered[0] if len(filtered[0]) <= len(filtered[1]) else filtered[1]
def fmt(index):
return int("".join([str(row[index]) for row in data]), 2)
oxygen = 0
co2 = 0
oxygen_check = [i for i in range(len(data[0]))]
co2_check = [i for i in range(len(data[0]))]
for row in data:
oxygen_check = preference(count(row, oxygen_check), "oxygen")
if len(oxygen_check) == 1:
oxygen = fmt(oxygen_check[0])
oxygen_check = []
co2_check = preference(count(row, co2_check), "co2")
if len(co2_check) == 1:
co2 = fmt(co2_check[0])
co2_check = []
return oxygen * co2
if __name__ == "__main__":
with open("../inputs/day_03.txt") as infile:
data = infile.readlines()
parsed = parse(data)
print(gamma_epsilon(parsed))
print(oxygen(parsed))