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.

45 lines
953 B
Python

4 years ago
import logging
lines = [x.strip() for x in open('input', 'r').readlines()]
east = 0
north = 0
# 0 = E, 1 = N, 2 = W, 3 = S
facing = 0
def serialize():
return f'N{north} E{east} F{facing}'
for line in lines:
pos = serialize()
command = line[0]
number = int(line[1:])
if command in ['S', 'W', 'R']:
number *= -1
elif command in ['F'] and facing in [2, 3]:
number *= -1
if command in ['N', 'S']:
north += number
elif command in ['E', 'W']:
east += number
elif command in ['R', 'L']:
facing = (facing + number // 90) % 4
elif command in ['F']:
if facing % 2 == 0:
east += number
else:
north += number
else:
raise Exception(f'Unknown command: {command}')
print(f'{line}: \t{pos} -> {serialize()}')
print()
print(f'{north=} {east=} {facing=}')
manhattan = abs(north) + abs(east)
print('Solution')
print(manhattan)