from core import DefaultLogger, AOCBase log = DefaultLogger().get_log() class Diagnostic(AOCBase): def column_bits(self, data, col): digits = [row[col] for row in data] ones = sum([1 for i in digits if i == '1']) zeros = len(digits) - ones return ones, zeros def most_common_bit(self, data, col): ones, zeros = self.column_bits(data, col) return "1" if ones > zeros or ones == zeros else "0" def least_common_bit(self, data, col): ones, zeros = self.column_bits(data, col) return "0" if zeros < ones or ones == zeros else "1" def gamma(self, data): data = data or self.read_input() gamma_str = "".join( [self.most_common_bit(data, i) for i in range(len(data[0]))]) return gamma_str def epsilon(self, gamma): epsilon_str = "".join([str(abs(int(c) - 1)) for c in list(gamma)]) return epsilon_str
from core import DefaultLogger, AOCBase from collections import defaultdict import re from functools import reduce log = DefaultLogger.get_log() class TicketAnalyzer(AOCBase): """ https://adventofcode.com/2020/day/16 """ def __init__(self): self.data = self.read_input() self.valid_tickets = [] self.valid_values = None self.cabin_rules = {} def _read_ticket(self, t): pass def _parse_rule(self, r): cabin, ranges = r.split(':') min, max = ranges.split('or') return cabin.strip(), min.strip(), max.strip() def _combine_range_values(self, ranges): valid_numbers = set() for m in ranges:
from core import DefaultLogger, AOCBase from .bingo_board import Board log = DefaultLogger().get_log() class Bingo(AOCBase): def __init__(self): self.boards = [] def _split(self, row): return list(filter(lambda c: c != '', row.split(' '))) def _board_generator(self, board_inputs): board_size = len(self._split(board_inputs[0])) for i in range(0, len(board_inputs), board_size): self.boards.append(Board(board_inputs[i:i + 5])) def bingo_input(self, data): numbers_input = data[0] board_inputs = list(filter(lambda r: r != '', data[2:])) self._board_generator(board_inputs) return numbers_input def call_number(self, n): unmarked_sum = None for b in self.boards: finished_board_sum = b.pick_val(n) if not unmarked_sum: unmarked_sum = finished_board_sum