Exemple #1
0
        node, tile = grid[y, x]
        for neighbor in G[node]:
            for neighbor_tile in orientations(tiles[neighbor]):
                if np.array_equal(tile[-1], neighbor_tile[0]):
                    grid[y + 1, x] = neighbor, neighbor_tile
                    break
                if np.array_equal(tile[:, -1], neighbor_tile[:, 0]):
                    grid[y, x + 1] = neighbor, neighbor_tile
                    break
        G.remove_node(node)

    image = np.block([[grid[y, x][1][1:-1, 1:-1] for x in range(12)]
                      for y in range(12)])

    lochness = np.array([[char == "#" for char in line]
                         for line in "                  #  \n"
                         "#    ##    ##    ### \n"
                         " #  #  #  #  #  #    \n".splitlines()])
    for lochness in orientations(lochness):
        if count := (convolve(image, lochness, output=int,
                              mode="constant") == lochness.sum()).sum():
            return image.sum() - count * lochness.sum()


raw = aoc_helper.day(20)
tiles = parse_raw()
border_to_tiles, G = build_graph()

aoc_helper.submit(20, part_one)
aoc_helper.submit(20, part_two)
Exemple #2
0
import aoc_helper
import re

FIELDS = "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"

raw = aoc_helper.day(4)
passports = [
    dict(matches) for line in raw.split("\n\n") if len(
        matches := re.findall(r"(?!cid)([a-z]{3}):(\S+)", line)) == len(FIELDS)
]


def part_one():
    return len(passports)


def part_two():
    RES = (
        r"19[2-9][\d]|200[0-2]",  # byr
        r"20(1[\d]|20)",  # iyr
        r"20(2[\d]|30)",  # eyr
        r"1([5-8][\d]|9[0-3])cm|(59|6[\d]|7[0-6])in",  # hgt
        r"#[0-9a-f]{6}",  # hcl
        r"amb|blu|brn|gr(y|n)|hzl|oth",  # ecl
        r"\d{9}",  # pid
    )
    return sum(
        all(map(bool, map(re.fullmatch, RES, map(pp.__getitem__, FIELDS))))
        for pp in passports)

Exemple #3
0
import aoc_helper

raw = aoc_helper.day(15)
data = list(aoc_helper.extract_ints(raw))


def van_eck(nth):
    memory = {}
    it = iter(data)
    last = next(it)
    for i, n in enumerate(it, start=1):
        memory[last], last = i, n

    for i in range(len(data), nth):
        memory[last], last = i, i - memory[last] if last in memory else 0
    return last


def part_one():
    return van_eck(2020)


def part_two():
    return van_eck(30_000_000)


aoc_helper.submit(15, part_one)
aoc_helper.submit(15, part_two)
Exemple #4
0
import itertools

import aoc_helper


def parse_raw(raw):
    return list(map(int, raw.split('\n')))


def part_one():
    a, b = next(
        filter(lambda x: sum(x) == 2020, itertools.combinations(data, 2)))
    return a * b


def part_two():
    a, b, c = next(
        filter(lambda x: sum(x) == 2020, itertools.combinations(data, 3)))
    return a * b * c


data = parse_raw(aoc_helper.day(1))

aoc_helper.submit(day=1, solv_func=part_one)
aoc_helper.submit(day=1, solv_func=part_two)