Skip to content

Commit

Permalink
cleaned up day9 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Sullivan committed Dec 9, 2022
1 parent 38ae4ab commit d060c82
Showing 1 changed file with 1 addition and 43 deletions.
44 changes: 1 addition & 43 deletions src/advent_of_code/day9/part2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,9 @@
Simulate your complete series of motions on a larger rope with ten knots. How many positions does the tail of the rope visit at least once?
"""

from typing import Literal

import numpy as np

Direction = Literal["R", "L", "U", "D"]


def update_head_position(
head_position: tuple[int, int], direction: Direction
) -> tuple[int, int]:
if direction == "R":
return (head_position[0], head_position[1] + 1)
elif direction == "L":
return (head_position[0], head_position[1] - 1)
elif direction == "U":
return (head_position[0] - 1, head_position[1])
else:
return (head_position[0] + 1, head_position[1])


def update_tail_position(
head_position: tuple[int, int], tail_position: tuple[int, int]
) -> tuple[int, int]:
x_direction_diff = head_position[1] - tail_position[1]
y_direction_diff = head_position[0] - tail_position[0]

abs_x = abs(x_direction_diff)
abs_y = abs(y_direction_diff)
if abs_x < 2 and abs_y < 2: # check if diagonal, adjacent or on top
return tail_position
elif abs_y == 0: # horizontal move
return tail_position[0], tail_position[1] + (x_direction_diff // 2)
elif abs_x == 0: # vertical move
return tail_position[0] + (y_direction_diff // 2), tail_position[1]
else:
if x_direction_diff < 0:
x_move = -1
else:
x_move = 1
if y_direction_diff < 0:
y_move = -1
else:
y_move = 1
return tail_position[0] + y_move, tail_position[1] + x_move
from advent_of_code.day9.part1 import update_head_position, update_tail_position


def num_visited_positions_multi_knots(input_str: str) -> int:
Expand Down

0 comments on commit d060c82

Please sign in to comment.