from math import cos, sin, radians from advent.input_reader import read_list_of_values instructions = read_list_of_values('input/2020/day_12', str) x = 0 y = 0 dir_x = 1 dir_y = 0 angle = 0 for i in instructions: order, val = i[0], int(i[1:]) if order == 'N': y += val if order == 'S': y -= val if order == 'E': x += val if order == 'W': x -= val if order == 'F': x += dir_x * val y += dir_y * val if order == 'R': angle -= val if order == 'L': angle += val dir_x = int(cos(radians(angle))) dir_y = int(sin(radians(angle)))
from advent.input_reader import read_list_of_values answers = read_list_of_values('input/2020/day_6', str) def at_least_one_yes(): total = 0 current_group = set() for line in answers: if len(line) > 1: current_group.update(set(line[:-1])) else: total += len(current_group) current_group = set() total += len(current_group) return total print('PART 1') print(at_least_one_yes()) def all_yes(): total = 0 current_group = set(chr(i) for i in range(256)) for line in answers: if len(line) > 1: current_group = current_group.intersection(set(line[:-1])) else: print(current_group, len(current_group)) total += len(current_group)
from advent.input_reader import read_list_of_values report = read_list_of_values('input/2020/day_1', int) report = sorted(report) def find_sum(sub_report, total): index_a = 0 index_b = len(sub_report) - 1 while sub_report[index_a] + sub_report[index_b] != total: if sub_report[index_a] + sub_report[index_b] > total: index_b -= 1 else: index_a += 1 if index_a >= index_b: return None, None return index_a, index_b a, b = find_sum(report, 2020) print('PART 1') print(report[a], report[b], report[a] + report[b], report[a] * report[b]) print('PART 2') for i, value in enumerate(report): total_to_reach = 2020 - value a, b = find_sum(report[i + 1:], total_to_reach) if a != b: print(
from advent.input_reader import read_list_of_values boarding_passes = read_list_of_values('input/2020/day_5', str) def get_seat_number(code): row, column = 0, 0 for i in range(0, 7): if code[i] == 'B': row += pow(2, 6-i) for i in range(0, 3): if code[i+7] == 'R': column += pow(2, 2-i) number = row * 8 + column return number seats = sorted(map(get_seat_number, boarding_passes)) print('PART 1') print(max(seats)) print('PART 2') start = min(seats) for i, seat in enumerate(seats): if i+start != seat: print(seat) break
from advent.input_reader import read_list_of_values adapters = read_list_of_values('input/2020/day_10', int) adapters = [0] + sorted(adapters) adapters.append(adapters[-1]+3) one_jolt = 0 three_jolt = 0 for i in range(1, len(adapters)): diff = adapters[i] - adapters[i-1] if diff == 1: one_jolt += 1 if diff == 3: three_jolt += 1 assert 0 < diff < 4 print('Part 1') print(one_jolt, three_jolt, one_jolt * three_jolt) combinations_so_far = 1 other_ways_to_get_to = {adapter: 0 for adapter in adapters} for i in range(0, len(adapters)-1): value = adapters[i] if other_ways_to_get_to[value]: combinations_so_far += other_ways_to_get_to[value] # print(f'{combinations_so_far} ways to get to {value}') # assert combinations_so_far < 10000000000000 if i == len(adapters)-1: break
from advent.input_reader import read_list_of_values modules = read_list_of_values('input/2019/day_1', int) requirements = 0 for module in modules: module -= module % 3 module /= 3 module -= 2 requirements += int(module) print('PART 1') print(requirements) def need_fuel(mass): mass -= mass % 3 mass /= 3 mass -= 2 if int(mass) <= 0: return 0 return int(mass + need_fuel(mass)) requirements = 0 for module in modules: requirements += need_fuel(module) print('PART 2') print(requirements)
from advent.input_reader import read_list_of_values rules = read_list_of_values('input/2020/day_7', str) print('Part 1') can_contain_gold = {} contained_by_colors = {} for rule in rules: container, contained = rule.split(' contain ') container = container.replace(' bags', '') if contained.startswith('no other bags'): contained_by_colors[container] = set() else: containees = contained.replace(' bags', '').replace( ' bag', '').replace('.', '').replace('\n', '').split(', ') contained_by_colors[container] = set( [' '.join(bag.split(' ')[1:]) for bag in containees]) def can_contain_given_color(bag, given_color): if bag in can_contain_gold: return can_contain_gold[bag] if not contained_by_colors[bag]: can_contain_gold[bag] = False return False if given_color in contained_by_colors[bag]: can_contain_gold[bag] = True return True for sub_bag in contained_by_colors[bag]: if can_contain_given_color(sub_bag, given_color):
from advent.input_reader import read_list_of_values cypher = read_list_of_values('input/2020/day_9', int) pre = 25 inv = -1 print('Part 1') for i in range(pre, len(cypher)): val = cypher[i] ok = False for n in cypher[i - pre:i]: for m in cypher[i - pre:i]: if n != m and n + m == val: ok = True break if ok: break if not ok: inv = val print(val) break print('Part 2') ok = False for i, n in enumerate(cypher): # print(f'start at [{i}]: {n}') val = n for j, m in enumerate(cypher[i + 1:]): val += m