Solve day9 part 2

This commit is contained in:
Gabriel Augendre 2022-12-09 10:29:28 +01:00
parent 41fec62b96
commit 3873358c14
2 changed files with 39 additions and 3 deletions

View file

@ -87,9 +87,37 @@ def solve_part_1(instructions: DataType) -> int:
def solve_part_2(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__": if __name__ == "__main__":
main("inputs/day09-test1", expected_part_1=13) main("inputs/day09-test1", expected_part_1=13, expected_part_2=1)
main("inputs/day09", expected_part_1=5619) main("inputs/day09-test2", expected_part_2=36)
main("inputs/day09", expected_part_1=5619, expected_part_2=2376)

8
2022/inputs/day09-test2 Normal file
View file

@ -0,0 +1,8 @@
R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20