-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.py
More file actions
68 lines (48 loc) · 1.41 KB
/
Copy pathday3.py
File metadata and controls
68 lines (48 loc) · 1.41 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
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
import sys
def main():
file = open('input3.txt', 'r')
binaries = []
binaries2 = []
for line in file:
binaries.append((map(int,line.strip())))
binaries2.append(list((map(int,line.strip()))))
summed = [ sum(x) for x in zip(*binaries) ]
gamma = []
for n in summed:
if n >= 500:
gamma.append(1)
else:
gamma.append(0)
episilon = []
for i in range(len(gamma)):
episilon.append(gamma[i]^1)
print("Part 1: " + str(binatodeci(gamma) * binatodeci(episilon)))
sokesett = binaries2.copy()
oxygen = algo(0, sokesett, True)
co2 = algo(0, sokesett, False)
print("Part 2: " + str(binatodeci(oxygen) * binatodeci(co2)))
# bool: 1 = oxygen, 0 = co2
def algo(i, list, bool):
if len(list) == 1:
return list[0]
onelist = []
zerolist = []
for n in list:
if n[i] == 1:
onelist.append(n)
else:
zerolist.append(n)
if bool:
if len(onelist) >= len(zerolist):
return algo(i+1, onelist, bool)
else:
return algo(i+1, zerolist, bool)
else:
if len(onelist) < len(zerolist):
return algo(i+1, onelist, bool)
else:
return algo(i+1, zerolist, bool)
def binatodeci(binary):
return sum(val*(2**idx) for idx, val in enumerate(reversed(binary)))
if __name__ == "__main__":
main()