my solutions for advent of code 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.1 KiB

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, [])
for _ in range(len(gridVisited[1])):
gridVisited[0].append('.')
posHead[0] = posHead[0] + 1
for i in range(len(posTail)):
posTail[i][0] = posTail[i][0] + 1
elif direct == "R":
for i in range(len(gridVisited)):
gridVisited[i].append('.')
elif direct == "D":
gridVisited.append([])
tempx = len(gridVisited) - 1
for _ in range(len(gridVisited[0])):
gridVisited[tempx].append('.')
elif direct == "L":
for i in range(len(gridVisited)):
gridVisited[i].insert(0, '.')
posHead[1] = posHead[1] + 1
for i in range(len(posTail)):
posTail[i][1] = posTail[i][1] + 1
def checkDist():
for z in range(9):
if z == 0:
one = posHead
else:
one = posTail[z-1]
two = posTail[z]
tempa = one[1] - two[1]
tempb = one[0] - two[0]
tempd = tempa * tempb
if one[0] == two[0]:
if not(tempa == 1 or tempa == -1):
if tempa > 0:
posTail[z][1] += 1
elif tempa < 0:
posTail[z][1] -= 1
elif one[1] == two[1]:
if not(tempb == 1 or tempb == -1):
if tempb > 0:
posTail[z][0] += 1
elif tempb < 0:
posTail[z][0] -= 1
elif (tempd in [-2, -4]) and tempb < 0:
posTail[z][0] -= 1
posTail[z][1] += 1
elif (tempd in [2, 4]) and tempb > 0:
posTail[z][0] += 1
posTail[z][1] += 1
elif (tempd in [-2, -4]) and tempb > 0:
posTail[z][0] += 1
posTail[z][1] -= 1
elif (tempd in [2, 4]) and tempb < 0:
posTail[z][0] -= 1
posTail[z][1] -= 1
# print(posTail[8])
gridVisited[posTail[8][0]][posTail[8][1]] = '#'
# drawGrid()
for _ in range(9):
posTail.append([0,0])
with open('input9.txt','r') as f:
inp = f.read().splitlines(keepends=False)
direction = []
distance = []
for i in inp:
tempa, tempb = i.split(' ')
direction.append(tempa)
distance.append(int(tempb))
for x in range(len(direction)):
if direction[x] == 'U':
for y in range(distance[x]):
if posHead[0] == 0:
expandGrid("U")
posHead[0] -= 1
checkDist()
elif direction[x] == 'R':
for y in range(distance[x]):
if posHead[1] == len(gridVisited[0])-1:
expandGrid("R")
posHead[1] += 1
checkDist()
elif direction[x] == 'D':
for y in range(distance[x]):
if posHead[0] == len(gridVisited) - 1:
expandGrid("D")
posHead[0] += 1
checkDist()
elif direction[x] == 'L':
for y in range(distance[x]):
if posHead[1] == 0:
expandGrid("L")
posHead[1] -= 1
checkDist()
visited = 0
for i in gridVisited:
for x in i:
if x == '#':
visited += 1
print(visited)