add remaining code and input files from 2020
This commit is contained in:
1 parent
c10382b0c0
commit
23b2e01280
41 files changed
+11320
-37
No files matched your search
@@ -0,0 +1,76 @@
|
||||
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))
|
||||
Reference in new issue
Block a user