Beispiel #1
0
    return accumulator


def swap_op(op, val):
    return ('nop', val) if op == 'jmp' else ('jmp', val)


def compute2(ops):
    for idx, (op, val) in enumerate(ops):
        if op != 'acc':
            new_ops = list(ops)
            new_ops[idx] = swap_op(op, val)
            accumulator, terminate = run_program(new_ops)
            if terminate:
                return accumulator
    assert False


if __name__ == '__main__':

    def transform(line):
        first, second = line.split()
        return first, int(second)

    test_data = single_line_records(__file__, tf=transform, test=True)
    data = single_line_records(__file__, tf=transform)
    test_solution(compute1, test_data, 5)
    test_solution(compute1, data, 1394)
    test_solution(compute2, test_data, 8)
    test_solution(compute2, data, 1626)
Beispiel #2
0
from functools import reduce
from operator import mul

from aoc.util import single_line_records, test_solution


def compute1(nums):
    return next(((n1, n2) for i1, n1 in enumerate(nums) for i2, n2 in enumerate(nums) if i1 < i2 and n1 + n2 == 2020))


def compute2(nums):
    return next(((n1, n2, n3) for i1, n1 in enumerate(nums) for i2, n2 in enumerate(nums) for i3, n3 in enumerate(nums) if i1 < i2 < i3 and n1 + n2 + n3 == 2020))


def compute(nums, f):
    return reduce(mul, f(nums), 1)


if __name__ == '__main__':
    test_data = single_line_records(__file__, tf=int, test=True)
    data = single_line_records(__file__, tf=int)

    test_solution(lambda records: compute(data, compute1), test_data, 514579)
    test_solution(lambda records: compute(data, compute1), data, 138379)
    test_solution(lambda records: compute(data, compute2), test_data, 241861950)
    test_solution(lambda records: compute(data, compute2), data, 85491920)
Beispiel #3
0
    sorted_adapters = sorted(records)
    previous = 0
    ones = 0
    threes = 1
    for adapter in sorted_adapters:
        if adapter - previous == 1:
            ones += 1
        elif adapter - previous == 3:
            threes += 1
        previous = adapter
    return ones * threes


def compute2(records):
    essential_segments = find_essential_segments(records)
    return reduce(mul,
                  [segment_partition_count(s) for s in essential_segments], 1)


if __name__ == '__main__':
    test_data = single_line_records(__file__, int, test=True)
    test_data_b = single_line_records(__file__, int, test=True, version='b')
    data = single_line_records(__file__, int)

    test_solution(compute1, test_data, 35)
    test_solution(compute1, test_data_b, 220)
    test_solution(compute1, data, 2244)
    test_solution(compute2, test_data, 8)
    test_solution(compute2, test_data_b, 19208)
    test_solution(compute2, data, 3947645370368)
Beispiel #4
0
        'ecl': lambda s: re.match(r'^(amb|blu|brn|gry|grn|hzl|oth)$', s),
        'pid': lambda s: re.match(r'^[0-9]{9}$', s)
    }
    for k in validations:
        if k not in doc or not validations[k](doc[k]):
            return False
    return True


def compute1(records):
    key_vals = parse2(records)
    docs = parse3(key_vals)
    return len([d for d in docs if valid(d)])


def compute2(records):
    key_vals = parse2(records)
    docs = parse3(key_vals)
    return len([d for d in docs if valid2(d)])


if __name__ == '__main__':
    test_data = multiple_line_records(__file__, test=True)
    test_data_b = multiple_line_records(__file__, test=True, version='b')
    data = multiple_line_records(__file__)

    test_solution(compute1, test_data, 2)
    test_solution(compute1, data, 228)
    test_solution(compute2, test_data_b, 4)
    test_solution(compute2, data, 175)
Beispiel #5
0
        if parent == bag:
            val = graph[(parent, child)]
            descendants += val
            descendants += val * count_descendants(child, graph)
    return descendants


def compute1(records):
    rule_dict = parse_rules(records)
    graph = build_graph(rule_dict)
    my_bag = 'shiny gold'
    return len(find_ancestors(my_bag, graph))


def compute2(records):
    rule_dict = parse_rules(records)
    graph = build_graph(rule_dict)
    my_bag = 'shiny gold'
    return count_descendants(my_bag, graph)


if __name__ == '__main__':
    pattern = r'^(.+) bags contain (.+)\.$'
    test_data = regex_parse_input(__file__, pattern, test=True)
    data = regex_parse_input(__file__, pattern)

    test_solution(compute1, test_data, 4)
    test_solution(compute1, data, 326)
    test_solution(compute2, test_data, 32)
    test_solution(compute2, data, 5635)