Exemplo n.º 1
0
        return grid


def find_prefix_fields(grid, prefix, restrictions, your_ticket):
    prefix_fields = []
    for i, key in enumerate(restrictions):
        if key.startswith(prefix):
            field_idx = np.where(grid[i] == 1)[0][0]
            prefix_fields.append(your_ticket[field_idx])
    return prefix_fields


def compute2(data, prefix):
    restrictions, your_ticket, nearby_tickets = parse(data)
    _, valid_tickets = validate_tickets(nearby_tickets, restrictions)
    grid = build_sudoku(restrictions, valid_tickets)
    grid = solve_sudoku(grid)
    prefix_fields = find_prefix_fields(grid, prefix, restrictions, your_ticket)
    return reduce(mul, prefix_fields, 1)


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

    test_solution(compute1, test_data, 71)
    test_solution(compute1, data, 24110)
    test_solution(compute2, test_data_b, 11, options='r')
    test_solution(compute2, data, 6766503490793, options='departure')
Exemplo n.º 2
0
        for option in rule:
            partial_match, remainder = matches_part(message, rules, option)
            if partial_match:
                return True, remainder
        return False, ''


def compute1(raw_data):
    rules, messages = parse(raw_data)
    matching = []
    for message in messages:
        match, remainder = matches(message, rules, rules[0])
        if match and not remainder:
            matching.append(message)
    print(matching)
    return len(matching)


def compute2(records):
    return None


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

    test_solution(compute1, test_data, 2)
    test_solution(compute1, data, 178)
    # test_solution(compute2, test_data, 32)
    # test_solution(compute2, data, 5635)
Exemplo n.º 3
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)