def main(): config = Config(day=7) # PART ONE path_file = os.path.join(config.path_data, "bag_color_rules_test.txt") data_test = read_txt_file(path_file=path_file) data_test_parsed = parse_rules(data=data_test) bags_carrying_shiny_gold = find_gold_carrying_bags(data=data_test_parsed, bag_type="shiny gold bag") n_bags_carrying_shiny_gold = len(bags_carrying_shiny_gold) assert 4 == n_bags_carrying_shiny_gold path_file = os.path.join(config.path_data, "bag_color_rules.txt") data = read_txt_file(path_file=path_file) data_parsed = parse_rules(data=data) bags_carrying_shiny_gold = find_gold_carrying_bags(data=data_parsed) assert 128 == len(bags_carrying_shiny_gold) # PART TWO bags_unpacked = unpack(rules=data_test_parsed, inner_bags=data_test_parsed["shiny gold bags"]) assert 32 == len(bags_unpacked) path_file = os.path.join(config.path_data, "bag_color_rules_test_part_two.txt") data_test_two = read_txt_file(path_file=path_file) data_test_two_parsed = parse_rules(data=data_test_two) bags_unpacked = unpack(rules=data_test_two_parsed, inner_bags=data_test_two_parsed["shiny gold bags"]) assert 126 == len(bags_unpacked) bags_unpacked = unpack(rules=data_parsed, inner_bags=data_parsed["shiny gold bags"]) print(f"The total number of bags within a shiny gold bag equals: {len(bags_unpacked)}") assert 20189 == len(bags_unpacked) return True
def main(): config = Config(day=4) path_file = os.path.join(config.path_data, "passport_batch_files.txt") path_file_invalid = os.path.join(config.path_data, "passport_batch_files_invalid_test.txt") path_file_valid = os.path.join(config.path_data, "passport_batch_files_valid_test.txt") with open(path_file, "r") as f: data = [value.strip("\n") for value in f.readlines()] data_parsed = parse_data(data=data) passport_validity = [*map(validate_passport_one, data_parsed)] print("PART ONE") print(f"Number of valid passports: {sum(passport_validity)} / {len(passport_validity)}") with open(path_file_invalid, "r") as f: data_invalid = [value.strip("\n") for value in f.readlines()] data_invalid_parsed = parse_data(data=data_invalid) passport_validity = [*map(validate_passport_two, data_invalid_parsed)] assert not all(passport_validity) with open(path_file_valid, "r") as f: data_valid = [value.strip("\n") for value in f.readlines()] data_valid_parsed = parse_data(data=data_valid) passport_validity = [*map(validate_passport_two, data_valid_parsed)] assert all(passport_validity) passport_validity = [*map(validate_passport_two, data_parsed)] print("PART TWO") print(f"Number of valid passports: {sum(passport_validity)} / {len(passport_validity)}") return True
def main(): config = Config(day=17) # PART ONE # Test state_initial = parse_initial_state(path_file=os.path.join(config.path_data, 'initial_state_test.txt')) state_final = simulate_cycles(state_initial=state_initial, n_cycles=6) assert 112 == state_final.sum().sum() # Real deal state_initial = parse_initial_state(path_file=os.path.join(config.path_data, 'initial_state.txt')) state_final = simulate_cycles(state_initial=state_initial, n_cycles=6) print(f"The number of active states upon 6 cycles equals: {state_final.sum().sum()}") assert 255 == state_final.sum().sum() # PART TWO # Test state_initial = parse_initial_state(path_file=os.path.join(config.path_data, 'initial_state_test.txt'), dims=4) state_final = simulate_cycles(state_initial=state_initial, n_cycles=6, dims=4) assert 848 == state_final.sum().sum() # Real deal state_initial = parse_initial_state(path_file=os.path.join(config.path_data, 'initial_state.txt'), dims=4) state_final = simulate_cycles(state_initial=state_initial, n_cycles=6, dims=4) print(f"PART TWO: The number of active states upon 6 cycles equals: {state_final.sum().sum()}") return True
def main(): config = Config(day=14) # PART ONE # Test path_file_test = os.path.join(config.path_data, "program_test.txt") program = parse_program(path_file_test) memory = execute_program(program=program) assert 165 == get_memory_sum(memory=memory) # Real deal path_file = os.path.join(config.path_data, "program.txt") program = parse_program(path_file) memory = execute_program(program=program) memory_sum = get_memory_sum(memory=memory) print(f"The sum of memory equals: {memory_sum}") assert 9619656261720 == memory_sum # PART TWO # Test path_file_test = os.path.join(config.path_data, "program_test_two.txt") program = parse_program(path_file_test) memory = execute_program_v2(program=program) assert 208 == get_memory_sum(memory=memory) # Real deal program = parse_program(path_file) memory = execute_program_v2(program=program) memory_sum = get_memory_sum(memory) print(f"The sum of memory for part 2 equals: {memory_sum}") assert 4275496544925 == sum(list(memory.values())) return True
def main(): config = Config(day=9) # PART ONE # Test path_data = os.path.join(config.path_data, "xmas_test.txt") data_test = read_txt_file(path_file=path_data) data_test = [int(val) for val in data_test] encoding_error_test = detect_encoding_error(sequence=data_test, preamble=5) assert 127 == encoding_error_test path_data = os.path.join(config.path_data, "xmas.txt") data = read_txt_file(path_file=path_data) data = [int(val) for val in data] encoding_error = detect_encoding_error(sequence=data, preamble=25) assert 167829540 == encoding_error print(f"The first digit which does not abide to XMAX encoding is: {encoding_error}") # PART TWO # Test encoding_weakness_test = detect_encryption_weakness(sequence=data_test, encoding_error=encoding_error_test) assert 62 == encoding_weakness_test encoding_weakness = detect_encryption_weakness(sequence=data, encoding_error=encoding_error) print( f"The sum of the min and max of the contiguous sequence of numbers, " f"which sum equals the encoding error equals: {encoding_weakness}" ) assert 28045630 == encoding_weakness return True
def main(): config = Config(day=6) path_file = os.path.join(config.path_data, "custom_declaration_forms.txt") # PART ONE path_file_test = os.path.join(config.path_data, "custom_declaration_forms_test.txt") data_test = read_txt_file(path_file=path_file_test) data_test_group = split_group_data(data=data_test) sum_of_counts = compute_part_one(groups=data_test_group) assert 11 == sum_of_counts, f"Script does not lead to the correct sum. 11 != {sum_of_counts}" data = read_txt_file(path_file=path_file) groups = split_group_data(data=data) sum_of_counts = compute_part_one(groups=groups) print( f"The sum of the counts of the unique questions to which a group answered yes equals: {sum_of_counts}" ) assert 6585 == sum_of_counts # PART TWO data_test_group = split_group_data(data=data_test) sum_of_counts = compute_part_two(groups=data_test_group) assert 6 == sum_of_counts, f"Script does not lead to the correct sum. 6 != {sum_of_counts}" groups = split_group_data(data=data) sum_of_counts = compute_part_two(groups=groups) print( f"The sum of the counts of the questions to which all group members answered yes: {sum_of_counts}" ) return True pass
def main(): config = Config(day=11) # PART ONE # Test one path_data = os.path.join(config.path_data, "seating_system_test.txt") seat_system_test_one = parse_seat_system(path_file=path_data) path_data = os.path.join(config.path_data, "seating_system_test_exp.txt") seat_system_exp = parse_seat_system(path_file=path_data) seat_system_new = fill_seats(seat_plan=seat_system_test_one) assert np.array_equal(seat_system_exp, seat_system_new) state_count = get_state_counts(seat_system=seat_system_new) assert state_count["#"] == 37 path_data = os.path.join(config.path_data, "seating_system.txt") seat_system = parse_seat_system(path_file=path_data) # seat_system = fill_seats(seat_plan=seat_system) # state_count = get_state_counts(seat_system=seat_system) # # print(f"Number of occupied seats equals: {state_count['#']}") # assert state_count["#"] == 2275 # Test get visible seats path_data = os.path.join(config.path_data, "seating_system_test_two.txt") seat_system_test_two = parse_seat_system(path_file=path_data) seats_occupied = get_visible_occupied_seats(seat_plan=seat_system_test_two, colidx=3, rowidx=4) assert seats_occupied == 8 path_data = os.path.join(config.path_data, "seating_system_test_three.txt") seat_system_test_three = parse_seat_system(path_file=path_data) seats_occupied = get_visible_occupied_seats( seat_plan=seat_system_test_three, colidx=1, rowidx=1) assert seats_occupied == 0 path_data = os.path.join(config.path_data, "seating_system_test_four.txt") seat_system_test_four = parse_seat_system(path_file=path_data) seats_occupied = get_visible_occupied_seats( seat_plan=seat_system_test_four, colidx=3, rowidx=3) assert seats_occupied == 0 # Test fill_seats_two seat_system_new = fill_seats_two(seat_plan=seat_system_test_one) state_count = get_state_counts(seat_system=seat_system_new) assert state_count["#"] == 26 # Real deal (Does not work) # seat_system_new = fill_seats_two(seat_plan=seat_system) # state_count = get_state_counts(seat_system=seat_system_new) print(f"The number of occupied seats equals: {state_count['#']}") return True
def main(): config = Config(day=13) # PART ONE # Test path_data_test = os.path.join(config.path_data, "notes_test.txt") timestamp_test, buslines_test = parse_notes(path_file=path_data_test) minutes_to_wait, bus_earliest = get_earliest_bus(timestamp=timestamp_test, buslines=buslines_test) assert 295 == bus_earliest * minutes_to_wait # Real deal path_data = os.path.join(config.path_data, "notes.txt") timestamp, buslines = parse_notes(path_file=path_data) minutes_to_wait, bus_earliest = get_earliest_bus(timestamp=timestamp, buslines=buslines) print( f"The earliest bus is {bus_earliest} which arrives in {minutes_to_wait} minutes, making the product equal to: " f"{minutes_to_wait * bus_earliest}") assert 410 == bus_earliest * minutes_to_wait # PART TWO # Test timestamp_test, buslines_test = parse_notes_two(path_file=path_data_test) t = get_t(buslines=buslines_test) assert 1068781 == t buslines_test = ["17", "x", "13", "19"] t = get_t(buslines=buslines_test) assert 3417 == t buslines_test = ["67", "7", "59", "61"] t = get_t(buslines=buslines_test) assert 754018 == t buslines_test = ["67", "x", "7", "59", "61"] t = get_t(buslines=buslines_test) assert 779210 == t # Real deal timestamp, buslines = parse_notes_two(path_file=path_data) t = get_t(buslines=buslines) print(f"t equals: {t} minutes") assert 600691418730595 == t return True
def main(): config = Config(day=1) path_file = os.path.join(config.path_data, "expense_report.txt") with open(path_file, "r") as f: data = [int(value.strip("\n")) for value in f.readlines()] for group_size in [2, 3]: group_2020 = next(get_2020_groups(lst=data, group_size=group_size)) if group_2020: print( f"Multiplying the group of size {group_size} which sum equals to 2020 {group_2020} gives: " f"{multiply(group_2020)}") return True
def main(): config = Config(day=18) path_homework = os.path.join(config.path_data, "homework.txt") # PART ONE results = execute_homework(path_homework=path_homework, solver=solve_math_expression_part_one) print(f"The sum of the resulting values: {sum(results)}") assert 131076645626 == sum(results) # PART TWO results = execute_homework(path_homework=path_homework, solver=solve_math_expression_part_two) print(f"The sum of the resulting values (PART TWO): {sum(results)}") assert 109418509151782 == sum(results) return True
def main(): config = Config(day=12) # PART ONE # Test one path_data = os.path.join(config.path_data, "evasive_actions_test.txt") evasive_actions_test = parse_puzzle_input(path_data=path_data) coordinate_final = execute_actions(actions=evasive_actions_test, location_init=[0, 0], direction_init="E") manhattan_distance = get_manhattan_distance(location=coordinate_final) assert 25 == manhattan_distance # Real deal path_data = os.path.join(config.path_data, "evasive_actions.txt") evasive_actions = parse_puzzle_input(path_data=path_data) coordinate_final = execute_actions(actions=evasive_actions, location_init=[0, 0], direction_init="E") manhattan_distance = get_manhattan_distance(location=coordinate_final) print( f"The manhattan distance of the final coordinate equals: {manhattan_distance}" ) assert 2280 == manhattan_distance # PART TWO # Test coordinate_final = execute_actions_two(actions=evasive_actions_test, location_init=[0, 0], waypoint_init=[10, 1]) manhattan_distance = get_manhattan_distance(location=coordinate_final) assert manhattan_distance == 286 coordinate_final = execute_actions_two(actions=evasive_actions, location_init=[0, 0], waypoint_init=[10, 1]) manhattan_distance = get_manhattan_distance(location=coordinate_final) print( f"The manhattan distance of the final coordinate equals: {manhattan_distance}" ) assert 38693 == manhattan_distance return True
def main(): config = Config(day=5) path_file = os.path.join(config.path_data, "boarding_passes.txt") with open(path_file, "r") as f: boarding_passes = [value.strip("\n") for value in f.readlines()] print(f"The number of boarding passes equals: {len(boarding_passes)}") boarding_passes_test = ["BFFFBBFRRR", "FFFBBBFRRR", "BBFFBBFRLL"] row_indices = [*range(0, 128)] column_indices = [*range(0, 8)] for boarding_pass, row_number_exp, col_number_exp in zip( boarding_passes_test, [70, 14, 102], [7, 7, 4]): row, = get_row(row_sequence=boarding_pass[:7], row_indices=row_indices) assert row_number_exp == row column, = get_column(col_sequence=boarding_pass[7:], col_indices=column_indices) assert col_number_exp == column seat_id = get_seat_id(row=row, column=column) print( f"Boardingpass {boarding_pass}, sits on row {row} and column {column}, with seat id {seat_id}" ) seat_ids = [] for boarding_pass in boarding_passes: row, = get_row(row_sequence=boarding_pass[:7], row_indices=row_indices) column, = get_column(col_sequence=boarding_pass[7:], col_indices=column_indices) seat_ids.append(get_seat_id(row=row, column=column)) print(f"The seat with the highest seat ID equals: {max(seat_ids)}") # PART TWO missing_seat_ids = set(range(0, max(seat_ids))).difference(set(seat_ids)) for seat_id in missing_seat_ids: if seat_id - 1 in seat_ids and seat_id + 1 in seat_ids: print(f"Your seat ID: {seat_id}") return True
def main(): config = Config(day=3) path_test_file = os.path.join(config.path_data, "tree_map_test.txt") slope = (1, 3) with open(path_test_file, "r") as f: data_test = [value.strip("\n") for value in f.readlines()] tree_count = count_encountered_trees(tree_data=data_test, slope=slope) assert tree_count == 7, f"The number of encountered trees in the test set != 7 but {tree_count}" path_file = os.path.join(config.path_data, "day_3", "tree_map.txt") with open(path_file, "r") as f: data = [value.strip("\n") for value in f.readlines()] # Part 1 tree_count = count_encountered_trees(tree_data=data, slope=slope) print( f"The total number of encountered trees with slope {slope} are: {tree_count}" ) # Part 2 slopes = [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)] tree_counts = [] tree_counts_test_exp = [2, 7, 3, 4, 2] for slope, tree_count_exp in zip(slopes, tree_counts_test_exp): tree_count_test = count_encountered_trees(tree_data=data_test, slope=slope) assert tree_count_test == tree_count_exp, f"Tree count != expected tree count. " \ f"{tree_count_test} != {tree_count_exp}" tree_count = count_encountered_trees(tree_data=data, slope=slope) tree_counts.append(tree_count) print( f"The total number of encountered trees with slope {slope} are: {tree_count}" ) print(f"The product of all the tree counts is: {math.prod(tree_counts)}") return True
def main(): config = Config(day=2) path_file = os.path.join(config.path_data, "password_policies.txt") with open(path_file, "r") as f: data = [value.strip("\n") for value in f.readlines()] valid_passwords_one = [] valid_passwords_two = [] for string in data: policy, password = split_policy_and_password(string=string) policy_integers, character = split_policy(policy=policy) if is_valid_rule_one(character_count_range=policy_integers, character=character, password=password): valid_passwords_one.append(string) if is_valid_rule_two(character_indices_policy=policy_integers, character=character, password=password): valid_passwords_two.append(string) print(f"The number of valid passwords according to rule #1 is: {len(valid_passwords_one)}") print(f"The number of valid passwords according to rule #2 is: {len(valid_passwords_two)}") return True
def main(): config = Config(day=10) # PART ONE # Test one path_data = os.path.join(config.path_data, "adapter_list_test_one.txt") data_test_one = read_adapter_data(path_file=path_data) result = compute_part_one(adapters=data_test_one) assert 35 == result # Test two path_data = os.path.join(config.path_data, "adapter_list_test_two.txt") data_test_two = read_adapter_data(path_file=path_data) result = compute_part_one(adapters=data_test_two) assert 220 == result path_data = os.path.join(config.path_data, "adapter_list.txt") data = read_adapter_data(path_file=path_data) result = compute_part_one(adapters=data) print(f"The number of 1 and 3 joltage differences multiplied equals: {result}") assert 1876 == result # PART TWO # Test one n_arrangements = get_adapter_arrangements(adapters=data_test_one) assert 8 == n_arrangements # Test two n_arrangements = get_adapter_arrangements(adapters=data_test_two) assert 19208 == n_arrangements n_arrangements = get_adapter_arrangements(adapters=data) print(f"The number of possible adapter arrangements equals: {n_arrangements}") assert 14173478093824 == n_arrangements return True
def main(): config = Config(day=16) # Test path_ticket_info = os.path.join(config.path_data, "ticket_info_test.txt") error_rate = compute_error_rate(path_ticket_info=path_ticket_info) assert 71 == error_rate # Real deal path_ticket_info = os.path.join(config.path_data, "ticket_info.txt") error_rate = compute_error_rate(path_ticket_info=path_ticket_info) print(f"The ticket scanning error rate equals: {error_rate}") assert 24980 == error_rate # PART TWO # Test path_ticket_info = os.path.join(config.path_data, "ticket_info_test_two.txt") notes, ticket_mine, tickets_nearby = parse_ticket_info( path_file=path_ticket_info) field_order = get_field_order(notes=notes, tickets_nearby=tickets_nearby) assert ["row", "class", "seat"] == field_order # Real deal path_ticket_info = os.path.join(config.path_data, "ticket_info.txt") notes, ticket_mine, tickets_nearby = parse_ticket_info( path_file=path_ticket_info) field_order = get_field_order(notes=notes, tickets_nearby=tickets_nearby) fields_departure = [ i for i, f in enumerate(field_order) if "departure" in f ] print( f"The product of the departure fields equals: {prod([ticket_mine[i] for i in fields_departure])}" ) assert 809376774329 == prod([ticket_mine[i] for i in fields_departure]) return True
def main(): """ Stolen from https://dev.to/qviper/advent-of-code-2020-python-solution-day-11-2lkj """ config = Config(day=11) # PART ONE # Test one path_data = os.path.join(config.path_data, "seating_system.txt") with open(path_data, "r") as fp: lines = [line.rstrip() for line in fp.readlines()] lines = [list(line) for line in lines] rows, cols = len(lines), len(lines[0]) deltas = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] occupancy = check_occupied2(lines, deltas=deltas, rows=rows, cols=cols) print(f"There are {occupancy} valid seats.")
def main(): config = Config(day=8) # PART ONE path_file_test = os.path.join(config.path_data, "boot_code_test.txt") path_file = os.path.join(config.path_data, "boot_code.txt") # Test set data_test = read_txt_file(path_file=path_file_test) print(f"The test boot code has {len(data_test)} lines") infinite_loop, accumulator = execute_boot_code(boot_code=data_test) assert infinite_loop, f"Execution of bootcode did not enter a infinite loop" assert 5 == accumulator, f"Accumulator value is incorrect. 5 != {accumulator}" data = read_txt_file(path_file=path_file) print(f"The boot code has {len(data)} lines") infinite_loop, accumulator = execute_boot_code(boot_code=data) print( f"The boot code accumulator equals {accumulator} before it hits an infinite loop" ) assert infinite_loop, f"Execution of bootcode did not enter a infinite loop" assert 1939 == accumulator, f"Accumulator value is incorrect. 1939 != {accumulator}" # PART TWO infinite_loop, accumulator, boot_code = fix_boot_code(boot_code=data_test) assert not infinite_loop assert 8 == accumulator, f"Accumulator value is incorrect. 8 != {accumulator}" infinite_loop, accumulator, boot_code = fix_boot_code(boot_code=data) assert not infinite_loop print( f"The fixed boot code accumulator equals {accumulator} after running until termination" ) assert 2212 == accumulator, f"Accumulator value is incorrect. 2212 != {accumulator}" return True