Skip to content

Instantly share code, notes, and snippets.

@plallin
Created May 7, 2017 19:56
Show Gist options
  • Save plallin/24401782cd21e6c625c2723904a57962 to your computer and use it in GitHub Desktop.
Save plallin/24401782cd21e6c625c2723904a57962 to your computer and use it in GitHub Desktop.
instructions = "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".split(", ")
instructions = [[x[0], int(x[1:])] for x in instructions]
def move(position, direction, number_of_step):
if direction == 'N':
return [position[0], position[1] - number_of_step]
elif direction == 'E':
return [position[0] + number_of_step, position[1]]
elif direction == 'S':
return [position[0], position[1] + number_of_step]
elif direction == 'W':
return [position[0] - number_of_step, position[1]]
def direction(movement, current_direction):
directions = ['N', 'E', 'S', 'W']
if movement == 'R':
return directions[(directions.index(current_direction) + 1) % len(directions)]
elif movement == 'L':
return directions[directions.index(current_direction) - 1]
def calculate_position(start_position, instructions):
curr_position = start_position
current_direction = 'N'
for instruction in instructions:
current_direction = direction(instruction[0], current_direction)
curr_position = move(curr_position, current_direction, instruction[1])
return curr_position
def absolute_distance(coord1):
return abs(coord1[0]) + abs(coord1[1])
def main(instructions):
final_position = calculate_position([0, 0], instructions)
print("final position: " + str(final_position))
final_distance = absolute_distance(final_position)
print("blocks away: " + str(final_distance))
main(instructions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment