Beispiel #1
0
def part2():
    bags = {}
    for line in inputs:
        root, *children = re.split(DELIMETER, line)
        bags[root] = children

    return utility.solution({'count': count_bags(bags, TARGET)})
Beispiel #2
0
def part1():
    executed = set()
    bootcode = Interpreter(inputs) \
        .inject(custom, executed) \
        .run()

    return utility.solution({'accumulator': bootcode.accumulator}, test=5)
Beispiel #3
0
def part2():
    slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]

    product = 1
    for x, y in slopes:
        product *= check_slope(grid, x, y)

    return utility.solution({'product': product})
Beispiel #4
0
def part1():
    """Find two entries in the input file that add to 2020, report the product."""
    for e1 in inputs:
        for e2 in inputs:
            if e1 + e2 == 2020:
                return utility.solution({
                    'product': e1 * e2,
                    'numbers': (e1, e2)
                })
Beispiel #5
0
def part2():
    """Find three entries in the input file that add to 2020, report the product."""
    for e1 in inputs:
        for e2 in inputs:
            for e3 in inputs:
                if e1 + e2 + e3 == 2020:
                    return utility.solution({
                        'product': e1 * e2 * e3,
                        'numbers': (e1, e2, e3)
                    })
Beispiel #6
0
def part1():
    valid = 0
    for bound, letter, password in inputs:
        count = password.count(letter[0])

        lower, upper = [int(b) for b in bound.split('-')]

        if count >= lower and count <= upper:
            valid += 1

    return utility.solution({'valid': valid})
Beispiel #7
0
def part2():
    valid = 0
    for bound, letter, password in inputs:
        lower, upper = [int(b) for b in bound.split('-')]

        lp, up = password[lower - 1], password[upper - 1]

        if (lp == letter[0]) ^ (up == letter[0]):
            valid += 1

    return utility.solution({'valid': valid})
Beispiel #8
0
def part1():
    tree = {}
    for line in inputs:
        root, *children = re.split(DELIMETER, line)

        for child in children:
            if child == 'no other': continue
            key = child[2:]
            tree[key] = [*tree.get(key, []), root]

    return utility.solution({'bags': traverse(tree, TARGET)}, test=4)
Beispiel #9
0
def part2():
    records = parse()
    valid = 0
    for record in records:
        is_super = set(record.keys()) >= REQUIRED

        if is_super and all(
                validator(record[field])
                for field, validator in FIELDS.items()):
            valid += 1

    return utility.solution({'valid': valid})
Beispiel #10
0
def part2():
    seat_id = lambda x, y: x * 8 + y

    seats = [seat_id(*binary_partition(ticket)) for ticket in inputs]

    seats.sort()

    missing = [
        seats[i] + 1 for i in range(len(seats) - 1)
        if seats[i] + 1 != seats[i + 1]
    ]

    return utility.solution({'missing': max(missing)})
Beispiel #11
0
def part2():
    start = 0
    while True:
        idx, total, walk = start, 0, []
        while total < SUM_TO:
            total += inputs[idx]
            walk.append(inputs[idx])

            if total == SUM_TO:
                return utility.solution({'val': min(walk) + max(walk)},
                                        test=None)

            idx += 1
        start += 1
Beispiel #12
0
def part1():
    idx = BUFFER
    while idx < len(inputs):
        preamble = inputs[idx - BUFFER:idx]
        cmp = inputs[idx]

        found = False
        for x in preamble:
            if (cmp - x) in preamble:
                found = True

        if not found:
            return utility.solution({'val': cmp}, test=127)
        idx += 1
Beispiel #13
0
def part1():
    jolts = sorted([*inputs])

    one_jolt, three_jolt, prev = 0, 0, 0
    for jolt in jolts:
        diff = jolt - prev

        if diff == 1:
            one_jolt += 1
        elif diff == 3:
            three_jolt += 1

        prev = jolt

    return utility.solution({'sol': one_jolt * (three_jolt + 1)}, test=220)
Beispiel #14
0
def part2():
    jolts = sorted([0, *inputs, max(inputs) + 3])
    MAX = len(jolts)

    def cmp(i, j):
        return jolts[j] - jolts[i] <= 3

    @lru_cache
    def arrange(i):
        if i == MAX - 1:
            return 1

        return sum(
            [arrange(j) for j in range(i + 1, min(i + 4, MAX)) if cmp(i, j)])

    return utility.solution({'sol': arrange(0)}, test=None)
Beispiel #15
0
def part2():
    idx, found = 0, False
    while not found:
        temps, executed = [*inputs], set()

        name, _ = temps[idx].split()
        _from, _to = ('jmp', 'nop') if name == 'jmp' else ('nop', 'jmp')

        temps[idx] = temps[idx].replace(_from, _to)

        bootcode = Interpreter(temps) \
            .inject(custom, executed) \
            .run()

        if not bootcode.suspend:
            found = True

        idx += 1

    return utility.solution({'accumulator': bootcode.accumulator})
Beispiel #16
0
def part1():
    records = parse()
    valid = sum(1 for record in records if set(record.keys()) >= REQUIRED)

    return utility.solution({'valid': valid}, test=2)
Beispiel #17
0
def part1():
    trees = check_slope(grid, 3, 1)

    return utility.solution({'trees': trees})
Beispiel #18
0
def part1():
    seat_id = lambda x, y: x * 8 + y

    seats = [seat_id(*binary_partition(ticket)) for ticket in inputs]

    return utility.solution({'max_seat': max(seats)}, test=820)
Beispiel #19
0
def part2():
    answers = map(len, map(distinct_group, groups))

    return utility.solution({'sum': sum(answers)}, test=6)