day12: use acceptable variable names

This commit is contained in:
Stefan Harmuth 2020-12-12 06:47:05 +01:00
parent 3c1a4269f8
commit 20e35877e2

View File

@ -6,27 +6,27 @@ TEST_SOLUTION_PART2 = 286
FACING = ['N', 'E', 'S', 'W'] FACING = ['N', 'E', 'S', 'W']
def move(instruction, x, y, j): def move(instruction, posX, posY, facing):
where = instruction[0] where = instruction[0]
dist = int(instruction[1:]) dist = int(instruction[1:])
if where == 'F': if where == 'F':
where = FACING[j] where = FACING[facing]
if where == 'N': if where == 'N':
y -= dist posY -= dist
elif where == 'S': elif where == 'S':
y += dist posY += dist
elif where == 'E': elif where == 'E':
x += dist posX += dist
elif where == 'W': elif where == 'W':
x -= dist posX -= dist
elif where == 'R': elif where == 'R':
j = (j + int(dist / 90)) % 4 facing = (facing + int(dist / 90)) % 4
elif where == 'L': elif where == 'L':
j = (j - int(dist / 90)) % 4 facing = (facing - int(dist / 90)) % 4
return x, y, j return posX, posY, facing
def part1(test_mode=False): def part1(test_mode=False):
@ -35,36 +35,36 @@ def part1(test_mode=False):
posX = 0 posX = 0
posY = 0 posY = 0
face = 1 face = 1
for direction in my_input: for instruction in my_input:
posX, posY, face = move(direction, posX, posY, face) posX, posY, face = move(instruction, posX, posY, face)
return abs(posX) + abs(posY) return abs(posX) + abs(posY)
def moveWaypoint(inst, sx, sy, wx, wy): def moveWaypoint(instruction, posShipX, posShipY, posWaypointX, posWaypointY):
where = inst[0] where = instruction[0]
dist = int(inst[1:]) dist = int(instruction[1:])
if where == 'F': if where == 'F':
sx += wx * dist posShipX += posWaypointX * dist
sy += wy * dist posShipY += posWaypointY * dist
if where == 'N': if where == 'N':
wy -= dist posWaypointY -= dist
elif where == 'S': elif where == 'S':
wy += dist posWaypointY += dist
elif where == 'E': elif where == 'E':
wx += dist posWaypointX += dist
elif where == 'W': elif where == 'W':
wx -= dist posWaypointX -= dist
elif where == 'R': elif where == 'R':
for i in range(int(dist / 90)): for i in range(int(dist / 90)):
wx, wy = -wy, wx posWaypointX, posWaypointY = -posWaypointY, posWaypointX
elif where == 'L': elif where == 'L':
for i in range(int(dist / 90)): for i in range(int(dist / 90)):
wx, wy = wy, -wx posWaypointX, posWaypointY = posWaypointY, -posWaypointX
return sx, sy, wx, wy return posShipX, posShipY, posWaypointX, posWaypointY
def part2(test_mode=False): def part2(test_mode=False):
@ -73,7 +73,7 @@ def part2(test_mode=False):
posShipY = 0 posShipY = 0
posWaypointX = 10 posWaypointX = 10
posWaypointY = -1 posWaypointY = -1
for direction in my_input: for instruction in my_input:
posShipX, posShipY, posWaypointX, posWaypointY = moveWaypoint(direction, posShipX, posShipY, posWaypointX, posWaypointY) posShipX, posShipY, posWaypointX, posWaypointY = moveWaypoint(instruction, posShipX, posShipY, posWaypointX, posWaypointY)
return abs(posShipX) + abs(posShipY) return abs(posShipX) + abs(posShipY)