80 lines
1.7 KiB
Python
80 lines
1.7 KiB
Python
import aoclib
|
|
|
|
DAY = 12
|
|
TEST_SOLUTION_PART1 = 25
|
|
TEST_SOLUTION_PART2 = 286
|
|
FACING = ['N', 'E', 'S', 'W']
|
|
|
|
|
|
def move(instruction, x, y, j):
|
|
where = instruction[0]
|
|
dist = int(instruction[1:])
|
|
|
|
if where == 'F':
|
|
where = FACING[j]
|
|
|
|
if where == 'N':
|
|
y -= dist
|
|
elif where == 'S':
|
|
y += dist
|
|
elif where == 'E':
|
|
x += dist
|
|
elif where == 'W':
|
|
x -= dist
|
|
elif where == 'R':
|
|
j = (j + int(dist / 90)) % 4
|
|
elif where == 'L':
|
|
j = (j - int(dist / 90)) % 4
|
|
|
|
return x, y, j
|
|
|
|
|
|
def part1(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
|
|
|
posX = 0
|
|
posY = 0
|
|
face = 1
|
|
for direction in my_input:
|
|
posX, posY, face = move(direction, posX, posY, face)
|
|
|
|
return abs(posX) + abs(posY)
|
|
|
|
|
|
def moveWaypoint(inst, sx, sy, wx, wy):
|
|
where = inst[0]
|
|
dist = int(inst[1:])
|
|
|
|
if where == 'F':
|
|
sx += wx * dist
|
|
sy += wy * dist
|
|
|
|
if where == 'N':
|
|
wy -= dist
|
|
elif where == 'S':
|
|
wy += dist
|
|
elif where == 'E':
|
|
wx += dist
|
|
elif where == 'W':
|
|
wx -= dist
|
|
elif where == 'R':
|
|
for i in range(int(dist / 90)):
|
|
wx, wy = -wy, wx
|
|
elif where == 'L':
|
|
for i in range(int(dist / 90)):
|
|
wx, wy = wy, -wx
|
|
|
|
return sx, sy, wx, wy
|
|
|
|
|
|
def part2(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
|
posShipX = 0
|
|
posShipY = 0
|
|
posWaypointX = 10
|
|
posWaypointY = -1
|
|
for direction in my_input:
|
|
posShipX, posShipY, posWaypointX, posWaypointY = moveWaypoint(direction, posShipX, posShipY, posWaypointX, posWaypointY)
|
|
|
|
return abs(posShipX) + abs(posShipY)
|