carts = new_carts if len(carts) == 1: i, j = next(iter(carts.values())).get_loc() print(f'{j},{i}') return def day13(input_): part1(input_) part2(input_) TEST_INPUT = test_case(R''' /->-\ | | /----\ | /-+--+-\ | | | | | v | \-+-/ \-+--/ \------/ ''') part1(TEST_INPUT) TEST_INPUT = test_case(R''' />-<\ | | | /<+-\ | | | v \>+</ | | ^ \<->/ ''')
from typing import List from utils import read_input, test_case Input = read_input(19) InputTest = test_case(''' #ip 0 seti 5 0 1 seti 6 0 2 addi 0 1 0 addr 1 2 3 setr 1 0 0 seti 8 0 4 seti 9 0 5 ''') def addr(registers, a, b, c): registers[c] = registers[a] + registers[b] def addi(registers, a, b, c): registers[c] = registers[a] + b def mulr(registers, a, b, c): registers[c] = registers[a] * registers[b] def muli(registers, a, b, c): registers[c] = registers[a] * b
from collections import defaultdict from dataclasses import dataclass from typing import List, Set, Dict, Optional from utils import read_input, test_case INPUT = read_input(7) INPUT_TEST = test_case(''' Step C must be finished before step A can begin. Step C must be finished before step F can begin. Step A must be finished before step B can begin. Step A must be finished before step D can begin. Step B must be finished before step E can begin. Step D must be finished before step E can begin. Step F must be finished before step E can begin. ''') Graph = Dict[str, Set[str]] def parse_graph(input_: List[str]) -> Graph: graph = defaultdict(set) for line in input_: words = line.split() prereq = words[1] step = words[7] graph[step].add(prereq) if prereq not in graph: graph[prereq] = set()
import re from collections import defaultdict, deque from itertools import count from typing import List, Tuple from utils import read_input, test_case INPUT = read_input(17) INPUT_TEST = test_case(''' x=495, y=2..7 y=7, x=495..501 x=501, y=3..7 x=498, y=2..4 x=506, y=1..2 x=498, y=10..13 x=504, y=10..13 y=13, x=498..504 ''') Pos = Tuple[int, int] def day17(input_: List[str]) -> None: min_y = 99999999 max_y = -1 min_x = 99999999 max_x = -1 ground = defaultdict(lambda: defaultdict(lambda: '.')) for line in input_: at, start, end = map(int, re.findall(r'\d+', line))
import dataclasses import re import timeit from typing import List import numpy as np from utils import read_input, test_case INPUT = read_input(23) INPUT_TEST_P1 = test_case(''' pos=<0,0,0>, r=4 pos=<1,0,0>, r=1 pos=<4,0,0>, r=3 pos=<0,2,0>, r=1 pos=<0,5,0>, r=3 pos=<0,0,3>, r=1 pos=<1,1,1>, r=1 pos=<1,1,2>, r=1 pos=<1,3,1>, r=1 ''') INPUT_TEST_P2 = test_case(''' pos=<10,12,12>, r=2 pos=<12,14,12>, r=2 pos=<16,12,12>, r=4 pos=<14,14,14>, r=6 pos=<50,50,50>, r=200 pos=<10,10,10>, r=5 ''')
import copy from collections import Counter from typing import List from utils import read_input, test_case INPUT_TEST = test_case(''' .#.#...|#. .....#|##| .|..|...#. ..|#.....# #.#|||#|#| ...#.||... .|....|... ||...#|.#| |.||||..|. ...#.|..|. ''') INPUT = read_input(18) Plot = List[List[str]] def print_plot(plot: Plot): for row in plot: for i in row: print(i, end='') print()
from day15_lib import day15 from utils import read_input, test_case INPUT = read_input(15) INPUT_TEST = test_case(""" ####### #.G...# #...EG# #.#.#G# #..G#E# #.....# ####### """) INPUT_TEST_1 = test_case(''' ####### #G..#E# #E#E.E# #G.##.# #...#E# #...E.# ####### ''') INPUT_TEST_2 = test_case(''' ####### #E..EG# #.#G.E# #E.##E# #G..#.#
for a in moons: a[0] += a[3] a[1] += a[4] a[2] += a[5] def part1(inp: List[str]) -> None: moons = parse_input(inp) simulate(moons) print(total_energy(moons)) test0 = test_case(""" <x=-1, y=0, z=2> <x=2, y=-10, z=-7> <x=4, y=-8, z=8> <x=3, y=5, z=-1> """) test1 = test_case(""" <x=-8, y=-10, z=0> <x=5, y=5, z=10> <x=2, y=-7, z=3> <x=9, y=-8, z=-3> """) if __name__ == '__main__': part1(INPUT)
angle, ast = min(angles, key=itemgetter(0)) last = ast deg = angle + 0.0001 print(f'{i + 1}th asteroid: {last} at deg {angle:.2f}') asteroids.remove(ast) print(last) print(last[0] * 100 + last[1]) test0 = test_case(''' .#..# ..... ##### ....# ...## ''') test1 = test_case(''' .#..##.###...####### ##.############..##. .#.######.########.# .###.#######.####.#. #####.##.#.##.###.## ..#####..#.######### #################### #.####....###.#.#.## ##.################# #####.##.###..####..
from collections import defaultdict from typing import Dict, List, Tuple from utils import read_input, test_case INPUT = read_input(12) TEST_INPUT = test_case(''' initial state: #..#.#..##......###...### ...## => # ..#.. => # .#... => # .#.#. => # .#.## => # .##.. => # .#### => # #.#.# => # #.### => # ##.#. => # ##.## => # ###.. => # ###.# => # ####. => # ''') def iterate(rules: Dict[str, str], pots: Dict[int, str]) -> Dict[int, str]: i_min = min(pots) i_max = max(pots) pots_new: Dict[int, str] = defaultdict(lambda: '.')
TEST_INPUT = test_case(''' position=< 9, 1> velocity=< 0, 2> position=< 7, 0> velocity=<-1, 0> position=< 3, -2> velocity=<-1, 1> position=< 6, 10> velocity=<-2, -1> position=< 2, -4> velocity=< 2, 2> position=<-6, 10> velocity=< 2, -2> position=< 1, 8> velocity=< 1, -1> position=< 1, 7> velocity=< 1, 0> position=<-3, 11> velocity=< 1, -2> position=< 7, 6> velocity=<-1, -1> position=<-2, 3> velocity=< 1, 0> position=<-4, 3> velocity=< 2, 0> position=<10, -3> velocity=<-1, 1> position=< 5, 11> velocity=< 1, -2> position=< 4, 7> velocity=< 0, -1> position=< 8, -2> velocity=< 0, 1> position=<15, 0> velocity=<-2, 0> position=< 1, 6> velocity=< 1, 0> position=< 8, 9> velocity=< 0, -1> position=< 3, 3> velocity=<-1, 1> position=< 0, 5> velocity=< 0, -1> position=<-2, 2> velocity=< 2, 0> position=< 5, -2> velocity=< 1, 2> position=< 1, 4> velocity=< 2, 1> position=<-2, 7> velocity=< 2, -2> position=< 3, 6> velocity=<-1, -1> position=< 5, 0> velocity=< 1, 0> position=<-6, 0> velocity=< 2, 0> position=< 5, 9> velocity=< 1, -2> position=<14, 7> velocity=<-2, 0> position=<-3, 6> velocity=< 2, -1> ''')