I've been doing Advent of Code all month, but this is the first time I've solved both parts within an hour of puzzle release, so here's my code for day 11.
#Day 11
class Octopus:
def __init__(self, value):
self.value = value
self.flashed = False
def setFlash(self):
self.flashed = True
def reset(self):
self.flashed = False
self.value = 0
def getChars(word):
return [char for char in word]
def printMaps(map):
#print map to check basins
for i in range(len(map)): #i = rowIndex
for j in range (len(map[i])): #j = columnIndex
thisOct = map[i][j]
if thisOct.flashed:
print("|" + str(thisOct.value) + "|", end='')
else:
print(" " + str(thisOct.value) + " ", end='')
print("")
def resetMap(map):
#loop over every point in the map to reset flashed
for i in range(len(map)): #i = rowIndex
for j in range (len(map[i])): #j = columnIndex
currentOct = map[i][j]
if(currentOct.flashed == True):
currentOct.reset()
return map
def checkAllFlash(map):
#loop over every point in the map to check for all 9
for i in range(len(map)): #i = rowIndex
for j in range (len(map[i])): #j = columnIndex
currentOct = map[i][j]
if currentOct.value != 0:
return False
#if didn't reach a false, all are 0s
return True
def incrementAll(map):
#loop over every point in the map to increase
for i in range(len(map)): #i = rowIndex
for j in range (len(map[i])): #j = columnIndex
currentOct = map[i][j]
currentOct.value += 1
def flashNines(map, flashCount):
flash = False
newmap = map
#loop over every point in the map, set nines to flashed, call incrementNeightbors
for i in range(len(map)): #i = rowIndex
for j in range (len(map[i])): #j = columnIndex
currentOct = map[i][j]
if currentOct.value > 9:
if currentOct.flashed == False: #not yet flashed this check
flash = True
flashCount += 1
currentOct.setFlash()
newmap = incrementNeightbors(newmap, i, j)
#printMaps(newmap)
#print("------------")
else:
pass
return newmap, flash, flashCount
def incrementNeightbors(map, i, j):
if i > 0:
West = map[i-1][j]
West.value += 1
if j > 0:
North = map[i][j-1]
North.value += 1
if i > 0 and j > 0:
NorthWest = map[i-1][j-1]
NorthWest.value += 1
if i < len(map)-1:
East = map[i+1][j]
East.value += 1
if j < len(map[i])-1:
South = map[i][j+1]
South.value += 1
if i < len(map)-1 and j < len(map[i])-1:
SouthEast = map[i+1][j+1]
SouthEast.value += 1
if i > 0 and j < len(map[i])-1:
NorthEast = map[i-1][j+1]
NorthEast.value += 1
if i < len(map)-1 and j > 0:
SouthWest = map[i+1][j-1]
SouthWest.value += 1
return map
def main():
f = open('input.txt')
lines = f.readlines()
#create map
map = []
for line in lines:
row = []
valueList = getChars(line.strip())
for energyValue in range(len(valueList)):
newOct = Octopus(int(valueList[energyValue]))
row.append(newOct)
map.append(row)
printMaps(map)
print("-----------------")
flashCount = 0
steps = 0
#steps = 100 #part 1
#for i in range(steps): #part 1
allEmpty = False #part 2
while(not allEmpty): #part 2
incrementAll(map)
map, flash, flashCount = flashNines(map, flashCount)
while(flash):
map, flash, flashCount = flashNines(map, flashCount)
map = resetMap(map)
steps += 1 #part 2
#printMaps(map)
allEmpty = checkAllFlash(map) #part 2
#print(flashCount) #part 1 answer
print(steps) #part 2 answer
main()
No comments:
Post a Comment