-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.py
53 lines (41 loc) · 1.23 KB
/
day03.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
input = open("day03_input.txt", "r").read().strip().split('\n')
def part1():
gamma = []
eps = []
for bit in range(12):
col = []
for line in input:
col.append(int(line[bit]))
most_common = int(col.count(1) >= col.count(0))
gamma.append(most_common)
eps.append(most_common ^ 1)
print(int("".join(str(x) for x in gamma), 2) * int("".join(str(x) for x in eps), 2))
def part2():
oxygen = input.copy()
co2 = input.copy()
bit = 0
while len(oxygen) > 1:
col = []
for line in oxygen:
col.append(int(line[bit]))
most_common = int(col.count(1) >= col.count(0))
for line in oxygen[:]:
if int(line[bit]) != most_common:
oxygen.remove(line)
bit += 1
bit = 0
while len(co2) > 1:
col = []
least_com = 1
for line in co2:
col.append(int(line[bit]))
if col.count(0) <= col.count(1):
least_com = 0
for line in co2[:]:
if int(line[bit]) != least_com:
co2.remove(line)
bit += 1
res = int("".join(x for x in oxygen[0]), 2) * int("".join(x for x in co2[0]), 2)
print(res)
part1()
part2()