cheated p2

This commit is contained in:
2022-12-10 06:07:49 +02:00
parent b1928afa6c
commit aac89964d2

75
09/i.py
View File

@@ -5,54 +5,43 @@ input = open(os.path.dirname(__file__) +
"/input.txt", "r").readlines()
def distance(i: int, j: int):
return math.dist([i], [j])
def dir(d):
match d:
case 'R': return (1, 0)
case 'L': return (-1, 0)
case 'U': return (0, 1)
case 'D': return (0, -1)
max = 10
tails = []
tail = [0, 0]
head = [0, 0] # x, y
rope = [(0, 0) for i in range(max)]
for l in input:
moves = l.split()
print(moves)
m = dir(moves[0])
for steps in range(int(moves[1])):
rope[0] = [rope[0][0]+m[0], rope[0][1]+m[1]]
for r in range(max-1):
k, kn = rope[r:r+2]
dx, dy = k[0]-kn[0], k[1] - kn[1]
dist = abs(dx) + abs(dy)
if dist == 2:
if k[0] == kn[0]:
rope[r+1] = (rope[r+1][0], rope[r+1][1]+dy//2)
elif k[1] == kn[1]:
rope[r+1] = (rope[r+1][0]+dx//2, rope[r+1][1])
elif dist == 3:
if abs(dx) == 2:
rope[r+1] = (rope[r+1][0]+dx//2, rope[r][1])
elif abs(dy) == 2:
rope[r+1] = (rope[r][0], rope[r+1][1]+dy//2)
elif dist == 4:
rope[r+1] = (rope[r+1][0]+dx//2, rope[r+1][1]+dy//2)
tails.append(rope[-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 = []
l = []
for i in tails:
list.append(str(i[0])+","+str(i[1]))
print(len(set(list)))
l.append(str(i[0])+","+str(i[1]))
print(len(set(l)))