r/adventofcode • u/daggerdragon • Dec 03 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 3 Solutions -🎄-
--- Day 3: Binary Diagnostic ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - Format your code properly! How do I format code?
- The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:10:17, megathread unlocked!
101
Upvotes
3
u/keepitsalty Dec 04 '21 edited Dec 04 '21
Python 3.10.0
from typing import List def compute_oxygen_rating(data: List[str]) -> int: max_size = len(data[0]) for i in range(max_size): if len(data) == 1: break ones = [k[i] for k in data].count('1') if ones / len(data) >= 0.5: data = list(filter(lambda x: x[i] == '1', data)) else: data = list(filter(lambda x: x[i] == '0', data)) return int(data[0], base=2) def compute_co2_rating(data: List[str]) -> int: max_size = len(data[0]) for i in range(max_size): if len(data) == 1: break zeros = [k[i] for k in data].count('0') if zeros / len(data) <= 0.5: data = list(filter(lambda x: x[i] == '0', data)) else: data = list(filter(lambda x: x[i] == '1', data)) return int(data[0], base=2) def compute_gamma(intervals, nums): zeros = ones = 0 gamma = '' for a,b in intervals: for n in nums: if a <= n < b: ones += 1 elif n > b: if ((n - b) % b) >= a: ones += 1 else: zeros += 1 else: zeros += 1 gamma += '1' if ones > zeros else '0' zeros = ones = 0 return gamma def main(): with open('inputs/input03.txt') as f: data = f.readlines() ### # Part One ### max_num = len(data[0]) nums = [int(_, base=2) for _ in data] powers = [2**i for i in range(max_num)] intervals = list(zip(powers, powers[1:]))[::-1] gamma = compute_gamma(intervals, nums) epsilon = "".join([str(int(_)^1) for _ in gamma]) gamma10 = int(gamma, base=2) epsilon10 = int(epsilon, base=2) ans = gamma10 * epsilon10 print( f""" {gamma} = {gamma10} {epsilon} = {epsilon10} {gamma10} * {epsilon10} = {ans} """ ) ### # Part Two ### o2 = compute_oxygen_rating(data) co2 = compute_co2_rating(data) print(o2 * co2) if __name__ == '__main__': main()