from collections import deque from lib.input import read_lines input = read_lines(9) input = [int(line) for line in input] """ ... <X[k-25] ... X[k-1]> <X[k]> ... for an X[k] there is at least one i and j for which the following holds: X[k] = X[k-i] + X[k-j] and 1 <= i,j <= 25 and i <> j find an algorithm which proves this and which is able to detect if an element does not conform to it. solution: for every number, subtract one of the previous 25 numbers and check if the result is equal to one of them.
from lib.input import read_lines input = read_lines(15) input = input[0].split(',') input = list(map(int, input)) class Game: def __init__(self): self._memory = {} self._last = None self._turn = 1 def add(self, number): self._last = number self._last_spoken_on = self._memory.get(number, None) self._memory[number] = self._turn self._turn += 1 def step(self): last_spoken = self._last_spoken_on number = self._turn - last_spoken - 1 if last_spoken else 0 self.add(number) @property def turn(self): return self._turn @property def last(self): return self._last
from collections import defaultdict, deque import re from lib.input import read_lines input = read_lines(7) regex = re.compile(r'^(?:([a-z\s]+)\sbags\scontain\s)' r'|(?:(\d+)\s([a-z\s]+)\sbags?)(?:,\s*|.$)') def get_mapping(): result = {} for line in input: match = re.findall(regex, line) which = match.pop(0)[0] current = result[which] = {} for item in match: count, item = item[1:] current[item] = int(count) return result def invert_mapping(mapping): result = defaultdict(dict) for this, counts in mapping.items():
from collections import defaultdict, deque from copy import copy, deepcopy import re from lib.input import read_lines input = read_lines('''?x''') def part_1(): return None def part_2(): return None solve_1 = lambda: part_1() solve_2 = lambda: part_2()
from copy import deepcopy from functools import partial import re from lib.input import read_lines input = read_lines(8) pattern = re.compile(r"([a-z]+)\s((?:\+|-)\d+)") def instructions(): for line in input: match = re.match(pattern, line) operation, argument = match.groups() yield operation, int(argument) def parse_code(): program = Program() for operation, argument in instructions(): program.add_instruction(operation, argument) return program class System: def __init__(self): self._accumulator = 0 self._counter = 0
import re from lib.input import read_lines input = read_lines(2) class Rule: def __init__(self, n1, n2, letter): self.n1 = n1 self.n2 = n2 self.letter = letter class OldRule(Rule): def __init__(self, min, max, letter): super().__init__(min, max, letter) self.range = range(min, max + 1) def is_valid(self, password): return password.count(self.letter) in self.range class NewRule(Rule): def is_valid(self, password): n1_valid = password[self.n1 - 1] == self.letter n2_valid = password[self.n2 - 1] == self.letter return n1_valid ^ n2_valid def parse_line(line):
from collections import defaultdict, deque from copy import copy, deepcopy import re from math import sqrt, prod as multiply from lib.input import read_lines input_ = read_lines(20) N_CORNER = 2 N_EDGE = 3 N_INNER = 4 TOP = 0 RIGHT = 1 BOTTOM = 2 LEFT = 3 VERTICAL = 4 HORIZONTAL = 5 def get_x_offset(direction): if direction == LEFT: return -1 if direction == RIGHT: return 1 return 0 def get_y_offset(direction): if direction == BOTTOM: return -1 if direction == TOP: return 1
from lib.input import read_lines X, Y = 0, 1 input = read_lines(3) width, height = len(input[0]), len(input) def is_tree(x, y): return input[y][x] == '#' def count_trees(right, down): trees = 0 position = [0, 0] while position[Y] < height: trees += int(is_tree(*position)) position[X] += right position[Y] += down # forest repeat's to the right position[X] %= width return trees def count_multiple_slopes(slopes): product = 1 for slope in slopes: product *= count_trees(*slope)
from lib.input import read_lines input = read_lines(5) replace = {'F': 0, 'B': 1, 'L': 0, 'R': 1} def seat_id(line): for char, to in replace.items(): line = line.replace(char, str(to)) return int(line, 2) def seat_ids(): return [seat_id(line) for line in input] def highest_seat_id(): return max(seat_ids()) def my_seat_id(): seats = set(seat_ids()) other = [ seat for seat in seats if seat - 1 not in seats and seat - 2 in seats or seat + 1 not in seats and seat + 2 in seats ] assert len(other) == 2, 'the condition is ambiguous'
import re from lib.describe import Is, All, Any, between, equal, match, in_ from lib.input import read_lines, blocks input = read_lines(4) required = set(['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']) optional = set(['cid']) structure = { 'byr': Is(int, between(1920, 2002)), 'iyr': Is(int, between(2010, 2020)), 'eyr': Is(int, between(2020, 2030)), 'hgt': Any([ All([ Is(lambda s: s[-2:], equal('cm')), Is(lambda s: s[:-2], int, between(150, 193)) ]), All([ Is(lambda s: s[-2:], equal('in')), Is(lambda s: s[:-2], int, between(59, 76)) ]) ]), 'hcl': All([ Is(len, equal(7)), Is(lambda s: s[:1], equal('#')), Is(lambda s: s[1:], match(r'^[0-9a-f]*$')) ]), 'ecl': Is(in_(set(['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']))),
from lib.input import read_lines, blocks input = read_lines(6) def answered(): for block in blocks(input): answers = set() for line in block: answers.update(line) yield answers def answered_by_all(): for block in blocks(input): answers = [set(line) for line in block] intersection = set.intersection(*answers) yield intersection def sum_len(collection): return sum(len(e) for e in collection) solve_1 = lambda: sum_len(answered()) solve_2 = lambda: sum_len(answered_by_all())