day 9 part 1

This commit is contained in:
2022-12-09 17:22:42 +02:00
parent baaba9b6da
commit b1928afa6c

58
09/i.py Normal file
View File

@@ -0,0 +1,58 @@
import os
import math
input = open(os.path.dirname(__file__) +
"/input.txt", "r").readlines()
def distance(i: int, j: int):
return math.dist([i], [j])
tails = []
tail = [0, 0]
head = [0, 0] # x, y
for l in input:
moves = l.split()
print(moves)
for steps in range(int(moves[1])):
match moves[0]:
case 'R':
print(">")
head = [head[0]+1, head[1]]
if distance(head[0], tail[0]) > 1:
if (tail[1] != head[1]):
tail[1] = head[1]
tail[0] += 1
case 'L':
print("<")
head = [head[0]-1, head[1]]
if distance(tail[0], head[0]) > 1:
if tail[1] != head[1]:
tail[1] = head[1]
tail[0] -= 1
case 'U':
print("^")
head = [head[0], head[1]+1]
if distance(head[1], tail[1]) > 1:
if tail[0] != head[0]:
tail[0] = head[0]
tail[1] += 1
case 'D':
print("v")
head = [head[0], head[1]-1]
if distance(tail[1], head[1]) > 1:
if tail[0] != head[0]:
tail[0] = head[0]
tail[1] -= 1
print(head)
print(tail)
tails.append(tail.copy())
list = []
for i in tails:
list.append(str(i[0])+","+str(i[1]))
print(len(set(list)))