From 12f551dbfef138880b29ba6abe4576714ae942cb Mon Sep 17 00:00:00 2001 From: blenovo Date: Sat, 2 Aug 2025 03:13:50 +0200 Subject: summer warmup sesh part 2 --- 2020/aoc2020-d12.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 2020/aoc2020-d12.py (limited to '2020/aoc2020-d12.py') diff --git a/2020/aoc2020-d12.py b/2020/aoc2020-d12.py new file mode 100644 index 0000000..4f70f79 --- /dev/null +++ b/2020/aoc2020-d12.py @@ -0,0 +1,93 @@ +#advent of code 2020 +#day 12 +#Im experimenting with different parsing, +#the [:-1] is for skipping the empty line +#big mistake: giving north a negative value, +#which meant that my left and right turns where flipped +#changed it after getting the correct value +navigationInstructions = [line for line in open("12.in","r").read().split("\n")][:-1]; + +directions = dict(); +directions[0] = (0,1); #east +directions[1] = (-1,0); #south +directions[2] = (0,-1); #west +directions[3] = (1,0); #north + +#part 1 +def actions(act,val,face): + if act == "N": + return (val,0,face); + elif act =="S": + return (-val,0,face); + elif act == "E": + return (0,val,face); + elif act == "W": + return (0,-val,face); + elif act == "L": + turn = val//90; + tempF = (4+face-turn)%4; + return (0,0,tempF); + elif act == "R": + turn = val//90; + tempF = (4+face+turn)%4 + return (0,0,tempF); + elif act == "F": + mody,modx = directions[face]; + return (mody*val,modx*val,face); + +#part 2 +def NewActions(act,val,wayY,wayX): + if act == "N": + return (0,0,wayY+val,wayX); + elif act =="S": + return (0,0,wayY-val,wayX); + elif act == "E": + return (0,0,wayY,wayX+val); + elif act == "W": + return (0,0,wayY,wayX-val); + elif act == "L": + turn = val//90; + tempW = (wayY,wayX); + for t in range(turn): + tempW = (tempW[1],-tempW[0]); + return (0,0,tempW[0],tempW[1]); + elif act == "R": + turn = val//90; + tempW = (wayY,wayX); + for t in range(turn): + tempW = (-tempW[1],tempW[0]); + return (0,0,tempW[0],tempW[1]); + elif act == "F": + return (wayY*val,wayX*val,wayY,wayX); + else: + return False; + +#ship coordinates, part 1 +X1 = 0; +Y1 = 0; +D1 = 0; #direction +#ship coordinates, part 2 +X2,Y2,D2 = 0,0,0; +WX, WY = 10,1; #waypoint relative coordinates + +for instr in navigationInstructions: + action = instr[0]; + value = int(instr[1:]); + dy1,dx1,dd1 = actions(action,value,D1); #part 1 + Y1 += dy1; + X1 += dx1; + D1 = dd1; + dy2,dx2,dwy,dwx = NewActions(action,value,WY, WX); #part 2 + Y2 += dy2; + X2 += dx2; + WY = dwy; + WX = dwx; + +if X1 < 0: X1 = -X1; +if Y1 < 0: Y1 = -Y1; +p1 = X1 + Y1; +if X2 < 0: X2 = -X2; +if Y2 < 0: Y2 = -Y2; +p2 = X2 + Y2; +print("part 1 =",p1); +print("part 2 =",p2); -- cgit v1.2.3