blob: 025b892777716d54c86673030756a4488a54c3f7 (
plain)
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
|
import sys
data = sys.argv[1] if len(sys.argv) > 1 else "../day3data.txt"
N = []
for line in open(data):
line = line.strip()
N.append(line)
width = len(N[0])
for x in N:
assert len(x) == width
V = [[0 for _ in range(width)] for _ in range(2)]
for x in N:
for i, c in enumerate(x):
V[1 if c == "1" else 0][i] += 1
gamma = ""
epsilon = ""
for i in range(width):
if V[0][i] > V[1][i]:
gamma += "0"
epsilon += "1"
else:
gamma += "1"
epsilon += "0"
print(int(gamma, 2) * int(epsilon, 2))
A = list(N)
B = list(N)
for i in range(width):
if len(A) > 1:
a0 = len([x for x in A if x[i] == "0"])
a1 = len([x for x in A if x[i] == "1"])
if a1 >= a0:
A = [x for x in A if x[i] == "1"]
else:
A = [x for x in A if x[i] == "0"]
if len(B) > 1:
b0 = len([x for x in B if x[i] == "0"])
b1 = len([x for x in B if x[i] == "1"])
if b1 >= b0:
B = [x for x in B if x[i] == "0"]
else:
B = [x for x in B if x[i] == "1"]
print(int(A[0], 2) * int(B[0], 2))
|