Esempio n. 1
0
def test_get_ints():
    inp = Input(IFILE, ["""123
456
7
0
"""])
    inp.use_test(0)
    i = inp.get_ints()
    assert(len(i) == 4)
    assert(i == [123, 456, 7, 0])
Esempio n. 2
0
def test_get_ints_tolerant():
    inp = Input(IFILE, ["""123
456abc
gg7
foo
3
"""])
    inp.use_test(0)
    i = inp.get_ints_tolerant(-1)
    assert(len(i) == 5)
    assert(i == [123, 456, 7, -1, 3])
Esempio n. 3
0
def test_get_groups():
    inp = Input(IFILE, ["""abc def
ghi jkl

xyz
abc
def
"""])
    inp.use_test(0)
    gr = inp.get_groups()
    assert(len(gr) == 2)
    assert(len(gr[0]) == 2)
    assert(gr[0] == ['abc def', 'ghi jkl'])
    assert(len(gr[1]) == 3)
    assert(gr[1] == ['xyz', 'abc', 'def'])
Esempio n. 4
0
def solution(puzzle_input: aoc.Input):
    codes = np.array([[int(b) for b in line.strip()]
                      for line in puzzle_input.lines()])
    gamma_arr = codes.sum(axis=0) > (len(codes) // 2)
    gamma = to_int(gamma_arr)
    epsilon = to_int(~gamma_arr)
    return gamma * epsilon
Esempio n. 5
0
def solution(puzzle_input: aoc.Input):
    lines = puzzle_input.lines()
    codes = [int(line, base=2) for line in lines]
    code_width = len(lines[0].strip())
    msb = 1 << (code_width - 1)
    oxygen_generator_rating = rating(codes, msb, True)
    CO2_scrubber_rating = rating(codes, msb, False)
    return oxygen_generator_rating * CO2_scrubber_rating
Esempio n. 6
0
def solution(puzzle_input: aoc.Input):
    numbers, *boards = puzzle_input.paragraphs()
    numbers = (int(n) for n in numbers.split(','))
    boards = [BingoBoard(board) for board in boards]
    for number in numbers:
        for board in boards:
            if board.mark(number):
                return number * board.score()
Esempio n. 7
0
def test_use_tests():
    inp = Input(IFILE, ["""abc
def
""", """ghi"""])
    inp.use_test(0)
    ls = inp.get_lines()
    assert(len(ls) == 3)
    assert(ls == ["abc", "def", ""])
    inp.use_test(1)
    ls = inp.get_lines()
    assert(len(ls) == 1)
    assert(ls == ["ghi"])
Esempio n. 8
0
def solution(puzzle_input: aoc.Input):
    numbers, *boards = puzzle_input.paragraphs()
    numbers = (int(n) for n in numbers.split(','))
    boards = [BingoBoard(board) for board in boards]
    for number in numbers:
        unfinished_boards = [
            board for board in boards if not board.mark(number)
        ]
        if len(boards) == 1 and boards[0].mark(number):
            return number * boards[0].score()
        boards = unfinished_boards
Esempio n. 9
0
#!/usr/bin/python3.8

from aoc import Input, Grid
from collections import defaultdict, deque

inp = Input('input11.txt', [
    """L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL"""
])

# inp.use_test(0)

orig_grid = Grid(inp.get_lines())


def surrounding_seats_part1(grid):
    visible_seats = defaultdict(list)
    for r in range(grid.h):
        for c in range(grid.w):
            if grid.get(r, c) == '.':
                continue
            visible_seats[(r, c)] = grid.neighbors8(r, c)
    return visible_seats
Esempio n. 10
0
def test_read_file():
    inp = Input(IFILE)
    ints = inp.get_ints()
    assert(len(ints) == 200)
    assert(ints[0] == 1918)
    assert(ints[-1] == 1407)
Esempio n. 11
0
#!/usr/bin/python3.8

from aoc import Input

inp = Input('input12.txt', ["""F10
N3
F7
R90
F11
"""])

# inp.use_test(0)

navi = [(ln[0], int(ln[1:])) for ln in inp.get_valid_lines()]
# It seems that L and R values are always multiples of 90

x = 0
y = 0
face = 0
directions = 'ENWS'

for i, a in navi:
    if i == 'F':
        i = directions[face]

    if i == 'E':
        x += a
    elif i == 'N':
        y += a
    elif i == 'W':
        x -= a
Esempio n. 12
0
#!/usr/bin/python3.8

from aoc import Input
import re
import sys

inp = Input(
    'input08.txt',
    ["""nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6"""])

#inp.use_test(0)

program = []
for ln in inp.get_valid_lines():
    instr = ln[:3]
    m = re.search(r'-?\d+', ln)
    assert (m is not None)
    op = int(m.group(0))
    program += [[instr, op]]


def run_prog(prog):
    acc = 0
Esempio n. 13
0
inp = Input('input10.txt', [
    """16
10
15
5
1
11
7
19
6
12
4
""", """28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3
"""
])
Esempio n. 14
0
from aoc import Input
from collections import defaultdict, deque
import re

target = "shiny gold"

input = Input('input07.txt', ["""light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.
""",
"""shiny gold bags contain 2 dark red bags.
dark red bags contain 2 dark orange bags.
dark orange bags contain 2 dark yellow bags.
dark yellow bags contain 2 dark green bags.
dark green bags contain 2 dark blue bags.
dark blue bags contain 2 dark violet bags.
dark violet bags contain no other bags.
"""])

# input.use_test(1)

line_pattern = re.compile(r'^(\w+ \w+) bags contain (.*)\.$')
cont_pattern = re.compile(r'(\d+) (\w+ \w+) bags?$')

bigger_to_smaller = defaultdict(list)