day12: use acceptable variable names
This commit is contained in:
parent
3c1a4269f8
commit
20e35877e2
50
day12.py
50
day12.py
@ -6,27 +6,27 @@ TEST_SOLUTION_PART2 = 286
|
||||
FACING = ['N', 'E', 'S', 'W']
|
||||
|
||||
|
||||
def move(instruction, x, y, j):
|
||||
def move(instruction, posX, posY, facing):
|
||||
where = instruction[0]
|
||||
dist = int(instruction[1:])
|
||||
|
||||
if where == 'F':
|
||||
where = FACING[j]
|
||||
where = FACING[facing]
|
||||
|
||||
if where == 'N':
|
||||
y -= dist
|
||||
posY -= dist
|
||||
elif where == 'S':
|
||||
y += dist
|
||||
posY += dist
|
||||
elif where == 'E':
|
||||
x += dist
|
||||
posX += dist
|
||||
elif where == 'W':
|
||||
x -= dist
|
||||
posX -= dist
|
||||
elif where == 'R':
|
||||
j = (j + int(dist / 90)) % 4
|
||||
facing = (facing + int(dist / 90)) % 4
|
||||
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):
|
||||
@ -35,36 +35,36 @@ def part1(test_mode=False):
|
||||
posX = 0
|
||||
posY = 0
|
||||
face = 1
|
||||
for direction in my_input:
|
||||
posX, posY, face = move(direction, posX, posY, face)
|
||||
for instruction in my_input:
|
||||
posX, posY, face = move(instruction, posX, posY, face)
|
||||
|
||||
return abs(posX) + abs(posY)
|
||||
|
||||
|
||||
def moveWaypoint(inst, sx, sy, wx, wy):
|
||||
where = inst[0]
|
||||
dist = int(inst[1:])
|
||||
def moveWaypoint(instruction, posShipX, posShipY, posWaypointX, posWaypointY):
|
||||
where = instruction[0]
|
||||
dist = int(instruction[1:])
|
||||
|
||||
if where == 'F':
|
||||
sx += wx * dist
|
||||
sy += wy * dist
|
||||
posShipX += posWaypointX * dist
|
||||
posShipY += posWaypointY * dist
|
||||
|
||||
if where == 'N':
|
||||
wy -= dist
|
||||
posWaypointY -= dist
|
||||
elif where == 'S':
|
||||
wy += dist
|
||||
posWaypointY += dist
|
||||
elif where == 'E':
|
||||
wx += dist
|
||||
posWaypointX += dist
|
||||
elif where == 'W':
|
||||
wx -= dist
|
||||
posWaypointX -= dist
|
||||
elif where == 'R':
|
||||
for i in range(int(dist / 90)):
|
||||
wx, wy = -wy, wx
|
||||
posWaypointX, posWaypointY = -posWaypointY, posWaypointX
|
||||
elif where == 'L':
|
||||
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):
|
||||
@ -73,7 +73,7 @@ def part2(test_mode=False):
|
||||
posShipY = 0
|
||||
posWaypointX = 10
|
||||
posWaypointY = -1
|
||||
for direction in my_input:
|
||||
posShipX, posShipY, posWaypointX, posWaypointY = moveWaypoint(direction, posShipX, posShipY, posWaypointX, posWaypointY)
|
||||
for instruction in my_input:
|
||||
posShipX, posShipY, posWaypointX, posWaypointY = moveWaypoint(instruction, posShipX, posShipY, posWaypointX, posWaypointY)
|
||||
|
||||
return abs(posShipX) + abs(posShipY)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user