def checkRolls(x: int, y: int) : rolls = 0 #!Check row above if y > 0 : #Check above left if x > 0 and inputGrid[y - 1][x - 1] == "@" : rolls += 1 #Check directly above if inputGrid[y - 1][x] == "@" : rolls += 1 #Check above right if x < len(inputGrid[y - 1]) - 1 and inputGrid[y - 1][x + 1] == "@" : rolls += 1 #!Check same row #Check left if x > 0 and inputGrid[y][x - 1] == "@" : rolls += 1 #Check right if x < len(inputGrid[y]) - 1 and inputGrid[y][x + 1] == "@": rolls += 1 #!Check row below if y < len(inputGrid) - 1 : #Check below left if x > 0 and inputGrid[y + 1][x - 1] == "@" : rolls += 1 #Check directly below if inputGrid[y + 1][x] == "@" : rolls += 1 #Check below right if x < len(inputGrid[y]) - 1 and inputGrid[y + 1][x + 1] == "@" : rolls += 1 return rolls testInput = [ "..@@.@@@@.", "@@@.@.@.@@", "@@@@@.@.@@", "@.@@@@..@.", "@@.@@@@.@@", ".@@@@@@@.@", ".@.@.@.@@@", "@.@@@.@@@@", ".@@@@@@@@.", "@.@.@@@.@." ] def readFile() : ary = [] with open("files/Problem4.txt", "r") as file : for line in file : ary.append(line.replace("\n", "")) return ary def printGrid() : for line in inputGrid : print(line) print("") def clearGrid() : for y in range(len(inputGrid)) : newLine = inputGrid[y] newLine = newLine.replace("x", ".") inputGrid[y] = newLine #inputGrid = testInput inputGrid = readFile() removableRoles = 0 x = 0 y = 0 rollLocations = [] #If accessible roles are found, remove them from the grid and begin again until no more are found while True : #printGrid() #clearGrid() for y in range(len(inputGrid)) : for x in range(len(inputGrid[y])) : if inputGrid[y][x] == "@" : rollsAround = checkRolls(x, y) #print(f"roll at ({x}, {y}) with {rollsAround} rolls around") if rollsAround < 4 : removableRoles += 1 #Save X,Y of accessible rolls rollLocations.append((x, y)) #print(f"Accessible roll at ({x}, {y}) with {rollsAround} rolls around") if len(rollLocations) == 0 : break for loc in rollLocations : inputGrid[loc[1]] = inputGrid[loc[1]][:loc[0]] + "x" + inputGrid[loc[1]][loc[0] + 1:] rollLocations = [] #printGrid() print(f"Total removable roles = {removableRoles}") #Too low = 8316