def main(): """ Read input from file and count the number of times the list is increasing. """ data = aoc.read_input("input.txt", int) print(count_increase(data, 1)) print(count_increase(data, 3))
def test_count_increase(): """ Test if the count increases """ data = read_input("test.txt", int) assert depth.count_increase(data, 1) == 7 assert depth.count_increase(data, 3) == 5
def test_part2_input(self): # When viewed, my answer is 'AGUEB' expected_result = \ " XX XX X X XXXX XXX \n" \ "X X X X X X X X X \n" \ "X X X X X XXX XXX \n" \ "XXXX X XX X X X X X \n" \ "X X X X X X X X X \n" \ "X X XXX XX XXXX XXX \n" result = day08.part2(aoc.read_input('day08.input')) self.assertEqual(expected_result, result)
def test_part2_input(self): result = day11.part2(aoc.read_input('day11.input')) # Our ship's identifier is AGALRGJE expected = \ " ** ** ** * *** ** ** **** \n" \ " * * * * * * * * * * * * * \n" \ " * * * * * * * * * * *** \n" \ " **** * ** **** * *** * ** * * \n" \ " * * * * * * * * * * * * * * \n" \ " * * *** * * **** * * *** ** **** \n" self.assertEqual(expected, result)
#!/usr/bin/env python3 from functools import reduce from operator import and_, or_ from aoc import read_input def group_reduce(groups, func): return sum(len(reduce(func, group)) for group in groups) def puzzle1(groups): return group_reduce(groups, or_) def puzzle2(groups): return group_reduce(groups, and_) raw = read_input(2020, 6).split('\n\n') groups = [list(map(set, group.split())) for group in raw] print("Puzzle 1: {}".format(puzzle1(groups))) print("Puzzle 2: {}".format(puzzle2(groups)))
def test_part1_input(self): result = day07.part1(aoc.read_input('day07.input')) self.assertEqual(result, 'GDHOSUXACIMRTPWNYJLEQFVZBK')
def test_part2_input(self): result = day07.part2(aoc.read_input('day07.input')) self.assertEqual(result, 1024)
def test_part2_input(self): distance = day24.part2(aoc.read_input('day24.input')) self.assertEqual(4428, distance)
if t < 0: t += m return t def f(bus1, bus2): a_1, m_1 = bus1 a_2, m_2 = bus2 return (a_1 * m_2 * modmultinv(m_2, m_1) + a_2 * m_1 * modmultinv(m_1, m_2)) % (m_1 * m_2), m_1 * m_2 def puzzle1(departure, busse): earliest = min([(bus, bus - departure % bus) for _, bus in busses], key=lambda b: b[1]) return prod(earliest) def puzzle2(busses): return reduce(f, busses) raw = read_input(2020, 13) departure, busses = raw.split() departure = int(departure) busses = [(-pos, int(bus)) for pos, bus in enumerate(busses.split(',')) if bus.isdigit()] print("Puzzle 1: {}".format(puzzle1(departure, busses))) print("Puzzle 2: {}".format(puzzle2(busses)))
if max([path.count(v) for v in path if v.upper() != v]) < max_visit or v not in path: explore.append(v) ret = [] for v in explore: rest = all_path(path + [v], graph, max_visit) for r in rest: if r: ret.append(r) return ret def puzzle1(graph): return len(all_path(['start'], graph, 1)) def puzzle2(graph): return len(all_path(['start'], graph, 2)) raw = read_input(2021, 12) edges = [l.split('-') for l in raw.split()] graph = defaultdict(list) for v1, v2 in edges: graph[v1].append(v2) graph[v2].append(v1) print(f"\033[97m★\033[00m {puzzle1(graph)}") print(f"\033[93m★\033[00m {puzzle2(graph)}")
def test_part2_input(self): result = day04.part2(aoc.read_input('day04.input')) self.assertEqual(710, result)
def test_part1_input(self): result = day11.part1(aoc.read_input('day11.input')) self.assertEqual(2252, result)
def test_dive(): data = read_input("test.txt", str) assert dive.dive(data) == 150 assert dive.aim(data) == 900
def test_part2_input(self): result = day01.part2(aoc.read_input('day01.input')) self.assertEqual(result, 5106777)
#!/usr/bin/env python3 from math import prod import numpy as np from scipy.ndimage.measurements import label from aoc import read_input def puzzle1(a): ap = np.pad(a, 1, mode='constant', constant_values=10) mini = ((a < ap[:-2, 1:-1]) & (a < ap[2:, 1:-1]) & (a < ap[1:-1, :-2]) & (a < ap[1:-1, 2:])) return sum(a[mini] + 1) def puzzle2(a): c, n = label(a<9, structure=[[0,1,0],[1,0,1],[0,1,0]]) _, s = np.unique(c, return_counts=True) return prod(sorted(s)[-4:-1]) a = np.array(list(map(list, read_input(2021, 9).split())), dtype=int) print(f"Puzzle 1: {puzzle1(a)}") print(f"Puzzle 2: {puzzle2(a)}")
def test_part1_input(self): result = day01.part1(aoc.read_input('day01.input')) self.assertEqual(result, 3406432)
# Day 2: Dive! from aoc import read_input data = read_input(2, str.split) x = y = aim = 0 for command, value in data: value = int(value) if command == 'forward': x += value y += aim * value elif command == 'down': aim += value elif command == 'up': aim -= value print(x * aim, x * y)
def test_part1_input(self): result = day05.part1(aoc.read_input('day05.input')) self.assertEqual(result, 11152)
def test_part1_input(self): result = day03.part1(aoc.read_input('day03.input')) self.assertEqual(293, result)
def main(): data = read_input("input.txt", str) print(dive(data)) print(aim(data))
def test_part2_input(self): result = day03.part2(aoc.read_input('day03.input')) self.assertEqual(27306, result)
def test_part2_input(self): result = day18.part2(aoc.read_input('day18.input')) self.assertEqual(1996, result)
t = 1 n = 0 while t != pub: t = (t * subject) % 20201227 n += 1 return n def enc_key(subject, n): t = 1 for _ in range(n): t = (t * subject) % 20201227 return t def puzzle1(door, card): loop_size = bf_loop_size(7, door) return enc_key(card, loop_size) def puzzle2(): pass raw = read_input(2020, 25) door, card = list(map(int, raw.split())) print(f"Puzzle 1: {puzzle1(door, card)}") print(f"Puzzle 2: {puzzle2()}")
def test_part1_input(self): result = day04.part1(aoc.read_input('day04.input')) self.assertEqual(1099, result)
#!/usr/bin/env python3 from aoc import read_input def puzzle1(raw): fwd = filter(lambda s: s['d']=='forward', raw) updown = filter(lambda s: s['d']!='forward', raw) fwd_total = sum((s['m'] for s in fwd)) updown_total = sum((s['m'] if s['d']=='down' else -s['m'] for s in updown)) return fwd_total * updown_total def puzzle2(raw): hor = dep = aim = 0 for s in raw: if s['d'] == 'down': aim += s['m'] if s['d'] == 'up': aim -= s['m'] if s['d'] == 'forward': hor += s['m'] dep += aim * s['m'] return dep * hor raw = read_input(2021, 2).split('\n')[:-1] raw = list(map(lambda s: dict(d=s.split()[0],m=int(s.split()[1])), raw)) print(f"Puzzle 1: {puzzle1(raw)}") print(f"Puzzle 2: {puzzle2(raw)}")
def test_part1_input(self): result = day08.part1(aoc.read_input('day08.input')) self.assertEqual(result, 41926)
#!/usr/bin/env python3 from statistics import median, mean from math import floor, ceil from aoc import read_input def solve(crabs, cost, center): m = center(crabs) opt = {floor(m), ceil(m)} return int(min((sum((cost(c, o) for c in crabs)) for o in opt))) def puzzle1(crabs): cost = lambda crab, pos: abs(crab - pos) return solve(crabs, cost, median) def puzzle2(crabs): cost = lambda crab, pos: abs(crab - pos) * (abs(crab - pos) + 1) // 2 return solve(crabs, cost, mean) raw = read_input(2021, 7) crabs = list(map(int, raw.split(","))) print(f"Puzzle 1: {puzzle1(crabs)}") print(f"Puzzle 2: {puzzle2(crabs)}")
def test_part2_input(self): result = day08.part2(aoc.read_input('day08.input')) self.assertEqual(result, 24262)
def test_part1_input(self): result = day18.part1(aoc.read_input('day18.input')) self.assertEqual(5964, result)
stack = deal_new(stack) elif i.startswith("deal"): _, _, _, n = i.split() stack = deal_inc(stack, int(n)) else: _, n = i.split() stack = cut(stack, int(n)) return stack def puzzle1(instr): stack = tuple(range(10007)) stack = run(instr, stack) pos, *_ = [n for (n, c) in enumerate(stack) if c == 2019] return (pos) def puzzle2(): stack = tuple(range(119315717514047)) for _ in range(101741582076661): stack = run(stack) pos, *_ = [n for (n, c) in enumerate(stack) if c == 2019] return (pos) raw = read_input(2019, 22) instr = tuple(raw.split('\n')[:-1]) print(f"Puzzle 1: {puzzle1(instr)}") print(f"Puzzle 2: {puzzle2()}")