-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3!.py
More file actions
75 lines (64 loc) · 1.35 KB
/
day3!.py
File metadata and controls
75 lines (64 loc) · 1.35 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
69
70
71
72
73
74
75
from functools import reduce
bitLen = 12
lines = []
with open("day3.txt", "r") as f:
f2 = ["00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
"11001",
"00010",
"01010"]
for line in f:
lines.append( [int(c) for c in line[:-1]] )
#print(lines)
def commonBit(one, two):
common = [None] * bitLen
for i in range(bitLen):
common[i] = one[i] + two[i]
return common
def list2bin(l):
res = 0
for i in range(len(l)):
res += pow(2, i) * l[-(i+1)]
return res
def filterO2(l, i):
if common[i] == 0.5:
return l[i] == 1
return l[i] == round(common[i])
def filterCO(l, i):
if common[i] == 0.5:
return l[i] == 0
return l[i] != round(common[i])
workList = lines
for i in range(bitLen):
print(workList)
common = reduce(commonBit, workList)
common = [bit / len(workList) for bit in common]
newList = []
for set in workList:
if filterO2(set, i):
newList.append(set)
workList = newList
if (len(workList) == 1):
break
print(workList[0])
print(list2bin(workList[0]))
workList = lines
for i in range(bitLen):
common = reduce(commonBit, workList)
common = [bit / len(workList) for bit in common]
newList = []
for set in workList:
if filterCO(set, i):
newList.append(set)
workList = newList
if (len(workList) == 1):
break
print(workList[0])
print(list2bin(workList[0]))