Beispiel #1
0
def parse_input(filepath) -> List[List[coord]]:
    """read the file and parse the inputs"""
    data: List[List[str]] = read_lines_as_list(filepath, split=" -> ")
    line_infos: List[List[coord]] = []
    for line in data:
        line_info: List[coord] = []
        for val in line:
            x, y = val.strip().split(",")
            pos: coord = (int(x), int(y))
            line_info.append(pos)
        line_infos.append(line_info)
    return line_infos
Beispiel #2
0
def load_data(filepath: str) -> Dict[str, List[str]]:
    """Load paths - bidirectional"""
    lines = read_lines_as_list(filepath, split="-")
    possible_paths: Dict[str, List[str]] = {}
    for line in lines:
        if line[0] in possible_paths.keys():
            possible_paths[line[0]].append(line[1])
        else:
            possible_paths[line[0]] = [line[1]]
        # bidirectional
        if line[1] in possible_paths.keys():
            possible_paths[line[1]].append(line[0])
        else:
            possible_paths[line[1]] = [line[0]]
    return possible_paths
Beispiel #3
0
class Test2021Day11(unittest.TestCase):
    test_data = [[int(val) for val in line]
                 for line in read_lines_as_list("data/11-test.txt", list)]

    @parameterized.expand([
        [
            1, 0,
            [[6, 5, 9, 4, 2, 5, 4, 3, 3, 4], [3, 8, 5, 6, 9, 6, 5, 8, 2, 2],
             [6, 3, 7, 5, 6, 6, 7, 2, 8, 4], [7, 2, 5, 2, 4, 4, 7, 2, 5, 7],
             [7, 4, 6, 8, 4, 9, 6, 5, 8, 9], [5, 2, 7, 8, 6, 3, 5, 7, 5, 6],
             [3, 2, 8, 7, 9, 5, 2, 8, 3, 2], [7, 9, 9, 3, 9, 9, 2, 2, 4, 5],
             [5, 9, 5, 7, 9, 5, 9, 6, 6, 5], [6, 3, 9, 4, 8, 6, 2, 6, 3, 7]]
        ],
        [
            10, 204,
            [[0, 4, 8, 1, 1, 1, 2, 9, 7, 6], [0, 0, 3, 1, 1, 1, 2, 0, 0, 9],
             [0, 0, 4, 1, 1, 1, 2, 5, 0, 4], [0, 0, 8, 1, 1, 1, 1, 4, 0, 6],
             [0, 0, 9, 9, 1, 1, 1, 3, 0, 6], [0, 0, 9, 3, 5, 1, 1, 2, 3, 3],
             [0, 4, 4, 2, 3, 6, 1, 1, 3, 0], [5, 5, 3, 2, 2, 5, 2, 3, 5, 0],
             [0, 5, 3, 2, 2, 5, 0, 6, 0, 0], [0, 0, 3, 2, 2, 4, 0, 0, 0, 0]]
        ],
        [
            100, 1656,
            [[0, 3, 9, 7, 6, 6, 6, 8, 6, 6], [0, 7, 4, 9, 7, 6, 6, 9, 1, 8],
             [0, 0, 5, 3, 9, 7, 6, 9, 3, 3], [0, 0, 0, 4, 2, 9, 7, 8, 2, 2],
             [0, 0, 0, 4, 2, 2, 9, 8, 9, 2], [0, 0, 5, 3, 2, 2, 2, 8, 7, 7],
             [0, 5, 3, 2, 2, 2, 2, 9, 6, 6], [9, 3, 2, 2, 2, 2, 8, 9, 6, 6],
             [7, 9, 2, 2, 2, 8, 6, 8, 6, 6], [6, 7, 8, 9, 9, 9, 8, 7, 6, 6]]
        ],
    ])
    def test_nof_flashes(self, steps, flashes, board):
        updated_data, measured_flashes = model_octopus(
            deepcopy(self.test_data), steps)
        self.assertEqual(measured_flashes, flashes)
        self.assertEqual(updated_data, board)

    def test_example_board(self):
        test_data2 = [[1, 1, 1, 1, 1], [1, 9, 9, 9, 1], [1, 9, 1, 9, 1],
                      [1, 9, 9, 9, 1], [1, 1, 1, 1, 1]]
        updated_data, measured_flashes = model_octopus(deepcopy(test_data2), 1)
        test_solution = [[3, 4, 5, 4, 3], [4, 0, 0, 0, 4], [5, 0, 0, 0, 5],
                         [4, 0, 0, 0, 4], [3, 4, 5, 4, 3]]
        self.assertEqual(measured_flashes, 9)
        self.assertEqual(test_solution, updated_data)

    def test_synchronising(self):
        self.assertEqual(did_synchronise(deepcopy(self.test_data)), 195)
Beispiel #4
0
            outp, _, _, _ = run_intcode_program(program, new_input)
            # Status (output[0])
            # 0: The repair droid hit a wall. Its position has not changed.
            # 1: The repair droid has moved one step in the requested direction.
            # 2: The repair droid has moved one step in the requested direction; new pos == oxygen system pos.
            if outp:  # 1 or 2
                # add new found program to possible descendants
                new_valid_programs.append(new_input)
                if outp == 2:
                    return len(new_input), new_input
    return find_shortest_path(program, new_valid_programs)


class Test2019Day15(unittest.TestCase):
    # @parameterized.expand([
    #     [],
    # ])
    def test_(self):
        pass
        # self.assertEqual(..., ...)


if __name__ == '__main__':
    print(">>> Start Main 15:")
    program = read_lines_as_list("data/15.txt")
    i, path = find_shortest_path(program, [[]])
    print("Part 1):")
    print(i)
    print("Part 2):")
    print("End Main 15<<<")
Beispiel #5
0
    ])
    def test_common(self, f, r):
        c = get_word_row_counts(self.default_data.copy())
        self.assertEqual(get_row_common(c, f), r)

    @parameterized.expand([
        ["O2GEN", "10111"],
        ["CO2", "01010"],
    ])
    def test_filter_data(self, crit, res):
        self.assertEqual(filter_data(self.default_data.copy(), crit), res)


if __name__ == '__main__':
    print(">>> Start Main 03:")
    puzzle_input = read_lines_as_list("data/03.txt")
    counts = get_word_row_counts(puzzle_input.copy())
    gamma_bin = get_row_common(counts, min)
    eps_bin = get_row_common(counts, max)
    print("Part 1): ")
    print("Gamma: ", gamma_bin, " -> ", int(gamma_bin, 2))
    print("Eps: ", eps_bin, " -> ", int(eps_bin, 2))
    print("--> ", int(gamma_bin, 2) * int(eps_bin, 2))
    print("Part 2): ")
    O2GEN_bin = filter_data(puzzle_input.copy(), "O2GEN")
    CO2_bin = filter_data(puzzle_input.copy(), "CO2")
    print("O2GEN: ", O2GEN_bin, " -> ", int(O2GEN_bin, 2))
    print("CO2: ", CO2_bin, " -> ", int(CO2_bin, 2))
    print("--> ", int(CO2_bin, 2) * int(O2GEN_bin, 2))
    print("End Main 03<<<")
Beispiel #6
0

def create_sliding_window_list(l: List[int], size: int = 3) -> List[int]:
    """for a given list, slide a window of given size and sum all the values in the window"""
    return [
        sum(l[pos + i] for i in range(size))
        for pos in range(len(l) - size + 1)
    ]


class Test2021Day01(unittest.TestCase):
    draft = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]

    @parameterized.expand([[draft, 7],
                           [[607, 618, 618, 617, 647, 716, 769, 792], 5]])
    def test_increasing(self, data, incr):
        self.assertEqual(find_increasing(data), incr)

    @parameterized.expand([[draft, [607, 618, 618, 617, 647, 716, 769, 792]]])
    def test_sliding_list(self, data, newl):
        self.assertEqual(create_sliding_window_list(data), newl)


if __name__ == '__main__':
    print(">>> Start Main 01:")
    puzzle_input = read_lines_as_list("data/01.txt", int)
    print("Part 1): ", find_increasing(puzzle_input))
    print("Part 2):",
          find_increasing(create_sliding_window_list(puzzle_input)))
    print("End Main 01<<<")
Beispiel #7
0
            aim -= int(dir[1])
        else:
            raise Exception("Unknown direction {}".format(dir[0]))
    return pos


class Test2021Day02(unittest.TestCase):
    @parameterized.expand([
        [[["forward", "5"], ["down", "5"], ["forward", "8"], ["up", "3"],
          ["down", "8"], ["forward", "2"]], (15, 10)],
    ])
    def test_follow_dirs(self, dirs, fin):
        self.assertTupleEqual(follow_directions(dirs), fin)

    @parameterized.expand([
        [[["forward", "5"], ["down", "5"], ["forward", "8"], ["up", "3"],
          ["down", "8"], ["forward", "2"]], (15, 80)],
    ])
    def test_follow_dirs_2(self, dirs, fin):
        self.assertTupleEqual(follow_directions(dirs), fin)


if __name__ == '__main__':
    print(">>> Start Main 02:")
    puzzle_input = read_lines_as_list("data/02.txt", split=" ")
    p_final = follow_directions(puzzle_input)
    print("Part 1): ", p_final, " -> ", p_final[0] * p_final[1])
    p_final_2 = follow_directions_2(puzzle_input)
    print("Part 2): ", p_final_2, " -> ", p_final_2[0] * p_final_2[1])
    print("End Main 02<<<")
Beispiel #8
0
 def test_basin_size(self, x, y, size):
     test_input = [[int(val) for val in line] for line in read_lines_as_list("data/09-test.txt", t=list)]
     self.assertEqual(get_basin_size(test_input, x, y), size)
Beispiel #9
0
 def test_find_optima(self):
     test_input = [[int(val) for val in line] for line in read_lines_as_list("data/09-test.txt", t=list)]
     low_points = find_low_points(test_input)
     self.assertEqual(sum(val[0] + 1 for val in low_points), 15)
     self.assertTupleEqual(low_points[0][1:], (1, 0))
Beispiel #10
0
 def test_lowest_adjacent(self, tx, ty, lowest):
     test_input = [[int(val) for val in line] for line in read_lines_as_list("data/09-test.txt", t=list)]
     self.assertEqual(get_lowest_adjacent(test_input, tx, ty), lowest)
Beispiel #11
0
        test_input = [[int(val) for val in line] for line in read_lines_as_list("data/09-test.txt", t=list)]
        self.assertEqual(get_lowest_adjacent(test_input, tx, ty), lowest)

    def test_find_optima(self):
        test_input = [[int(val) for val in line] for line in read_lines_as_list("data/09-test.txt", t=list)]
        low_points = find_low_points(test_input)
        self.assertEqual(sum(val[0] + 1 for val in low_points), 15)
        self.assertTupleEqual(low_points[0][1:], (1, 0))

    @parameterized.expand([
        [1, 0, 3],
        [9, 0, 9],
        [2, 2, 14],
        [6, 4, 9],
    ])
    def test_basin_size(self, x, y, size):
        test_input = [[int(val) for val in line] for line in read_lines_as_list("data/09-test.txt", t=list)]
        self.assertEqual(get_basin_size(test_input, x, y), size)


if __name__ == '__main__':
    print(">>> Start Main 09:")
    puzzle_input = [[int(val) for val in line] for line in read_lines_as_list("data/09.txt", t=list)]
    low_points = find_low_points(puzzle_input)
    print("Part 1): ", sum(val[0] + 1 for val in low_points))

    basin_sizes = [get_basin_size(puzzle_input, line[1], line[2]) for line in low_points]
    basin_sizes.sort(reverse=True)
    print("Part 2): ", basin_sizes[0] * basin_sizes[1] * basin_sizes[2])
    print("End Main 09<<<")
Beispiel #12
0
             [7, 9, 2, 2, 2, 8, 6, 8, 6, 6], [6, 7, 8, 9, 9, 9, 8, 7, 6, 6]]
        ],
    ])
    def test_nof_flashes(self, steps, flashes, board):
        updated_data, measured_flashes = model_octopus(
            deepcopy(self.test_data), steps)
        self.assertEqual(measured_flashes, flashes)
        self.assertEqual(updated_data, board)

    def test_example_board(self):
        test_data2 = [[1, 1, 1, 1, 1], [1, 9, 9, 9, 1], [1, 9, 1, 9, 1],
                      [1, 9, 9, 9, 1], [1, 1, 1, 1, 1]]
        updated_data, measured_flashes = model_octopus(deepcopy(test_data2), 1)
        test_solution = [[3, 4, 5, 4, 3], [4, 0, 0, 0, 4], [5, 0, 0, 0, 5],
                         [4, 0, 0, 0, 4], [3, 4, 5, 4, 3]]
        self.assertEqual(measured_flashes, 9)
        self.assertEqual(test_solution, updated_data)

    def test_synchronising(self):
        self.assertEqual(did_synchronise(deepcopy(self.test_data)), 195)


if __name__ == '__main__':
    print(">>> Start Main 11:")
    puzzle_input = [[int(val) for val in line]
                    for line in read_lines_as_list("data/11.txt", list)]
    updated_puzzle, puzzle_flashes = model_octopus(deepcopy(puzzle_input), 100)
    print("Part 1): ", puzzle_flashes)
    print("Part 2): ", did_synchronise(deepcopy(puzzle_input)))
    print("End Main 11<<<")