Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Last active February 10, 2017 22:58
Show Gist options
  • Save NotTheEconomist/c876d7f74637b0337dc34fe2d9ab3a16 to your computer and use it in GitHub Desktop.
Save NotTheEconomist/c876d7f74637b0337dc34fe2d9ab3a16 to your computer and use it in GitHub Desktop.
from enum import Enum
puzzle = "L5, R1, L5, L1, R5, R1, R1, L4, L1, L3, R2, R4, L4, L1, L1, R2, R4, R3, L1, R4, L4, L5, L4, R4, L5, R1, R5, L2, R1, R3, L2, L4, L4, R1, L192, R5, R1, R4, L5, L4, R5, L1, L1, R48, R5, R5, L2, R4, R4, R1, R3, L1, L4, L5, R1, L4, L2, L5, R5, L2, R74, R4, L1, R188, R5, L4, L2, R5, R2, L4, R4, R3, R3, R2, R1, L3, L2, L5, L5, L2, L1, R1, R5, R4, L3, R5, L1, L3, R4, L1, L3, L2, R1, R3, R2, R5, L3, L1, L1, R5, L4, L5, R5, R2, L5, R2, L1, L5, L3, L5, L5, L1, R1, L4, L3, L1, R2, R5, L1, L3, R4, R5, L4, L1, R5, L1, R5, R5, R5, R2, R1, R2, L5, L5, L5, R4, L5, L4, L4, R5, L2, R1, R5, L1, L5, R4, L3, R4, L2, R3, R3, R3, L2, L2, L2, L1, L4, R3, L4, L2, R2, R5, L1, R2"
Directions = Enum("Directions", "NORTH EAST SOUTH WEST")
def turn(facing, vector):
# Handle wrapping
if facing == Directions.WEST and vector == "R":
return Directions.NORTH
elif facing == Directions.NORTH and vector == "L":
return Directions.WEST
# Handle non-wrapping
else:
return Directions(facing.value + (1 if vector == "R" else (-1)))
results = {}
facing = Directions.NORTH
for step in puzzle.split(", "):
vector, steps = step[0], int(step[1:])
facing = turn(facing, steps)
results.setdefault(facing, 0) += steps
walk_distance = abs(results[Directions.NORTH] - results[Directions.SOUTH]) + \
abs(results[Directions.EAST] - results[Directions.WEST])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment