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