Beispiel #1
0
def part2():
    with ManyLineInput('./input.txt', Ingredient) as data:
        answer = max([
            product2(list(zip(data, partition)))
            for partition in partitions(len(data), 100)
        ])
        print(f"part 2: {answer}")
Beispiel #2
0
def part1():
    with ManyLineInput('./input.txt', PasswordPolicy) as data:
        answer = len(
            list(
                filter(
                    lambda p: is_in_range(
                        Counter(p.password)[p.letter], p.lo, p.hi), data)))
        print(f"part 1: {answer}")
Beispiel #3
0
def part1():
    with ManyLineInput('./input.txt') as data:
        numbers = [SFParser().parse(line) for line in data]

        n = numbers[0]
        for other in numbers[1:]:
            n.add(other)
        print(f"part 1: {n.magnitude()}")
Beispiel #4
0
def part2():
    with ManyLineInput('./input.txt', Reindeer) as data:
        winners = [
            winner for t in range(1, 2503 + 2)
            for winner in Race(data, t).winners()
        ]
        c = Counter(winners)
        answer = max(c.values())
        print(f"part 2: {answer}")
Beispiel #5
0
def part1():
    with ManyLineInput('./input.txt', int) as data:
        socket = 0
        inbuilt = max(data) + 3
        counter = collections.Counter(
            map(lambda w: w[1] - w[0],
                windowed(sorted(data + [socket, inbuilt]), 2)))
        answer = counter[1] * counter[3]
        print(f"part 1: {answer}")
Beispiel #6
0
def part1():
    with ManyLineInput('./input.txt', BagRule) as data:
        results = dict()
        rules = {rule.container: rule for rule in data}
        for rule in data:
            is_in_gold(rule, results, rules)
        print(
            f"part 1: {len([result for result in results.values() if result is True])}"
        )
Beispiel #7
0
def part2():
    with ManyLineInput('./input.txt') as data:
        game_state = set()
        for y, row in enumerate(data):
            for x, c in enumerate(row):
                if c == ACTIVE:
                    game_state.add((x, y, 0, 0))
        for s in range(6):
            step2(game_state)
        print(f"part 2: {len(game_state)}")
Beispiel #8
0
def part1():
    with ManyLineInput('./input.txt', Instruction) as data:
        mask = None
        mem = dict()
        for i in data:
            if i.type == 'mem':
                mem[i.addr] = mask_val(mask, bits(i.val))
            else:
                mask = i.mask
        print(f"part 1: {sum(mem.values())}")
Beispiel #9
0
def part1():
    with ManyLineInput('./input.txt', Food) as data:
        all_ingredients = {i for f in data for i in f.ingredients}
        allergen_candidates = get_candidate_map(data)

        maybe_dangerous = {i for l in allergen_candidates.values() for i in l}
        safe = all_ingredients - maybe_dangerous
        answer = sum(
            len(list(filter(lambda i: i in safe, f.ingredients)))
            for f in data)
        print(f"part 1: {answer}")
Beispiel #10
0
def part2():
    with ManyLineInput('./input.txt', Distance) as data:
        distances = {d.locations: d.distance for d in data}
        locations = reduce(lambda s, d: set(d.locations).union(s), data, set())
        answer = max(
            map(
                lambda selection: sum(
                    map(lambda w: distances[tuple(sorted(w))],
                        windowed(selection, 2))),
                permutations(locations, len(locations))))
        print(f"part 2: {answer}")
Beispiel #11
0
def part2():
    with ManyLineInput('./input.txt', Instruction) as data:
        mask = None
        mem = dict()
        for i in data:
            if i.type == 'mem':
                for addr in mask_val_2(mask, bits(i.addr)):
                    mem[addr] = int(i.val)
            else:
                mask = i.mask
        print(f"part 2: {sum(mem.values())}")
Beispiel #12
0
def part1():
    with ManyLineInput('./input.txt') as data:
        risks = {(x, y): int(data[y][x])
                 for x in range(len(data[0])) for y in range(len(data))}
        height = len(data)
        width = len(data[0])
        explorer = Explorer(risks, height, width)

    best_risk = explorer.explore(explorer.width - 1, explorer.height - 1)

    print(f"part 1: {best_risk}")
Beispiel #13
0
def part2():
    with ManyLineInput('./input.txt', int) as data:
        answer = None
        target = first_invalid(data)
        for start_ix in range(len(data)):
            sums = [a for a in accumulate(data[start_ix:]) if a <= target]
            if len(sums) and sums[-1] == target:
                nums = data[start_ix:start_ix + len(sums)]
                answer = max(nums) + min(nums)
                break
        print(f"part 2: {answer}")
Beispiel #14
0
def part2():
    with ManyLineInput('./input.txt') as data:
        max_magnitude = 0
        for ix1 in range(len(data)):
            for ix2 in range(len(data)):
                if ix1 != ix2:
                    n1 = SFParser().parse(data[ix1])
                    n2 = SFParser().parse(data[ix2])
                    n1.add(n2)
                    max_magnitude = max(n1.magnitude(), max_magnitude)

        print(f"part 2: {max_magnitude}")
Beispiel #15
0
def part2():
    with ManyLineInput('./input.txt', Inst) as program:
        answer = None
        for ix, inst in enumerate(program):
            if inst.name in ['jmp', 'nop']:
                machine = Machine()
                prog = [Inst(f"{i.name} {i.arg}") for i in program]
                if inst.name == 'jmp':
                    prog[ix].name = 'nop'
                elif inst.name == 'nop':
                    prog[ix].name = 'jmp'
                machine.execute(prog)
                if machine.normal:
                    answer = machine.acc
                    break

    answer = answer or "nothing terminated normally"
    print(f"part 2: {answer}")
Beispiel #16
0
def part2():
    with ManyLineInput('./input.txt', Food) as data:
        candidates = get_candidate_map(data)
        identified = {}
        while candidates:
            for allergen, ingredients in candidates.items():
                possibles = [i for i in ingredients if i not in identified]
                if len(possibles) == 1:
                    found_a = allergen
                    found_i = possibles[0]
                    break

            candidates.pop(found_a)
            identified[found_i] = found_a

        answer = ','.join(
            i for i, _ in sorted(identified.items(), key=lambda m: m[1]))
        print(f"part 2: {answer}")
Beispiel #17
0
def part2():
    with ManyLineInput('./input.txt', int) as data:
        socket = 0
        inbuilt = max(data) + 3
        data = list(sorted(data + [socket, inbuilt]))
        dag = {}
        for n in data:
            dag[n] = []
            for m in range(1, 4):
                if n + m in data:
                    dag[n].append(n + m)

        routes_to_end = {}
        for k in data[::-1]:
            routes_to_end[k] = max(1, sum(routes_to_end[i] for i in dag[k]))

        answer = routes_to_end[0]
        print(f"part 2: {answer}")
Beispiel #18
0
def part2():
    extended = []
    with ManyLineInput('./input.txt') as data:
        for j in range(5):
            for y in range(len(data)):
                line = []
                for i in range(5):
                    for x in range(len(data[0])):
                        val = int(data[y][x]) + i + j
                        while val > 9:
                            val -= 9
                        line.append(val)
                extended.append(line)

    risks = {(x, y): int(extended[y][x])
             for x in range(len(extended[0])) for y in range(len(extended))}
    explorer = Explorer(risks, len(extended), len(extended[0]))

    best_risk = explorer.explore(explorer.width - 1, explorer.height - 1)

    print(f"part 2: {best_risk}")
Beispiel #19
0
def part1():
    with ManyLineInput('./input.txt', int) as data:
        answer = first_invalid(data)
        print(f"part 1: {answer}")
Beispiel #20
0
def part2():
    with ManyLineInput('./input.txt') as data:
        answer = sum(evaluate2(parse(expr.replace(" ", ""))) for expr in data)
        print(f"part 2: {answer}")
Beispiel #21
0
def part2():
    with ManyLineInput('./input.txt', int) as data:
        print(f"part 2: {solve(data, 3)}")
Beispiel #22
0
def part2():
    with ManyLineInput('./input.txt', BoardingPass) as data:
        seat_ids = {bp.seat_id() for bp in data}
        answer = set(range(min(seat_ids), max(seat_ids))) - seat_ids
        print(f"part 2: {list(answer)[0]}")
Beispiel #23
0
def part2():
    with ManyLineInput('./input.txt', PasswordPolicy) as data:
        answer = len(list(filter(lambda p: condition2(p), data)))
        print(f"part 2: {answer}")
Beispiel #24
0
def part2():
    with ManyLineInput('./input.txt', Gate) as data:
        round1 = process_gates(data)
        round2 = process_gates(data, {'b': round1})
        print(f"part 2: {round2}")
Beispiel #25
0
def part2():
    with ManyLineInput('./input.txt', LightInstruction) as data:
        grid = Grid2()
        for instruction in data:
            grid.apply(instruction)
        print(f"part 2: {grid.count_on()}")
Beispiel #26
0
def part1():
    with ManyLineInput('./input.txt', Gate) as data:
        print(f"part 1: {process_gates(data)}")
Beispiel #27
0
def part2():
    with ManyLineInput('./input.txt', Instruction) as data:
        x, y, dx, dy = reduce(apply_instruction_2, data, (0, 0, 10, 1))
        answer = manhattan(x, y)
        print(f"part 2: {answer}")
Beispiel #28
0
def part1():
    with ManyLineInput('./input.txt', Instruction) as data:
        x, y, d = reduce(apply_instruction, data, (0, 0, Direction.East))
        answer = manhattan(x, y)
        print(f"part 1: {answer}")
Beispiel #29
0
def part2():
    with ManyLineInput('./input.txt') as data:
        answer = ''
        print(f"part 2: {answer}")
Beispiel #30
0
def part1():
    with ManyLineInput('./input.txt', Aunt) as data:
        answer = next(filter(props_match, data))
        print(f"part 1: {answer}")