import sys sys.path.append('/Users/jskogeby/git/aoc2020/src') from copy import deepcopy from utils.computer import Computer from utils import parse data = parse.split_input("day8/input.txt") data = parse.split_lines(data, " ", (str, int)) def part1(): cp = Computer(data) cp.execute() print(cp.acc) return cp.read_instructions def part2(): lines_read = part1() for line in lines_read: new_data = deepcopy(data) if new_data[line][0] == "acc": continue elif new_data[line][0] == "jmp": new_data[line][0] = "nop" elif new_data[line][0] == "nop": new_data[line][0] = "jmp" cp = Computer(new_data) result = cp.execute() if result: print(result)
import sys sys.path.append('/Users/jskogeby/git/aoc2020/src') from utils import parse data = parse.split_input("day10/input.txt", form=int) + [0] data.sort() def part1(): jolt1 = 0 jolt3 = 1 # Built-in adapter is rated for 3 jolts for i in range(1, len(data)): diff = data[i] - data[i - 1] if diff == 1: jolt1 += 1 elif diff == 3: jolt3 += 1 print(jolt1 * jolt3) def paths(idx, dic): for i in range(idx + 1, len(data)): if data[i] - data[idx] <= 3: dic[idx] += dic[i] else: break def part2():
import sys sys.path.append('/Users/jskogeby/git/aoc2020/src') from utils import parse data = parse.split_input("day9/input.txt", form=int) preamble = 25 def check_sum(l): sums = [] for i in range(len(l) - 2): for j in range(i + 1, len(l) - 1): sums.append(l[i] + l[j]) return l[-1] in set(sums) def part1(): for i, num in enumerate(data[preamble:]): if not check_sum(data[i:i + preamble + 1]): print(num) return num if __name__ == "__main__": invalid_num = part1() for i in range(2, len(data) - preamble): for j in range(0, len(data) - i): l = data[j:j + i] if sum(l) == invalid_num: l.sort() print(l[0] + l[-1])
import sys sys.path.append('/Users/jskogeby/git/aoc2020/src') import math from utils import parse data = parse.split_input("day13/input.txt", form=str) earliest_departure = int(data[0]) busses = data[1].split(",") deps = [] for offset, frequency in enumerate(busses): if frequency != "x": deps.append((offset,int(frequency))) part1 = [[departure, -earliest_departure % departure] for _, departure in deps] part1 = sorted(part1, key=lambda x: x[1]) part1 = math.prod(part1[0]) print(part1) increment = deps[0][1] t = 0 for i in range(len(deps) - 1): while bool((t + deps[i+1][0]) % deps[i+1][1]): t += increment increment = math.prod([deps[b][1] for b in range(i+2)]) print(t)
import sys sys.path.append('/Users/jskogeby/git/aoc2020/src') from utils import parse groups = parse.group(parse.split_input("day6/input.txt", "\n")) def count_unpack(data): return len(set(parse.unpack(data))) def part1(): print(sum(map(count_unpack, groups))) def part2(): accum = 0 for g in groups: size = len(g) flat = parse.unpack(g) occ = {question: flat.count(question) for question in set(flat)} accum += list(occ.values()).count(size) print(accum) if __name__ == "__main__": part1() part2()