cleanup and catchup
This commit is contained in:
parent
10875cb375
commit
700245aa3a
|
@ -2,27 +2,78 @@ 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:
|
||||||
if number[i] == '1':
|
if number[i] == '1':
|
||||||
ones_count += 1
|
ones_count += 1
|
||||||
|
|
||||||
half = len(report) / 2
|
half = len(report) / 2
|
||||||
if ones_count > half:
|
if ones_count > half:
|
||||||
gamma += '1'
|
gamma += '1'
|
||||||
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)
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
with open("input.txt") as f:
|
with open("input.txt") as f:
|
||||||
crabs = [int(x) for x in f.read().split(',')]
|
crabs = [int(x) for x in f.read().split(',')]
|
||||||
|
|
||||||
from statistics import median, mean
|
from statistics import median, mean
|
||||||
from math import floor, ceil
|
from math import floor, ceil
|
||||||
|
|
||||||
def part1(crabs):
|
def part1(crabs):
|
||||||
m = int(median(crabs)) # the median is the midpoint
|
m = int(median(crabs)) # the median is the midpoint
|
||||||
offsets = [abs(x-m) for x in crabs] # that will be used to calculate simple cost.
|
offsets = [abs(x-m) for x in crabs] # that will be used to calculate simple cost.
|
||||||
return sum(offsets)
|
return sum(offsets)
|
||||||
|
|
||||||
print(part1(crabs))
|
print(part1(crabs))
|
||||||
|
|
||||||
def part2(crabs):
|
def part2(crabs):
|
||||||
def cost(d):
|
def cost(d):
|
||||||
return d*(d+1)//2 # sum of successive integers = triangle numbers.
|
return d*(d+1)//2 # sum of successive integers = triangle numbers.
|
||||||
# because the mean is not an integer,
|
# because the mean is not an integer,
|
||||||
m1 = floor(mean(crabs)) # we might need the floor,
|
m1 = floor(mean(crabs)) # we might need the floor,
|
||||||
m2 = ceil(mean(crabs)) # or we might need the ceiling.
|
m2 = ceil(mean(crabs)) # or we might need the ceiling.
|
||||||
fuel1 = [cost(abs(x-m1)) for x in crabs] # we calculate both costs
|
fuel1 = [cost(abs(x-m1)) for x in crabs] # we calculate both costs
|
||||||
fuel2 = [cost(abs(x-m2)) for x in crabs] # because it could be either one.
|
fuel2 = [cost(abs(x-m2)) for x in crabs] # because it could be either one.
|
||||||
return min([sum(fuel1), sum(fuel2)]) # just take the lesser of the two.
|
return min([sum(fuel1), sum(fuel2)]) # just take the lesser of the two.
|
||||||
|
|
||||||
print(part2(crabs))
|
print(part2(crabs))
|
Loading…
Reference in a new issue