Advent of Code 2020 – Day 12
So far, I think Day 12 has been my favorite puzzle. Not too hard, not too easy, it feels like an actually useful problem to solve instead of some arbitrary manipulation of a bunch of numbers.
This puzzle involved moving a boat based on a set of instructions. The only part that felt off was that sometimes the boat moves forward, and sometimes it just sort of, slides in one cardinal direction.
def rotator(degrees, current):
change = degrees+current
if change < 0:
change = change+360
if change >=360:
change = change-360
#print change
return change
with open('day12data.txt') as f:
lines = [line.rstrip() for line in f]
xpos=0
ypos=0
#0 is East
orientation=0
cardinals = ["E","S","W","N"]
for i in lines:
#print i
next_orient=i[0]
amount=int(i[1:])
print "Ship Moves: "+next_orient+" direction "+str(amount)+" times"
if next_orient == "L":
orientation = rotator(-amount,orientation)
if next_orient == "R":
orientation = rotator(amount,orientation)
if next_orient == "F":
next_orient = cardinals[(orientation/90)]
#print amount
#print next_orient
#print cardinals[(orientation/90)]
if next_orient == "N":
xpos=xpos+amount
if next_orient == "S":
xpos=xpos-amount
if next_orient == "E":
ypos=ypos+amount
if next_orient == "W":
ypos=ypos-amount
print xpos
print ypos
print abs(xpos)+abs(ypos)
This whole thing was a pretty straight forward string of conditionals, and a few functions to rotate the orientation. I am actually particularly proud of how I handled the orientation here, using an array. Your orientation is expressed in degrees, which when divided by 90 gives you a number 0, 1, 2, or 3, which become indexes in an array of cardinal directions.
Part two changed things a bit, and solved the “how does a boat facing east move north” question. Instead of moving the boat, you move a navigational buoy. The boat old moves when you get a forward command, and it moves towards the buoy.
def rotator(degrees, current):
global xpos
global ypos
change = degrees+current
if change < 0:
change = change+360
if change >=360:
change = change-360
#print change
#return change
if change == 90 or change == 270:
temp=xpos
xpos=-ypos
ypos=temp
if change == 180 or change == 270:
xpos=-xpos
ypos=-ypos
with open('day12data.txt') as f:
lines = [line.rstrip() for line in f]
xpos=1
ypos=10
shipx=0
shipy=0
#0 is East
orientation=0
cardinals = ["E","S","W","N"]
for i in lines:
#print i
next_orient=i[0]
amount=int(i[1:])
if next_orient == "L":
rotator(-amount,orientation)
if next_orient == "R":
rotator(amount,orientation)
if next_orient == "N":
xpos=xpos+amount
if next_orient == "S":
xpos=xpos-amount
if next_orient == "E":
ypos=ypos+amount
if next_orient == "W":
ypos=ypos-amount
if next_orient == "F":
shipx=shipx+(xpos)*amount
shipy=shipy+(ypos)*amount
print "Ship Moves: "+next_orient+" direction "+str(amount)+" times"
print "Beacon: "+str(xpos)+"E/W "+str(ypos)+"N/S"
print "Ship: "+str(shipx)+"E/W "+str(shipy)+"N/S"
print abs(shipx)+abs(shipy)
print shipx
print shipy
The sad part is, RIP clever Orientation gimmick, though the array is still in there for posterity. When the buoy rotates, it doesn’t face a different direction, it rotates around the ship, so the functions just became swapped coordinates instead of degrees corresponding to an orientation.
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.