From 3873358c14ca92b3ded56c5ef34d2a874310bd14 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 9 Dec 2022 10:29:28 +0100 Subject: [PATCH] Solve day9 part 2 --- 2022/day09_bridge.py | 34 +++++++++++++++++++++++++++++++--- 2022/inputs/day09-test2 | 8 ++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 2022/inputs/day09-test2 diff --git a/2022/day09_bridge.py b/2022/day09_bridge.py index a7ca75f..d5c168c 100644 --- a/2022/day09_bridge.py +++ b/2022/day09_bridge.py @@ -87,9 +87,37 @@ def solve_part_1(instructions: DataType) -> int: def solve_part_2(instructions: DataType) -> int: - return 0 + head = Point() + tails = [Point() for _ in range(9)] + visited = set() + for instruction in instructions: + print(instruction) + count = instruction[1] + while count > 0: + head.move(instruction) + tails[0].follow(head) + for i in range(1, len(tails)): + tails[i].follow(tails[i - 1]) + tail_tuple = tails[-1].tuple + visited.add(tail_tuple) + count -= 1 + # print_grid([head, *tails]) + return len(visited) + + +def print_grid(rope: list[Point], size: tuple[int, int] = (21, 27)) -> None: + rope = [knot.tuple for knot in rope] + for row in range(int(-size[0] / 2), int(size[0] / 2)): + for col in range(int(-size[1] / 2), int(size[1] / 2)): + try: + char = rope.index((col, row)) + except ValueError: + char = "." + print(char, end="") + print() if __name__ == "__main__": - main("inputs/day09-test1", expected_part_1=13) - main("inputs/day09", expected_part_1=5619) + main("inputs/day09-test1", expected_part_1=13, expected_part_2=1) + main("inputs/day09-test2", expected_part_2=36) + main("inputs/day09", expected_part_1=5619, expected_part_2=2376) diff --git a/2022/inputs/day09-test2 b/2022/inputs/day09-test2 new file mode 100644 index 0000000..60bd43b --- /dev/null +++ b/2022/inputs/day09-test2 @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20