You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
62 lines
1.2 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
def inverted(i):
|
|
if i == 0:
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
def filter(lis, pos, i):
|
|
if len(lis) == 1:
|
|
return lis
|
|
if sum([b[pos] for b in lis]) >= sum([inverted(b[pos]) for b in lis]):
|
|
val = i
|
|
else:
|
|
val = inverted(i)
|
|
|
|
return [i for i in lis if i[pos] == val]
|
|
|
|
if __name__ == '__main__':
|
|
lines = open(sys.argv[1]).readlines()
|
|
|
|
bins = [[int(i) for i in list(l.strip('\n'))] for l in lines]
|
|
|
|
# challenge 1
|
|
lis = [0] * len(bins[0])
|
|
|
|
for i in range(len(bins[0])):
|
|
lis[i] = sum([b[i] for b in bins])
|
|
|
|
g = ''
|
|
e = ''
|
|
|
|
for i in range(len(bins[0])):
|
|
if lis[i] > 0.5 * len(lines):
|
|
g += '1'
|
|
e += '0'
|
|
else:
|
|
g += '0'
|
|
e += '1'
|
|
|
|
gamma = int(g, 2)
|
|
eps = int(e,2)
|
|
|
|
res1 = str(gamma * eps)
|
|
print("challenge 1:" + res1 + "\n")
|
|
|
|
ox = bins
|
|
co2 = bins
|
|
|
|
# challenge 2
|
|
for i in range(len(bins[0])):
|
|
ox = filter(ox, i, 1)
|
|
co2 = filter(co2, i, 0)
|
|
|
|
oxr = int(''.join(str(i) for i in ox[0]), 2)
|
|
csr = int(''.join(str(i) for i in co2[0]), 2)
|
|
|
|
res2 = str(oxr * csr)
|
|
print("challenge 2:" + res2 + "\n")
|
|
|
|
|