cleanup and catchup
This commit is contained in:
parent
10875cb375
commit
700245aa3a
|
@ -2,12 +2,10 @@ report = []
|
||||||
|
|
||||||
with open("input.txt") as f:
|
with open("input.txt") as f:
|
||||||
lines = f.read().split('\n')
|
lines = f.read().split('\n')
|
||||||
for line in lines:
|
for line in lines:
|
||||||
report.append(line)
|
report.append(line)
|
||||||
|
|
||||||
gamma = ''
|
gamma = ''
|
||||||
power = 2 ** len(report[0]) - 1
|
|
||||||
|
|
||||||
for i in range(len(report[0])):
|
for i in range(len(report[0])):
|
||||||
ones_count = 0
|
ones_count = 0
|
||||||
for number in report:
|
for number in report:
|
||||||
|
@ -20,9 +18,62 @@ for i in range(len(report[0])):
|
||||||
else:
|
else:
|
||||||
gamma += '0'
|
gamma += '0'
|
||||||
|
|
||||||
|
power = 2 ** len(gamma) - 1
|
||||||
gamma = int(gamma, 2)
|
gamma = int(gamma, 2)
|
||||||
epsilon = power - gamma
|
epsilon = power - gamma
|
||||||
part1 = gamma * epsilon
|
part1 = gamma * epsilon
|
||||||
print(part1)
|
print(part1)
|
||||||
|
|
||||||
o2 = co2 = 0
|
|
||||||
|
o2_candidates = set()
|
||||||
|
co2_candidates = set()
|
||||||
|
for number in report:
|
||||||
|
o2_candidates.add(number)
|
||||||
|
co2_candidates.add(number)
|
||||||
|
|
||||||
|
for i in range(len(report[0])):
|
||||||
|
if len(o2_candidates) == 1:
|
||||||
|
break
|
||||||
|
# first pass: count the bits in each position
|
||||||
|
ones_count = 0
|
||||||
|
zeros_count = 0
|
||||||
|
for number in o2_candidates:
|
||||||
|
if number[i] == '0':
|
||||||
|
zeros_count += 1
|
||||||
|
else:
|
||||||
|
ones_count += 1
|
||||||
|
# Keep 1, unless 0 is more common.
|
||||||
|
if zeros_count > ones_count:
|
||||||
|
keep = '0'
|
||||||
|
else:
|
||||||
|
keep = '1'
|
||||||
|
# second pass: filter out the candidates
|
||||||
|
for number in o2_candidates.copy():
|
||||||
|
if number[i] != keep:
|
||||||
|
o2_candidates.remove(number)
|
||||||
|
|
||||||
|
for i in range(len(report[0])):
|
||||||
|
if len(co2_candidates) == 1:
|
||||||
|
break
|
||||||
|
# first pass: count the bits in each position
|
||||||
|
ones_count = 0
|
||||||
|
zeros_count = 0
|
||||||
|
for number in co2_candidates:
|
||||||
|
if number[i] == '0':
|
||||||
|
zeros_count += 1
|
||||||
|
else:
|
||||||
|
ones_count += 1
|
||||||
|
# Keep 0, unless 1 is less common.
|
||||||
|
if ones_count < zeros_count:
|
||||||
|
keep = '1'
|
||||||
|
else:
|
||||||
|
keep = '0'
|
||||||
|
# second pass: filter out the candidates
|
||||||
|
for number in co2_candidates.copy():
|
||||||
|
if number[i] != keep:
|
||||||
|
co2_candidates.remove(number)
|
||||||
|
|
||||||
|
o2 = int(o2_candidates.pop(), 2)
|
||||||
|
co2 = int(co2_candidates.pop(), 2)
|
||||||
|
part2 = o2 * co2
|
||||||
|
print(part2)
|
||||||
|
|
Loading…
Reference in a new issue