Beispiel #1
0
def parse_raw():
    rules, my_ticket, nearby_tickets = raw.split("\n\n")

    fields = {
        field: Range(f"[{p}, {q}]") | Range(f"[{r}, {s}]")
        for field, p, q, r, s in re.findall(
            r"(.+): (\d+)-(\d+) or (\d+)-(\d+)", rules)
    }
    my_ticket = list(aoc_helper.extract_ints(my_ticket))
    nearby_tickets = [
        list(aoc_helper.extract_ints(line))
        for line in nearby_tickets.splitlines()[1:]
    ]

    return fields, my_ticket, nearby_tickets
Beispiel #2
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)
Beispiel #3
0
def parse_raw():
    p1, p2 = raw.split("\n\n")
    return aoc_helper.extract_ints(p1)[1:], aoc_helper.extract_ints(p2)[1:]
Beispiel #4
0
import aoc_helper

raw = aoc_helper.day(25)
card_key, door_key = aoc_helper.extract_ints(raw)
MOD = 20201227


def part_one():
    for n in range(1, MOD):
        if (s := pow(7, n, MOD)) == card_key:
            return pow(door_key, n, MOD)
        elif s == door_key:
            return pow(card_key, n, MOD)


aoc_helper.submit(25, part_one)
Beispiel #5
0
import aoc_helper
from itertools import combinations

raw = aoc_helper.day(1)
data = set(aoc_helper.extract_ints(raw))

def part_one():
    for a in data:
        if (b := (2020 - a)) in data:
            return a * b

def part_two():
    # We know that at least two of the numbers must be less than half the target.
    # Note len(filtered_data) is 5 for my input.  We only need to check 10 combinations!
    filtered_data = (i for i in data if i < 1010)

    for a, b in combinations(filtered_data, 2):
        if (c := (2020 - a - b)) in data:
            return a * b * c

aoc_helper.submit(1, part_one)
aoc_helper.submit(1, part_two)
Beispiel #6
0
def parse_raw():
    jolts = list(aoc_helper.extract_ints(raw))
    jolts += 0, max(jolts) + 3
    return sorted(jolts)
Beispiel #7
0
def parse_raw():
    return aoc_helper.extract_ints(raw)
Beispiel #8
0
def parse_raw():
    return sorted(aoc_helper.extract_ints(raw))