Skip to content

Instantly share code, notes, and snippets.

@9helix
Created December 12, 2021 08:48
Show Gist options
  • Save 9helix/45bf82d749b2c185101f66075a493730 to your computer and use it in GitHub Desktop.
Save 9helix/45bf82d749b2c185101f66075a493730 to your computer and use it in GitHub Desktop.
input = '''6744638455
3135745418
4754123271
4224257161
8167186546
2268577674
7177768175
2662255275
4655343376
7852526168'''.splitlines()
input = '''5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526'''.splitlines()
flashes = 0
for i in range(len(input)):
input[i] = list(input[i])
for j in range(len(input[i])):
input[i][j] = int(input[i][j])
def search(i, j, flashes):
try:
input[i][j-1] += 1
if input[i][j-1] > 9:
input[i][j] = 0
flashes += 1
search(i, j-1, flashes)
except Exception:
pass
try:
input[i][j+1] += 1
if input[i][j+1] > 9:
input[i][j] = 0
flashes += 1
search(i, j+1, flashes)
except Exception:
pass
try:
input[i+1][j-1] += 1
if input[i+1][j-1] > 9:
input[i][j] = 0
flashes += 1
search(i+1, j-1, flashes)
except Exception:
pass
try:
input[i-1][j-1] += 1
if input[i-1][j-1] > 9:
input[i][j] = 0
flashes += 1
search(i-1, j-1, flashes)
except Exception:
pass
try:
input[i+1][j+1] += 1
if input[i+1][j+1] > 9:
input[i][j] = 0
flashes += 1
search(i+1, j+1, flashes)
except Exception:
pass
try:
input[i+1][j] += 1
if input[i+1][j] > 9:
input[i][j] = 0
flashes += 1
search(i+1, j, flashes)
except Exception:
pass
try:
input[i-1][j] += 1
if input[i-1][j] > 9:
input[i][j] = 0
flashes += 1
search(i-1, j, flashes)
except Exception:
pass
try:
input[i-1][j+1] += 1
if input[i-1][j+1] > 9:
input[i][j] = 0
flashes += 1
search(i-1, j+1, flashes)
except Exception:
pass
def gridprint(grid):
for i in range(len(grid)):
for j in range(len(grid[i])):
if j == len(grid[i])-1:
print(grid[i][j])
else:
print(grid[i][j], end=' ')
steps = 2
for k in range(steps):
for i in range(len(input)):
for j in range(len(input[i])):
if input[i][j] >= 9:
input[i][j] = 0
flashes += 1
search(i, j, flashes)
else:
input[i][j] += 1
gridprint(input)
print(flashes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment