You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1019 B
Python
49 lines
1019 B
Python
lines = [x.strip() for x in open('input', 'r').readlines()]
|
|
|
|
east = 0
|
|
north = 0
|
|
# 0 = E, 1 = N, 2 = W, 3 = S
|
|
|
|
wp_east = 10
|
|
wp_north = 1
|
|
|
|
|
|
def serialize():
|
|
"""Debug a position"""
|
|
return f'N{north} E{east} / N{wp_north} E{wp_east}'
|
|
|
|
|
|
def rot90(x, y):
|
|
"""Rotates counter clockwise"""
|
|
return [-y, x]
|
|
|
|
|
|
for line in lines:
|
|
pos = serialize()
|
|
command = line[0]
|
|
number = int(line[1:])
|
|
|
|
if command in ['S', 'W', 'R']:
|
|
number *= -1
|
|
|
|
if command in ['N', 'S']:
|
|
wp_north += number
|
|
elif command in ['E', 'W']:
|
|
wp_east += number
|
|
elif command in ['R', 'L']:
|
|
number = number // 90 % 4
|
|
for _ in range(number):
|
|
wp_east, wp_north = rot90(wp_east, wp_north)
|
|
elif command in ['F']:
|
|
north += wp_north * number
|
|
east += wp_east * number
|
|
else:
|
|
raise Exception(f'Unknown command: {command}')
|
|
|
|
print(f'{line}: \t{pos} -> {serialize()}')
|
|
|
|
print()
|
|
manhattan = abs(north) + abs(east)
|
|
print('Solution')
|
|
print(manhattan)
|