Skip to content

Instantly share code, notes, and snippets.

@cpilsworth
Created December 9, 2019 07:50
Show Gist options
  • Save cpilsworth/bdc1b607090d1508604cdca9ddb2c6c5 to your computer and use it in GitHub Desktop.
Save cpilsworth/bdc1b607090d1508604cdca9ddb2c6c5 to your computer and use it in GitHub Desktop.
import collections
import sys
Point = collections.namedtuple("Point",['x', 'y'])
Command = collections.namedtuple("Command", ['direction', 'magnitude'])
start_pos = Point(0,0)
map = [[]]
def cmd(instruction):
return Command(direction=instruction[0:1], magnitude=int(instruction[1:]))
def move(pos, cmd):
print(pos)
if cmd.direction == "U":
return [[Point(pos.x, y + 1) for y in range(cmd.magnitude) ]]
elif cmd.direction == "D":
return [[Point(pos.x, y - 1) for y in range(cmd.magnitude) ]]
elif cmd.direction == "R":
return [[Point(x + 1, pos.y) for x in range(cmd.magnitude) ]]
elif cmd.direction == "L":
return [[Point(x - 1, pos.y) for x in range(cmd.magnitude) ]]
else:
print({cmd.direction})
raise Exception(f"Unrecognised direction: {cmd.direction}")
x = lambda k: k.x
y = lambda k: k.y
def map_size(positions):
[( Point(min(p.x, key=x), min(p.y,key=y)), Point(max(p.x, key=x), max(p.y,key=y))) for list in points for p in list ]
## array of position arrays
positions = [[start_pos]]
instructions = [ cmd(x) for x in 'R8,U1'.split(',')]
for instruction in instructions:
# the last element of each array is the location of each step
last_pos = positions[-1][-1]
positions += move(last_pos, instruction)
print (positions[-1][-1])
print(positions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment