示例#1
0
def problem2_solution():
    data = tuple(map(int, load_input(9, 2)))
    for y in range(2, len(data) + 1):
        for x in range(y, len(data) + 1):
            if sum(data[x - y:x]) == P1_SOL:
                return min(data[x - y:x]) + max(data[x - y:x])
    return "Solution not found"
示例#2
0
def problem2_solution():
    count = 0
    for i1, i2, char, pword in tuple(map(get_params, load_input(2, 2))):
        char = char[0]
        if (char == pword[i1 - 1]) != (char == pword[i2 - 1]):
            count += 1
    return count or "Solution not found."
示例#3
0
def problem2_solution():
    data = tuple(map(int, load_input(1, 2)))
    for num1, num2 in combinations(data, 2):
        num3 = 2020 - num1 - num2
        if num3 in data:
            return num1 * num2 * num3
    return "Solution not found."
示例#4
0
def problem1_solution():
    data = tuple(map(int, load_input(1, 1)))
    for num1 in data:
        num2 = 2020 - num1
        if num2 in data:
            return num1 * num2
    return "Solution not found."
示例#5
0
def problem2_solution():
    seat_ids = [compute_seat_id(t.strip()) for t in load_input(5, 2)]
    seat_ids.sort()
    for x in range(len(seat_ids) - 1):
        if seat_ids[x + 1] - seat_ids[x] == 2:
            return seat_ids[x] + 1
    return "No solution found"
示例#6
0
def problem1_solution():
    data = tuple(map(int, load_input(9, 1)))
    for x in range(25, len(data) + 1):
        curr_num = data[x]
        previous_nums = data[x - 25:x]
        if not any(abs(curr_num - p) in previous_nums for p in previous_nums):
            global P1_SOL
            P1_SOL = curr_num
            return curr_num
    return "Solution not found"
示例#7
0
def problem1_solution():
    raw_rules, _, other_t = ''.join(load_input(16, 1)).split('\n\n')
    rules = get_rules(raw_rules)
    other_tickets = [get_ticket(t) for t in other_t.split('\n')[1:]]
    ticket_error_rate = 0
    for ticket in other_tickets:
        _, invalid_values = split_ticket_values(ticket, rules)
        if invalid_values:
            ticket_error_rate += sum(invalid_values)
    return ticket_error_rate
示例#8
0
def problem1_solution():
    data = load_input(13, 1)
    ts = int(data[0])
    bus_ids = [int(i) for i in data[1].strip().split(',') if i != 'x']
    earliest_bus_id = 0
    min_wait_time = min(bus_ids)
    for bus_id in bus_ids:
        wait_time = bus_id - divmod(ts, bus_id)[1]
        if wait_time <= min_wait_time:
            min_wait_time = wait_time
            earliest_bus_id = bus_id
    return earliest_bus_id * min_wait_time
示例#9
0
def problem2_solution():
    prog = {n: parse_instruction(i) for n, i in enumerate(load_input(8, 2), 1)}
    for num, (op, val) in prog.items():
        op = {'nop': 'jmp', 'jmp': 'nop'}.get(op)
        if not op:
            continue
        patched_instructions = prog.copy()
        patched_instructions[num] = (op, val)
        console = Console(patched_instructions)
        console.run()
        if console.stop == 100:
            return console.accumulator
    return "Solution not found"
示例#10
0
def problem1_solution():
    mask = ''
    mem = {}
    for line in map(lambda x: x.strip(), load_input(14, 1)):
        if line.startswith('mask'):
            mask = line.replace('mask = ', '')
            continue
        # Retrieve address and value in base 10
        addr, decval = line.replace('mem[', '').replace(']', '').split(' = ')
        # Convert value to base 2, fill chars to make its length reach 36
        binval = decimalstr_to_binarystr(decval).zfill(36)
        # Apply mask rules
        newbinval = ''.join(b if m == 'X' else m for b, m in zip(binval, mask))
        # Convert back to base 10, set in memory to its address
        mem[addr] = binarystr_to_decimalstr(newbinval)
    return sum([int(v) for v in mem.values()])
示例#11
0
def problem2_solution():
    raw_rules, my_t, other_t = ''.join(load_input(16, 1)).split('\n\n')
    rules = get_rules(raw_rules)
    my_ticket = get_ticket(my_t.split('\n')[1])
    other_tickets = [get_ticket(t) for t in other_t.split('\n')[1:]]
    valid_tickets = []
    for ticket in other_tickets:
        _, invalid_values = split_ticket_values(ticket, rules)
        if not invalid_values:
            valid_tickets.append(ticket)
    fields_position = get_fields_position(rules, valid_tickets)
    result = 1
    for field, pos in fields_position.items():
        if field.startswith('departure'):
            result *= my_ticket[pos]
    return result
示例#12
0
def problem2_solution():
    data = load_input(13, 2)
    bus_ids = [int(i) if i != 'x' else i for i in data[1].strip().split(',')]
    bus_ids_delta = []  # List of tuples made as (Bus ID, offset)
    for n, bus_id in tuple(filter(lambda i: i[1] != 'x', enumerate(bus_ids))):
        if n == 0:
            bus_ids_delta.append((bus_id, 0))
        else:
            bus_ids_delta.append((bus_id, bus_id - (n % bus_id)))
    # Going to use the LCM: sort the deltas so that we start right away with
    # big numbers, instead of cycling through small ones
    bus_ids_delta.sort(key=lambda i: i[1], reverse=True)
    lcm = 1
    ts = bus_ids_delta[0][1]
    for n, delta in enumerate(bus_ids_delta[:-1]):
        lcm *= delta[0]
        while ts % bus_ids_delta[n + 1][0] != bus_ids_delta[n + 1][1]:
            ts += lcm
    return ts
示例#13
0
def problem2_solution():
    mask = ''
    mem = {}
    for line in map(lambda x: x.strip(), load_input(14, 2)):
        if line.startswith('mask'):
            mask = line.replace('mask = ', '')
            continue
        # Retrieve address and value in base 10
        addr, decval = line.replace('mem[', '').replace(']', '').split(' = ')
        # Convert address to base 2, fill chars to make its length reach 36
        binaddr = decimalstr_to_binarystr(addr).zfill(36)
        # Apply mask rules
        maskedaddr = ''.join(b if m == '0' else m if m == '1' else 'X'
                             for b, m in zip(binaddr, mask))
        # For each X in the masked address, apply once 0 and once 1 (so, for 2
        # Xs, we'll have 4 possible substitutions: (0, 0), (0, 1), (1, 0),
        # (1, 1), giving 4 different addresses)
        for bits in product(*[(0, 1) for _ in range(maskedaddr.count('X'))]):
            newaddr = maskedaddr.replace('X', '{}').format(*bits)
            # Convert back to base 10, set its value in memory
            mem[binarystr_to_decimalstr(newaddr)] = decval
    return sum([int(v) for v in mem.values()])
示例#14
0
def problem1_solution():
    return max([compute_seat_id(t.strip()) for t in load_input(5, 1)])
示例#15
0
def problem1_solution():
    expressions = [''.join(e.split()) for e in load_input(18, 1)]
    return sum([evaluate_expr(e) for e in expressions])
示例#16
0
def problem2_solution():
    expressions = [''.join(e.split()) for e in load_input(18, 2)]
    return sum([evaluate_expr(e, 'add_first') for e in expressions])
示例#17
0
def problem2_solution():
    data = dict(enumerate(map(lambda l: l.strip(), load_input(3, 2))))
    res = 1
    for x, y in [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]:
        res *= tree_counter(data, x=x, y=y)
    return res or "Solution not found."
示例#18
0
def problem1_solution():
    data = dict(enumerate(map(lambda l: l.strip(), load_input(3, 1))))
    return tree_counter(data, x=3, y=1) or "Solution not found."
示例#19
0
def problem2_solution():
    policies = tuple(map(lambda x: x.strip(), load_input(7, 2)))
    cmap = map_policies_to_colors(policies)
    return count_inner_colors(cmap, MY_COLOR)
示例#20
0
def problem2_solution():
    passports = convert2passports(load_input(4, 2))
    return len(tuple(filter(has_all_valid_fields, passports)))
示例#21
0
def problem2_solution():
    return get_arrangements(list(map(int, load_input(10, 2))))
示例#22
0
def problem1_solution():
    count = 0
    for n1, n2, char, pword in tuple(map(get_params, load_input(2, 1))):
        if n1 <= Counter(pword).get(char, 0) <= n2:
            count += 1
    return count or "Solution not found."
示例#23
0
def problem1_solution():
    return memory(list(map(int, load_input(15, 1)[0].split(','))), 2020)
示例#24
0
def problem2_solution():
    old_map = {}
    new_map = get_seat_map(load_input(11, 2))
    while old_map != new_map:
        old_map, new_map = new_map, evolve_map(new_map, 'first_seen')
    return len([s for s in new_map.values() if s['type'] == '#'])
示例#25
0
def problem1_solution():
    policies = tuple(map(lambda x: x.strip(), load_input(7, 1)))
    cmap = map_policies_to_colors(policies)
    return len([c for c in cmap if contains_color(cmap, c, MY_COLOR)])
示例#26
0
def problem1_solution():
    prog = {n: parse_instruction(i) for n, i in enumerate(load_input(8, 1), 1)}
    console = Console(prog)
    console.run()
    return console.accumulator if console.stop == 300 else "Solution not found"
示例#27
0
def problem1_solution():
    jumps = get_jumps(list(map(int, load_input(10, 1))))
    return jumps[1] * jumps[3]
示例#28
0
def problem2_solution():
    return memory(list(map(int, load_input(15, 2)[0].split(','))), 30000000)
示例#29
0
def problem1_solution():
    data = load_input(19, 1)
    rules = dictify_rules(data[:data.index('\n')])
    valid_msgs = set(get_valid_msgs(rules))
    msgs = [m.strip() for m in data[data.index('\n') + 1:]]
    return len(valid_msgs.intersection(msgs))
示例#30
0
def problem1_solution():
    passports = convert2passports(load_input(4, 1))
    return len(tuple(filter(has_all_mandatory_fields, passports)))