From 54f5746a8ab52e4e4540230ec053275ff6727907 Mon Sep 17 00:00:00 2001 From: Luna Lailatova Date: Fri, 9 Dec 2022 22:54:09 +0100 Subject: [PATCH] day9 finished --- day9-2.py | 57 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/day9-2.py b/day9-2.py index 20a3d32..9e05322 100644 --- a/day9-2.py +++ b/day9-2.py @@ -1,3 +1,39 @@ +gridVisited =[['#']] +posHead = [0, 0] +posTail = [] + +# drawgrid only used for visualized debugging +def drawGrid(): + visualGrid = [] + + for yCoord in range(len(gridVisited)): + visualGridLine = [] + + for xCoord in range(len(gridVisited[0])): + if posHead[0] == yCoord and posHead[1] == xCoord: + visualGridLine.append('H') + continue + + haveAppended = False + for tp in range(9): + if posTail[tp][0] == yCoord and posTail[tp][1] == xCoord: + visualGridLine.append(str(tp+1)) + haveAppended = True + break + + if not haveAppended: + if gridVisited[yCoord][xCoord] == '#': + visualGridLine.append('#') + else: + visualGridLine.append('.') + + visualGrid.append(visualGridLine) + + for ln in visualGrid: + print("".join(ln)) + + input("Ready for next move?") + def expandGrid(direct): if direct == "U": gridVisited.insert(0, []) @@ -31,6 +67,7 @@ def checkDist(): tempa = one[1] - two[1] tempb = one[0] - two[0] tempc = tempa + tempb + tempd = tempa * tempb if one[0] == two[0]: if not(tempa == 1 or tempa == -1): if tempa > 0: @@ -43,28 +80,28 @@ def checkDist(): posTail[z][0] += 1 elif tempb < 0: posTail[z][0] -= 1 - elif (tempc == -1 or tempc == 1) and tempb < 0: + elif (tempc in [-1, 0, 1]) and tempb < 0 and tempd != -1: posTail[z][0] -= 1 posTail[z][1] += 1 - elif tempc == 3: + elif tempc in [3, 4]: posTail[z][0] += 1 posTail[z][1] += 1 - elif (tempc == -1 or tempc == 1) and tempb > 0: + elif (tempc in [-1, 0, 1]) and tempb > 0 and tempd != -1: posTail[z][0] += 1 posTail[z][1] -= 1 - elif tempc == -3: + elif tempc in [-4, -3]: posTail[z][0] -= 1 posTail[z][1] -= 1 - print(posTail[8]) + + # print(posTail[8]) gridVisited[posTail[8][0]][posTail[8][1]] = '#' -gridVisited =[['#']] -posHead = [0, 0] -posTail = [] -for z in range(9): + # drawGrid() + +for _ in range(9): posTail.append([0,0]) -with open('in.txt','r') as f: +with open('input9.txt','r') as f: inp = f.read().splitlines(keepends=False) direction = []