Example #1
0
def solve_part1(file_name):
    tiles = _read_tiles(file_name)
    _, tile_nums = next(_get_tile_positions(tiles))
    n2 = len(tiles)
    n = int(sqrt(n2))
    return multiply(
        map(lambda i: tiles[tile_nums[i]][0], (0, n - 1, n2 - n, n2 - 1)))
Example #2
0
def solve_part2(file_name):
    diffs = tuple(_read_and_compute_diffs(file_name))
    all_n_combos_less_than_3 = []
    i = 0
    while i < len(diffs):
        j = i + 1
        while j < len(diffs) and diffs[j] != 3:
            j += 1
        all_n_combos_less_than_3.append(n_combos_less_than_3(diffs[i:j]))
        i = j
    return multiply(all_n_combos_less_than_3)
Example #3
0
def solve_part2(file_name, prefix):
    your_ticket, nearby_tickets, rules = _read_and_parse_data(file_name)

    drop_tickets = []
    for ticket_num, ticket in enumerate(nearby_tickets):
        for value in ticket:
            for rule in rules:
                if _is_within_limits(value, rule):
                    break
            else:
                drop_tickets.append(ticket_num)

    nearby_tickets = [
        t for i, t in enumerate(nearby_tickets) if i not in drop_tickets
    ]

    n = len(rules)
    all_valid = np.zeros((n, n))
    for pos in range(n):
        for rule_num, rule in enumerate(rules):
            all_valid[pos][rule_num] = all(
                _is_within_limits(ticket[pos], rule)
                for ticket in nearby_tickets)
    valid_in_pos = all_valid.sum(axis=0)

    positions = [None] * n
    for _ in range(n):
        i = valid_in_pos.argmin()
        j = np.argmax(all_valid[:, i])
        valid_in_pos[i] = np.inf
        valid_in_pos -= all_valid[j]
        all_valid[j] = -1
        positions[j] = i

    prod = []
    for pos, rule_num in enumerate(positions):
        if rules[rule_num][0].startswith(prefix):
            prod.append(your_ticket[pos])

    return multiply(prod)
Example #4
0
def chinese_remainder(modulus, remainders):
    prod = multiply(modulus)
    residual = 0
    for m, r in zip(modulus, remainders):
        residual += r * mul_inv(p := prod // m, m) * p
    return residual % prod
Example #5
0
def solve_part2(file_name):
    wood = read_line_separated_list(file_name)
    return multiply([
        _count_trees(wood, s) for s in [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)]
    ])
Example #6
0
def product_where_sum_of_combo_equals_n(combo_length, n, file_name):
    values = read_line_separated_list(file_name, int)
    return multiply(
        first_true(combinations(values, combo_length),
                   pred=lambda c: sum(c) == n))
Example #7
0
def _mul(nums_str):
    return str(multiply(map(int, nums_str.split(" * "))))