feat: day 9 part 1 complete

This commit is contained in:
2022-12-12 11:56:38 -05:00
parent bab0cf750a
commit a407435c7d
3 changed files with 2064 additions and 0 deletions

2000
day9/input.txt Normal file

File diff suppressed because it is too large Load Diff

56
day9/main.py Normal file
View File

@@ -0,0 +1,56 @@
import time
moves = []
input_file = open("input.txt", "r")
lines = input_file.readlines()
for line in lines:
line = line.strip()
moves.append((line[0], int(line.split(" ")[-1])))
head = (0, 0)
tail = (0, 0)
visited_positions = {tail}
def get_tails_new_pos(head, tail):
x_diff = head[0] - tail[0]
y_diff = head[1] - tail[1]
if abs(x_diff) <= 1 and abs(y_diff) <= 1:
return tail
new_x = tail[0]
new_y = tail[1]
if abs(x_diff) > 1 and abs(y_diff) >= 1:
new_x = tail[0] + x_diff // 2
new_y = tail[1] + y_diff
elif abs(y_diff) > 1 and abs(x_diff) >= 1:
new_y = tail[1] + y_diff // 2
new_x = tail[0] + x_diff
elif abs(x_diff) > 1:
new_x = tail[0] + x_diff // 2
elif abs(y_diff) > 1:
new_y = tail[1] + y_diff // 2
return (new_x, new_y)
for move in moves:
for _ in range(move[1]):
if move[0] == "U":
head = (head[0] + 1, head[1])
elif move[0] == "D":
head = (head[0] - 1, head[1])
elif move[0] == "R":
head = (head[0], head[1] + 1)
elif move[0] == "L":
head = (head[0], head[1] - 1)
tail = get_tails_new_pos(head, tail)
visited_positions.add(tail)
print("Part 1: " + str(len(visited_positions)))

8
input.txt Normal file
View File

@@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2