Skip to content

Instantly share code, notes, and snippets.

@sadraskol
Created December 14, 2022 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadraskol/4886dc07e3f164ef0c84e3fd927d0b65 to your computer and use it in GitHub Desktop.
Save sadraskol/4886dc07e3f164ef0c84e3fd927d0b65 to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
import sys
dir = {"R": (1, 0), "U": (0, 1), "L": (-1, 0), "D": (0, -1)}
def mul(a, v):
return (v[0] * a, v[1] * a)
def add(v1, v2):
x, y = v1
a, b = v2
return (x + a, y + b)
def diff(v1, v2):
x, y = v1
a, b = v2
return (x - a, y - b)
def distance(v1, v2):
x, y = v1
a, b = v2
return (a - x)**2 + (b - y)**2
physics = {
(2, 0): (1, 0),
(2, 1): (1, 1),
(1, 2): (1, 1),
(0, 2): (0, 1),
(-1, 2): (-1, 1),
(-2, 1): (-1, 1),
(-2, 0): (-1, 0),
(-2, -1): (-1, -1),
(-1, -2): (-1, -1),
(0, -2): (0, -1),
(1, -2): (1, -1),
(2, -1): (1, -1),
(-2, -2): (-1, -1),
(-2, 2): (-1, 1),
(2, -2): (1, -1),
(2, 2): (1, 1),
}
position = [(0, 0) for _ in range(10)]
tails = set([(0, 0)])
for l in sys.stdin.readlines():
cmd, num = [l.strip().split(' ')[0], int(l.strip().split(' ')[1])]
for _ in range(0, num):
position[0] = add(position[0], dir[cmd])
for i in range(1, 10):
new_head, tail = position[i-1:i+1]
new_tail = tail
if distance(new_head, tail) > 2:
d = physics[diff(new_head, tail)]
new_tail = add(tail, d)
position[i] = new_tail
tails.add(position[-1])
print(len(tails))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment