def test_step_optimized(): polymer_template, rules_map = _parse_input(auto_read_input()) letter_counts = step_optimized(polymer_template, rules_map, 10) expected_letter_counts = {"B": 1749, "C": 298, "H": 161, "N": 865} assert letter_counts == expected_letter_counts polymer_template, rules_map = _parse_input(auto_read_input()) letter_counts = step_optimized(polymer_template, rules_map, 40) assert letter_counts["B"] == 2192039569602 assert letter_counts["H"] == 3849876073
def test__parse_input(): ## Start here polymer_template, rules_map = _parse_input(auto_read_input()) expected_polymer_template = "NNCB" expected_rules_map = { "C": { "H": "B", "B": "H", "C": "N", "N": "C", }, "H": { "H": "N", "B": "C", "C": "B", "N": "C", }, "N": { "H": "C", "N": "C", "C": "B", "B": "B", }, "B": { "H": "H", "N": "B", "B": "N", "C": "B", }, } assert polymer_template == expected_polymer_template assert rules_map == expected_rules_map
def test_Graph_paths_visiting_all_small_caves_at_most_once_2(): graph = _parse_input(auto_read_input(suffix=2)) paths = graph.paths_visiting_all_small_caves_at_most_once() expected_paths = { "start,HN,dc,HN,end", "start,HN,dc,HN,kj,HN,end", "start,HN,dc,end", "start,HN,dc,kj,HN,end", "start,HN,end", "start,HN,kj,HN,dc,HN,end", "start,HN,kj,HN,dc,end", "start,HN,kj,HN,end", "start,HN,kj,dc,HN,end", "start,HN,kj,dc,end", "start,dc,HN,end", "start,dc,HN,kj,HN,end", "start,dc,end", "start,dc,kj,HN,end", "start,kj,HN,dc,HN,end", "start,kj,HN,dc,end", "start,kj,HN,end", "start,kj,dc,HN,end", "start,kj,dc,end", } assert paths == expected_paths
def part2(): algorithm, img = _parse_input(auto_read_input()) for gen in range(50): print(f"gen: {gen}") img = img.get_enhanced(algorithm, gen) print(img) return img.num_on()
def test__parse_input(): ## Start here displays = _parse_input(auto_read_input()[:2]) assert displays[0]._signal_patterns == [ set("be"), set("cfbegad"), set("cbdgef"), set("fgaecd"), set("cgeb"), set("fdcge"), set("agebfd"), set("fecdb"), set("fabcd"), set("edb"), ] assert displays[0]._output_value == ["fdgacbe", "cefdb", "cefbgd", "gcbe"] assert displays[1]._signal_patterns == [ set("edbfga"), set("begcd"), set("cbg"), set("gc"), set("gcadebf"), set("fbgde"), set("acbgfd"), set("abcde"), set("gfcbed"), set("gfec"), ] assert displays[1]._output_value == ["fcgedb", "cgb", "dgebacf", "gc"]
def part2(): x_min, x_max, y_min, y_max = _parse_input(auto_read_input()) possible_x_v0 = _possible_x_v0(x_min, x_max) possible_y_v0 = _possible_y_v0(y_min, y_max) possible_x_v0_and_y_v0 = _possible_x_v0_and_y_v0(possible_x_v0, possible_y_v0) return len({(v0_data.x_v0, v0_data.y_v0) for v0_data in possible_x_v0_and_y_v0})
def test_HeightMap_get_all_low_points(): heightmap = _parse_input(auto_read_input()) assert heightmap.get_all_low_points() == [ Point(0, 1, 1), Point(0, 9, 0), Point(2, 2, 5), Point(4, 6, 5), ]
def part1(): octopus_map = _parse_input(auto_read_input()) total_flashes = 0 for i in range(100): total_flashes += octopus_map.step() return total_flashes
def test_Graph_paths_visiting_all_small_caves_at_most_once_one_small_cave_at_most_twice_3( ): graph = _parse_input(auto_read_input(suffix=3)) paths = ( graph. paths_visiting_all_small_caves_at_most_once_one_small_cave_at_most_twice( )) assert len(paths) == 3509
def part1(): x_min, x_max, y_min, y_max = _parse_input(auto_read_input()) possible_x_v0 = _possible_x_v0(x_min, x_max) possible_y_v0 = _possible_y_v0(y_min, y_max) possible_x_v0_and_y_v0 = _possible_x_v0_and_y_v0(possible_x_v0, possible_y_v0) v0_pair, max_height = _valid_v0_with_highest_peak(possible_x_v0_and_y_v0) return v0_pair, max_height
def part2(): octopus_map = _parse_input(auto_read_input()) num_flashes = 0 num_steps = 0 while num_flashes != 100: num_flashes = octopus_map.step() num_steps += 1 return num_steps
def test__parse_input(): ## Start here heightmap = _parse_input(auto_read_input()) assert heightmap._heights == [ [2, 1, 9, 9, 9, 4, 3, 2, 1, 0], [3, 9, 8, 7, 8, 9, 4, 9, 2, 1], [9, 8, 5, 6, 7, 8, 9, 8, 9, 2], [8, 7, 6, 7, 8, 9, 6, 7, 8, 9], [9, 8, 9, 9, 9, 6, 5, 6, 7, 8], ]
def test_enhance(): algorithm, img = _parse_input(auto_read_input()) img = img.get_enhanced(algorithm, 0) assert img.num_on() == 24 img = img.get_enhanced(algorithm, 1) assert img.num_on() == 35 # Slow for gen in range(2, 50): # rest up to 50 gens img = img.get_enhanced(algorithm, gen) assert img.num_on() == 3351
def test__pixel_on_after_enhance(): algorithm, img = _parse_input(auto_read_input()) # value_idx = 34 assert img._pixel_on_after_enhance(algorithm, 2, 2, 0) == True # value_idx: 18 assert img._pixel_on_after_enhance(algorithm, 0, 0, 0) == False # value_idx: 134 assert img._pixel_on_after_enhance(algorithm, 4, 3, 0) == False # value_idx: 312 assert img._pixel_on_after_enhance(algorithm, 4, 3, 0) == False
def test_Basin_size(): heightmap = _parse_input(auto_read_input()) b1 = Basin(heightmap, Point(0, 1, 1)) assert b1.size == 3 b2 = Basin(heightmap, Point(0, 9, 0)) assert b2.size == 9 b3 = Basin(heightmap, Point(2, 2, 5)) assert b3.size == 14 b4 = Basin(heightmap, Point(4, 6, 5)) assert b4.size == 9 basins = [b1, b2, b3, b4] top3_basins = sorted(basins, key=lambda b: b.size)[-3:] assert math.prod([b.size for b in top3_basins]) == 1134
def test_Graph_paths_visiting_all_small_caves_at_most_once_1(): graph = _parse_input(auto_read_input(suffix=1)) paths = graph.paths_visiting_all_small_caves_at_most_once() expected_paths = { "start,A,b,A,c,A,end", "start,A,b,A,end", "start,A,b,end", "start,A,c,A,b,A,end", "start,A,c,A,b,end", "start,A,c,A,end", "start,A,end", "start,b,A,c,A,end", "start,b,A,end", "start,b,end", } assert paths == expected_paths
def test__parse_input(): ## Start here grid = _parse_input(auto_read_input()) expected_grid = [ [1, 1, 6, 3, 7, 5, 1, 7, 4, 2], [1, 3, 8, 1, 3, 7, 3, 6, 7, 2], [2, 1, 3, 6, 5, 1, 1, 3, 2, 8], [3, 6, 9, 4, 9, 3, 1, 5, 6, 9], [7, 4, 6, 3, 4, 1, 7, 1, 1, 1], [1, 3, 1, 9, 1, 2, 8, 1, 3, 7], [1, 3, 5, 9, 9, 1, 2, 4, 2, 1], [3, 1, 2, 5, 4, 2, 1, 6, 3, 9], [1, 2, 9, 3, 1, 3, 8, 5, 2, 1], [2, 3, 1, 1, 9, 4, 4, 5, 8, 1], ] assert grid == expected_grid
def test__parse_input(): ## Start here algorithm, img = _parse_input(auto_read_input()) expected_algorithm = 0b00101001111101010101110110000011101101001110111100111110010000100100110011100111111011100011110010011111001100101111100011010100101100101000000101110111111011101111000101101100100100111110000010100001110010110000001000001001001001100100011011111101111011110101000100000001001010100011110110100000010010001101011001000110101100111010000001010000000101010111101110110001000001111010010010110100001100101111000011000110010001000000101000000010000000110011110010001010100011001010011100111110000000010011110000001001 expected_img = { (0, 0), (0, 3), (1, 0), (2, 0), (2, 1), (2, 4), (3, 2), (4, 2), (4, 3), (4, 4), } expected_img = { (0, 0): True, (0, 1): False, (0, 2): False, (0, 3): True, (0, 4): False, (1, 0): True, (1, 1): False, (1, 2): False, (1, 3): False, (1, 4): False, (2, 0): True, (2, 1): True, (2, 2): False, (2, 3): False, (2, 4): True, (3, 0): False, (3, 1): False, (3, 2): True, (3, 3): False, (3, 4): False, (4, 0): False, (4, 1): False, (4, 2): True, (4, 3): True, (4, 4): True, } assert algorithm == expected_algorithm assert img == expected_img
def test__parse_input(): ## Start here # start # / \ # c--A-----b--d # \ / # end graph = _parse_input(auto_read_input(suffix=1)) expected_edges = { "start": {"A", "b"}, "A": {"start", "c", "b", "end"}, "b": {"start", "A", "d", "end"}, "c": {"A"}, "d": {"b"}, "end": {"A", "b"}, } assert graph.edges == expected_edges
def test__parse_input(): ## Start here octopus_map = _parse_input(auto_read_input()) expected_power_levels = [ [5, 4, 8, 3, 1, 4, 3, 2, 2, 3], [2, 7, 4, 5, 8, 5, 4, 7, 1, 1], [5, 2, 6, 4, 5, 5, 6, 1, 7, 3], [6, 1, 4, 1, 3, 3, 6, 1, 4, 6], [6, 3, 5, 7, 3, 8, 5, 4, 7, 8], [4, 1, 6, 7, 5, 2, 4, 6, 4, 5], [2, 1, 7, 6, 8, 4, 1, 7, 2, 1], [6, 8, 8, 2, 8, 8, 1, 1, 3, 4], [4, 8, 4, 6, 8, 4, 8, 5, 5, 4], [5, 2, 8, 3, 7, 5, 1, 5, 2, 6], ] assert octopus_map._power_levels == expected_power_levels
def test__parse_input(): ## Start here paper, instructions = _parse_input(auto_read_input()) expected_paper = [ [".", ".", ".", "#", ".", ".", "#", ".", ".", "#", "."], [".", ".", ".", ".", "#", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], ["#", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "#", ".", ".", ".", ".", "#", ".", "#"], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", "#", ".", ".", ".", ".", "#", ".", "#", "#", "."], [".", ".", ".", ".", "#", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", "#", ".", ".", ".", "#"], ["#", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], ["#", ".", "#", ".", ".", ".", ".", ".", ".", ".", "."], ] expected_instructions = [(FoldDirection.UP, 7), (FoldDirection.LEFT, 5)] assert paper == expected_paper assert instructions == expected_instructions
def test_fold(): paper, _ = _parse_input(auto_read_input()) resulting_paper = fold(paper, (FoldDirection.UP, 7)) expected_resulting_paper = [ ["#", ".", "#", "#", ".", ".", "#", ".", ".", "#", "."], ["#", ".", ".", ".", "#", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", "#", ".", ".", ".", "#"], ["#", ".", ".", ".", "#", ".", ".", ".", ".", ".", "."], [".", "#", ".", "#", ".", ".", "#", ".", "#", "#", "#"], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."], ] assert resulting_paper == expected_resulting_paper resulting_paper = fold(resulting_paper, (FoldDirection.LEFT, 5)) expected_resulting_paper = [ ["#", "#", "#", "#", "#"], ["#", ".", ".", ".", "#"], ["#", ".", ".", ".", "#"], ["#", ".", ".", ".", "#"], ["#", "#", "#", "#", "#"], [".", ".", ".", ".", "."], [".", ".", ".", ".", "."], ] assert resulting_paper == expected_resulting_paper
def part2(): grid = _parse_input(auto_read_input()) grid = expand_5x(grid) return lowest_threat_level_bfs(grid)
def test__parse_input(): ## Start here _ = _parse_input(auto_read_input()) assert False
def test_valid_v0_with_highest_peak(): x_min, x_max, y_min, y_max = _parse_input(auto_read_input()) possible_x_v0 = _possible_x_v0(x_min, x_max) possible_y_v0 = _possible_y_v0(y_min, y_max) possible_x_v0_and_y_v0 = _possible_x_v0_and_y_v0(possible_x_v0, possible_y_v0) v0_pair, highest_peak = _valid_v0_with_highest_peak(possible_x_v0_and_y_v0) expected_possible_v0s = set([ (23, -10), (25, -9), (27, -5), (29, -6), (22, -6), (21, -7), (9, 0), (27, -7), (24, -5), (25, -7), (26, -6), (25, -5), (6, 8), (11, -2), (20, -5), (29, -10), (6, 3), (28, -7), (8, 0), (30, -6), (29, -8), (20, -10), (6, 7), (6, 4), (6, 1), (14, -4), (21, -6), (26, -10), (7, -1), (7, 7), (8, -1), (21, -9), (6, 2), (20, -7), (30, -10), (14, -3), (20, -8), (13, -2), (7, 3), (28, -8), (29, -9), (15, -3), (22, -5), (26, -8), (25, -8), (25, -6), (15, -4), (9, -2), (15, -2), (12, -2), (28, -9), (12, -3), (24, -6), (23, -7), (25, -10), (7, 8), (11, -3), (26, -7), (7, 1), (23, -9), (6, 0), (22, -10), (27, -6), (8, 1), (22, -8), (13, -4), (7, 6), (28, -6), (11, -4), (12, -4), (26, -9), (7, 4), (24, -10), (23, -8), (30, -8), (7, 0), (9, -1), (10, -1), (26, -5), (22, -9), (6, 5), (7, 5), (23, -6), (28, -10), (10, -2), (11, -1), (20, -9), (14, -2), (29, -7), (13, -3), (23, -5), (24, -8), (27, -9), (30, -7), (28, -5), (21, -10), (7, 9), (6, 6), (21, -5), (27, -10), (7, 2), (30, -9), (21, -8), (22, -7), (24, -9), (20, -6), (6, 9), (29, -5), (8, -2), (27, -8), (30, -5), (24, -7), ]) assert {(v0_data.x_v0, v0_data.y_v0) for v0_data in possible_x_v0_and_y_v0} == expected_possible_v0s assert (v0_pair, highest_peak) == ( (6, 9), 45, )
def test__parse_input(): ## Start here x_min, x_max, y_min, y_max = _parse_input(auto_read_input()) assert (x_min, x_max, y_min, y_max) == (20, 30, -10, -5)
def part1(): _ = _parse_input(auto_read_input()) return None
def part2(): polymer, rules_map = _parse_input(auto_read_input()) letter_counts = step_optimized(polymer, rules_map, 40) return max(letter_counts.values()) - min(letter_counts.values())
def part1(): grid = _parse_input(auto_read_input()) return lowest_threat_level(grid)
def test__parse_input(): ## Start here msg = _parse_input(auto_read_input()) assert msg == "test input"