From 700245aa3ad9085b9cad51095ae5b9d3c1c7d77d Mon Sep 17 00:00:00 2001 From: a Date: Tue, 7 Dec 2021 23:41:35 -0600 Subject: [PATCH] cleanup and catchup --- 2021/03/03.py | 79 ++++++++++++++++++++++++++++++++++++++++++--------- 2021/07/07.py | 24 ++++++++-------- 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/2021/03/03.py b/2021/03/03.py index a6c0a24..14d0ec0 100644 --- a/2021/03/03.py +++ b/2021/03/03.py @@ -2,27 +2,78 @@ report = [] with open("input.txt") as f: lines = f.read().split('\n') - for line in lines: - report.append(line) +for line in lines: + report.append(line) gamma = '' -power = 2 ** len(report[0]) - 1 - for i in range(len(report[0])): - ones_count = 0 - for number in report: - if number[i] == '1': - ones_count += 1 + ones_count = 0 + for number in report: + if number[i] == '1': + ones_count += 1 - half = len(report) / 2 - if ones_count > half: - gamma += '1' - else: - gamma += '0' + half = len(report) / 2 + if ones_count > half: + gamma += '1' + else: + gamma += '0' +power = 2 ** len(gamma) - 1 gamma = int(gamma, 2) epsilon = power - gamma part1 = gamma * epsilon print(part1) -o2 = co2 = 0 \ No newline at end of file + +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) diff --git a/2021/07/07.py b/2021/07/07.py index 83a834e..4d5e0dc 100644 --- a/2021/07/07.py +++ b/2021/07/07.py @@ -1,24 +1,24 @@ 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 math import floor, ceil def part1(crabs): - 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. - return sum(offsets) + 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. + return sum(offsets) print(part1(crabs)) def part2(crabs): - def cost(d): - return d*(d+1)//2 # sum of successive integers = triangle numbers. - # because the mean is not an integer, - m1 = floor(mean(crabs)) # we might need the floor, - m2 = ceil(mean(crabs)) # or we might need the ceiling. - 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. - return min([sum(fuel1), sum(fuel2)]) # just take the lesser of the two. + def cost(d): + return d*(d+1)//2 # sum of successive integers = triangle numbers. + # because the mean is not an integer, + m1 = floor(mean(crabs)) # we might need the floor, + m2 = ceil(mean(crabs)) # or we might need the ceiling. + 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. + return min([sum(fuel1), sum(fuel2)]) # just take the lesser of the two. print(part2(crabs)) \ No newline at end of file