This commit is contained in:
parent
8b03f53d7b
commit
2c309668f3
3 changed files with 115 additions and 0 deletions
95
11/11.py
Executable file
95
11/11.py
Executable file
|
@ -0,0 +1,95 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# 2021 - Advent Of Code - 11
|
||||||
|
|
||||||
|
GRID_SIZE = 10
|
||||||
|
|
||||||
|
|
||||||
|
def parse_file(file):
|
||||||
|
with open(file) as f:
|
||||||
|
columns = []
|
||||||
|
for line in f.readlines():
|
||||||
|
columns.append([int(c) for c in line.strip()])
|
||||||
|
|
||||||
|
return columns
|
||||||
|
|
||||||
|
|
||||||
|
def print_grid(grid):
|
||||||
|
for y in range(GRID_SIZE):
|
||||||
|
for x in range(GRID_SIZE):
|
||||||
|
print(grid[y][x], end='')
|
||||||
|
print()
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
def check_flash(grid, f):
|
||||||
|
for y in range(GRID_SIZE):
|
||||||
|
for x in range(GRID_SIZE):
|
||||||
|
if grid[y][x] > 9:
|
||||||
|
f = flash(grid, x, y, f)
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
def flash(grid, x, y, f):
|
||||||
|
grid[y][x] = 0
|
||||||
|
f += 1
|
||||||
|
if y > 0: # upper neighbours
|
||||||
|
if x > 0:
|
||||||
|
if grid[y-1][x-1] != 0: # 1
|
||||||
|
grid[y-1][x-1] += 1
|
||||||
|
if grid[y-1][x-1] > 9:
|
||||||
|
f = flash(grid, x-1, y-1, f)
|
||||||
|
if grid[y-1][x] != 0: # 2
|
||||||
|
grid[y-1][x] += 1
|
||||||
|
if grid[y-1][x] > 9:
|
||||||
|
f = flash(grid, x, y-1, f)
|
||||||
|
|
||||||
|
if x < GRID_SIZE-1:
|
||||||
|
if grid[y-1][x+1] != 0: # 3
|
||||||
|
grid[y-1][x+1] += 1
|
||||||
|
if grid[y-1][x+1] > 9:
|
||||||
|
f = flash(grid, x+1, y-1, f)
|
||||||
|
|
||||||
|
if x > 0: # middle neighbours
|
||||||
|
if grid[y][x-1] != 0: # 4
|
||||||
|
grid[y][x-1] += 1
|
||||||
|
if grid[y][x-1] > 9:
|
||||||
|
f = flash(grid, x-1, y, f)
|
||||||
|
if x < GRID_SIZE - 1: # 6
|
||||||
|
if grid[y][x+1] != 0:
|
||||||
|
grid[y][x+1] += 1
|
||||||
|
if grid[y][x+1] > 9:
|
||||||
|
f = flash(grid, x+1, y, f)
|
||||||
|
|
||||||
|
if y < GRID_SIZE-1: # down neighbours
|
||||||
|
if x > 0:
|
||||||
|
if grid[y+1][x-1] != 0: # 7
|
||||||
|
grid[y+1][x-1] += 1
|
||||||
|
if grid[y+1][x-1] > 9:
|
||||||
|
f = flash(grid, x-1, y+1, f)
|
||||||
|
if grid[y+1][x] != 0: # 8
|
||||||
|
grid[y+1][x] += 1
|
||||||
|
if grid[y + 1][x] > 9:
|
||||||
|
f = flash(grid, x, y+1, f)
|
||||||
|
if x < GRID_SIZE-1:
|
||||||
|
if grid[y+1][x+1] != 0: # 9
|
||||||
|
grid[y+1][x+1] += 1
|
||||||
|
if grid[y+1][x+1] > 9:
|
||||||
|
f = flash(grid, x+1, y+1, f)
|
||||||
|
return f
|
||||||
|
|
||||||
|
|
||||||
|
octopus_map = parse_file('input.txt')
|
||||||
|
# octopus_map = parse_file('input_example.txt')
|
||||||
|
print_grid(octopus_map)
|
||||||
|
|
||||||
|
nbflash = 0
|
||||||
|
for step in range(100):
|
||||||
|
# increase all energy levels
|
||||||
|
for mapy in range(GRID_SIZE):
|
||||||
|
for mapx in range(GRID_SIZE):
|
||||||
|
octopus_map[mapy][mapx] += 1
|
||||||
|
|
||||||
|
nbflash = check_flash(octopus_map, nbflash)
|
||||||
|
print_grid(octopus_map)
|
||||||
|
|
||||||
|
print(f'number of flash: {nbflash}')
|
10
11/input.txt
Normal file
10
11/input.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
8258741254
|
||||||
|
3335286211
|
||||||
|
8468661311
|
||||||
|
6164578353
|
||||||
|
2138414553
|
||||||
|
1785385447
|
||||||
|
3441133751
|
||||||
|
3586862837
|
||||||
|
7568272878
|
||||||
|
6833643144
|
10
11/input_example.txt
Normal file
10
11/input_example.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
5483143223
|
||||||
|
2745854711
|
||||||
|
5264556173
|
||||||
|
6141336146
|
||||||
|
6357385478
|
||||||
|
4167524645
|
||||||
|
2176841721
|
||||||
|
6882881134
|
||||||
|
4846848554
|
||||||
|
5283751526
|
Loading…
Reference in a new issue