示例#1
0
def main():
    lines = read_input_file("14.txt")
    value = Interpreter(version=1).interpret(lines)
    print("Part 1: the sum of all masked memory values is {}".format(value))

    value = Interpreter(version=2).interpret(lines)
    print("Part 2: the sum of all masked memory values is {}".format(value))
示例#2
0
def main():
    lines = read_input_file("4.txt")
    passports = gather_passports(lines)
    valid_passports = [passport for passport in passports if is_valid_v1(passport)]
    print("Part 1: found {} valid passports".format(len(valid_passports)))

    valid_passports = [passport for passport in passports if is_valid_v2(passport)]
    print("Part 2: found {} valid passports".format(len(valid_passports)))
示例#3
0
def main():
    lines = read_input_file("20.txt")
    tiles = parse_tiles(lines)

    all_transformed_tiles = build_all_transformed_tiles(tiles)
    assembled_matrix = assemble(all_transformed_tiles)
    value = part_1(assembled_matrix)

    print("Part 1: the value is {}".format(value))
示例#4
0
def main():
    lines = read_input_file("10.txt")
    adapters = [int(line) for line in lines]
    number = get_joltage_number(adapters)
    print("Part 1: the product number is {}".format(number))

    combinations_count = get_all_combinations_count(adapters)
    print(
        "Part 2: found a total of {} combinations".format(combinations_count))
示例#5
0
def main():
    lines = read_input_file("6.txt")
    all_group_answers = gather_all_group_answers(lines, from_anyone)
    sum_counts = sum_count_answers(all_group_answers)
    print("Part 1 - anyone: the sum of all counts is {}".format(sum_counts))

    all_group_answers = gather_all_group_answers(lines, from_everyone)
    sum_counts = sum_count_answers(all_group_answers)
    print("Part 2 - everyone: the sum of all counts is {}".format(sum_counts))
示例#6
0
def main():
    lines = read_input_file("19.txt")
    rules, messages = parse(lines)

    count = Solver(rules).count_matching(messages)
    print("Part 1: the number of matching messages is {}".format(count))

    count = Solver(rules, part_2=True).count_matching(messages)
    print("Part 2: the number of matching messages is {}".format(count))
示例#7
0
def main():
    lines = read_input_file("16.txt")
    rules, my_ticket, nearby_tickets = Parser().parse(lines)

    error_rate = find_error_rate(rules, nearby_tickets)
    print("Part 1: the error rate is {}".format(error_rate))

    value = resolve_my_ticket(rules, my_ticket, nearby_tickets)
    print("Part 2: the value is {}".format(value))
示例#8
0
def main():
    lines = read_input_file("7.txt")
    rules = gather_rules(lines)

    outer_bags = search_outer_bags(rules, "shiny gold")
    count = len(outer_bags)
    print("Part 1 - {} bag(s) can contain a shiny gold one".format(count))

    count = count_inner_bags(rules, "shiny gold")
    print("Part 2 - a shiny gold bag contains {} other bags".format(count))
示例#9
0
def main():
    lines = read_input_file("11.txt")
    configuration = parse_initial_configuration(lines)

    simulator = Part1Simulator(configuration)
    occupied_seats = simulator.run()
    print("Part 1: the number of occupied seats is {}".format(occupied_seats))

    simulator = Part2Simulator(configuration)
    occupied_seats = simulator.run()
    print("Part 2: the number of occupied seats is {}".format(occupied_seats))
示例#10
0
def main():
    lines = read_input_file("9.txt")
    numbers = parse_numbers(lines)
    uncompliant_number = find_first_uncompliant(numbers, window_size=25)
    print("Part 1: the first uncompliant number is {}".format(
        uncompliant_number))

    contiguous_range = find_contiguous_range_with_sum(
        numbers, target_sum=uncompliant_number)
    weakness = find_encryption_weakness(contiguous_range)
    print("Part 2: the encryption weakness is {}".format(weakness))
示例#11
0
def main():
    lines = read_input_file("13.txt")
    earliest_timestamp, bus_ids = parse_lines(lines)

    bus_id, waiting_time = find_earliest_bus(earliest_timestamp, bus_ids)
    print("Part 1: the earliest bus ID is {}, arriving in {} minutes".format(
        bus_id, waiting_time))
    print("  > the answer is {}".format(bus_id * waiting_time))

    timestamp = find_earliest_timestamp(bus_ids)
    print("Part 2: the earliest timestamp found is {}".format(timestamp))
示例#12
0
def main():
    lines = read_input_file("8.txt")
    instructions = parse_instructions(lines)
    accumulator, _ = run_and_stop_at_first_cycle(instructions)
    print("Part 1: the value in the accumulator is {}".format(accumulator))

    search_result = search_fixed_instructions(instructions)
    if search_result is None:
        print("Part 2: no search result was found ... suspicious")
    else:
        accumulator, _ = search_result
        print("Part 2: the value in the accumulator is {}".format(accumulator))
示例#13
0
def main():
    lines = read_input_file("5.txt")
    seats = [decode_seat(line) for line in lines]
    seat_ids = [get_seat_id(seat) for seat in seats]
    max_seat_id = max(seat_ids)
    print("Part 1: the maximum seat id is {}".format(max_seat_id))

    missing_seat_id = find_missing_seat_id(seat_ids)
    if missing_seat_id is None:
        print("Part 2: no missing seat id has been found. Weird.")
    else:
        print("Part 2: the missing seat id is {}".format(missing_seat_id))
示例#14
0
def main():
    lines = read_input_file("3.txt")
    locations = build_locations(lines)
    count = count_trees(locations, slope_x=3, slope_y=1)
    print("Part 1: found {} tree(s) along the path".format(count))

    slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
    counts = count_all_trees(locations, slopes)
    counts_product = product(counts)
    print("Part 2: found product {}".format(counts_product))

    for i, slope in enumerate(slopes):
        print("  > slope {}: found {} tree(s)".format(slope, counts[i]))
示例#15
0
def main():
    global VERSION

    lines = read_input_file("18.txt")
    expressions = list(tokenize(line) for line in lines)

    VERSION = 1
    total = evaluate_all(expressions)
    print("Part 1: the sum of evaluated expressions is {}".format(total))

    VERSION = 2
    total = evaluate_all(expressions)
    print("Part 2: the sum of evaluated expressions is {}".format(total))
示例#16
0
def main():
    lines = read_input_file("12.txt")
    commands = parse_commands(lines)

    ship = Ship1(facing_direction='E')
    ship.follow(commands)
    distance = ship.get_manhattan_distance()
    print('Part 1: the Manhattan distance is {}'.format(distance))

    ship = Ship2(waypoint=(10, 1))
    ship.follow(commands)
    distance = ship.get_manhattan_distance()
    print('Part 2: the Manhattan distance is {}'.format(distance))
示例#17
0
def main():
    lines = read_input_file("17.txt")
    space = parse_layer(lines)

    count_active = apply_cycles(space,
                                n_cycles=6,
                                include_hyperspace_dimension=False)
    print("Part 1: the number of active cubes after 6 cycles is {}".format(
        count_active))

    count_active = apply_cycles(space,
                                n_cycles=6,
                                include_hyperspace_dimension=True)
    print("Part 2: the number of active cubes after 6 cycles is {}".format(
        count_active))
示例#18
0
def main():
    lines = read_input_file("2.txt")
    policies = [parse_policy(line) for line in lines]

    compliant_policies_v1 = [
        policy for policy in policies if policy.is_compliant_v1()
    ]
    print("Part 1: found {} compliant policies".format(
        len(compliant_policies_v1)))

    compliant_policies_v2 = [
        policy for policy in policies if policy.is_compliant_v2()
    ]
    print("Part 2: found {} compliant policies".format(
        len(compliant_policies_v2)))
示例#19
0
def main():
    lines = read_input_file("1.txt")
    entries = [int(line) for line in lines]

    entries_set = set(entries)
    assert len(entries) == len(
        entries_set), "Implementation requires no duplicates in the input"

    pair = find_pair(entries_set, 2020)

    if pair is None:
        print("Pair not found")
    else:
        first, second = pair
        print("Found pair {} x {} = {}".format(first, second, first * second))

    trio = find_trio(entries_set, 2020)

    if trio is None:
        print("Trio not found")
    else:
        (first, second, third) = trio
        print("Found trio {} x {} x {} = {}".format(first, second, third,
                                                    first * second * third))
示例#20
0
def main():
    lines = read_input_file("21.txt")
    recipes = parse_recipes(lines)
    for r in recipes:
        print(r)