import re #create a class to store the map class plan: def __init__(self): self.grid = [] self.goTo = [(0, 1), (1, 0), (0, -1), (-1, 0)] def addLine(self, inputLine): inputLine = inputLine.replace(' ', '+') self.grid.append(list(inputLine)) def testPrint(self): for line in self.grid: for point in line: print(point, end = ' ') print('\n') def setStart(self): self.direction = 0 self.column = self.grid[0].index('.') self.row = 0 maxL = 0 for line in self.grid: if len(line) > maxL: maxL = len(line) for i in range(len(self.grid)): while len(self.grid[i]) < maxL: self.grid[i].append('+') def getInstruction(self, instruction): if instruction.isnumeric(): self.move(int(instruction)) if instruction.isalpha(): self.turn(instruction) def turn(self, direction): if direction == 'L': self.direction -= 1 if self.direction < 0: self.direction = 3 elif direction == 'R': self.direction = (self.direction + 1) % 4 def move(self, distance): for _ in range(distance): directionChange = self.direction nextRow = self.row + self.goTo[self.direction][0] nextCol = self.column + self.goTo[self.direction][1] if nextRow < 0 or nextCol < 0 or nextRow >= len(self.grid) or nextCol >= len(self.grid[nextRow]) or self.grid[nextRow][nextCol] == '+': nextRow, nextCol, directionChange = self.wrapAround(self.row, self.column) if self.grid[nextRow][nextCol] == '#': return self.direction = directionChange self.column = nextCol self.row = nextRow def wrapAround(self, nextRow, nextCol): if self.direction == 0: if nextRow < 50: nextCol = 99 nextRow = 149 - nextRow directionChange = 2 elif nextRow < 100: nextCol = 50 + nextRow nextRow = 49 directionChange = 3 elif nextRow < 150: nextRow = 149 - nextRow nextCol = 149 directionChange = 2 else: nextCol = nextRow - 100 nextRow = 149 directionChange = 3 elif self.direction == 1: if nextCol < 50: nextCol += 100 nextRow = 0 directionChange = 1 elif nextCol < 100: nextRow = 100 + nextCol nextCol = 49 directionChange = 2 else: nextRow = nextCol - 50 nextCol = 99 directionChange = 2 elif self.direction == 2: if nextRow < 50: nextCol = 0 nextRow = 149 - nextRow directionChange = 0 elif nextRow < 100: nextCol = nextRow - 50 nextRow = 100 directionChange = 1 elif nextRow < 150: nextRow = 49 - (nextRow - 100) nextCol = 50 directionChange = 0 else: nextCol = nextRow - 100 nextRow = 0 directionChange = 1 elif self.direction == 3: if nextCol < 50: nextRow = nextCol + 50 nextCol = 50 directionChange = 0 elif nextCol < 100: nextRow = nextCol + 100 nextCol = 0 directionChange = 0 else: nextCol = nextCol - 100 nextRow = 199 directionChange = 3 return nextRow, nextCol, directionChange def getPassword(self): return (self.column + 1)*4 + (self.row + 1)*1000 + self.direction #create an object of the map path = plan() # reading input and inserting it into the map with open('input22.txt','r') as f: inp = f.read().splitlines(keepends=False) for line in inp: if line == '': break path.addLine(line) instructionSet = inp[-1] instructionSet = re.split('(\d+)', instructionSet) path.setStart() #go through the instructions for instruction in instructionSet: path.getInstruction(instruction) # print solution print(path.column + 1, path.row + 1, path.direction, sep = '---') print(path.getPassword())