You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.9 KiB
76 lines
1.9 KiB
import re
|
|
|
|
|
|
def parse(l):
|
|
group = re.compile(r"(?P<dir>\w)(?P<int>\d+)").findall(l)
|
|
return group[0]
|
|
|
|
|
|
def move_ship(direction, amount, east, north, facing):
|
|
array = ['N', 'E', 'S', 'W']
|
|
amount = int(amount)
|
|
if direction == 'F':
|
|
direction = facing
|
|
if direction == 'N':
|
|
north += amount
|
|
if direction == 'S':
|
|
north -= amount
|
|
if direction == 'E':
|
|
east += amount
|
|
if direction == 'W':
|
|
east -= amount
|
|
if direction == 'L':
|
|
pos = array.index(facing)
|
|
pos2 = amount // 90
|
|
facing = array[pos - pos2]
|
|
if direction == 'R':
|
|
pos = array.index(facing)
|
|
pos2 = amount // 90
|
|
facing = array[(pos + pos2) % 4]
|
|
|
|
return east, north, facing
|
|
|
|
|
|
def move_ship_waypoint(direction, amount, ship, waypoint):
|
|
amount = int(amount)
|
|
if direction == 'F':
|
|
ship += waypoint*amount
|
|
if direction == 'N':
|
|
waypoint += amount * (0+1j)
|
|
if direction == 'S':
|
|
waypoint += amount * (0-1j)
|
|
if direction == 'E':
|
|
waypoint += amount * (1+0j)
|
|
if direction == 'W':
|
|
waypoint += amount * (-1+0j)
|
|
if direction == 'L':
|
|
waypoint = waypoint * 1j**(amount // 90)
|
|
if direction == 'R':
|
|
waypoint = waypoint * (-1j)**(amount // 90)
|
|
|
|
return ship, waypoint
|
|
|
|
|
|
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input12") as f:
|
|
lines = f.readlines()
|
|
|
|
# make dictionary with the values
|
|
directions = []
|
|
for line in lines:
|
|
directions.append(parse(line))
|
|
|
|
# current position [east / west, north / south, facing]
|
|
current = [0, 0, 'E']
|
|
|
|
for dire in directions:
|
|
current = move_ship(*dire, *current)
|
|
|
|
print(abs(current[0]) + abs(current[1]))
|
|
|
|
ship_position = 0+0j
|
|
waypoint_position = 10+1j
|
|
|
|
for dire in directions:
|
|
ship_position, waypoint_position = move_ship_waypoint(*dire, ship_position, waypoint_position)
|
|
|
|
print(abs(ship_position.real) + abs(ship_position.imag))
|
|
|