From 7717713bde40ccc675cc3ec4e9e5442d088f1085 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Mon, 13 Dec 2021 13:59:07 -0500 Subject: [PATCH] Finished day 11. --- 2021/11/data | 10 +++++++++ 2021/11/example_data | 10 +++++++++ 2021/11/main.py | 42 +++++++++++++++++++++++++++++++++++++ 2021/11/main2.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 2021/11/data create mode 100644 2021/11/example_data create mode 100644 2021/11/main.py create mode 100644 2021/11/main2.py diff --git a/2021/11/data b/2021/11/data new file mode 100644 index 0000000..714a2d0 --- /dev/null +++ b/2021/11/data @@ -0,0 +1,10 @@ +1254117228 +4416873224 +8354381553 +1372637614 +5586538553 +7213333427 +3571362825 +1681126243 +8718312138 +5254266347 \ No newline at end of file diff --git a/2021/11/example_data b/2021/11/example_data new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/2021/11/example_data @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/2021/11/main.py b/2021/11/main.py new file mode 100644 index 0000000..6fa8755 --- /dev/null +++ b/2021/11/main.py @@ -0,0 +1,42 @@ +grid = [] +to_flash = [] + +for line in open('data', 'r'): + grid_line = [] + for ch in line.strip(): + grid_line.append(int(ch)) + grid.append(grid_line) + +flashed = 0 +for step in range(0, 100): + # Step 1: Increment + for y in range(0, len(grid)): + for x in range(0, len(grid[y])): + grid[y][x] += 1 + if(grid[y][x] > 9): + to_flash.append(tuple([y, x])) + + if(len(to_flash) == len(grid) * len(grid[0])): + print('all flash step #', step + 1) + # Step 2: Flash + while(len(to_flash)): + cell = to_flash[-1] # take the last one + to_flash.pop() # remove + flashed += 1 # count + for y in range(-1, 2): + new_y = cell[0] + y + if(new_y >= 0 and new_y < len(grid)): # in bounds + for x in range(-1, 2): + new_x = cell[1] + x + if(new_x >= 0 and new_x < len(grid[y])): # in bounds + grid[new_y][new_x] += 1 + if(grid[new_y][new_x] == 10): # JUST flashed + to_flash.append(tuple([new_y, new_x])) + + # Step 3: Reset (if flashed) + for y in range(0, len(grid)): + for x in range(0, len(grid[y])): + if(grid[y][x] > 9): + grid[y][x] = 0 + + print('Step #', (step + 1), ': ', flashed) diff --git a/2021/11/main2.py b/2021/11/main2.py new file mode 100644 index 0000000..402543c --- /dev/null +++ b/2021/11/main2.py @@ -0,0 +1,49 @@ +grid = [] +to_flash = [] + +for line in open('data', 'r'): + grid_line = [] + for ch in line.strip(): + grid_line.append(int(ch)) + grid.append(grid_line) + +flashed = 0 +step = 0 +#for step in range(0, 100): +while(True): + # Step 1: Increment + for y in range(0, len(grid)): + for x in range(0, len(grid[y])): + grid[y][x] += 1 + if(grid[y][x] > 9): + to_flash.append(tuple([y, x])) + + # Step 2: Flash + prior_flash = flashed + while(len(to_flash)): + cell = to_flash[-1] # take the last one + to_flash.pop() # remove + flashed += 1 # count + for y in range(-1, 2): + new_y = cell[0] + y + if(new_y >= 0 and new_y < len(grid)): # in bounds + for x in range(-1, 2): + new_x = cell[1] + x + if(new_x >= 0 and new_x < len(grid[y])): # in bounds + grid[new_y][new_x] += 1 + if(grid[new_y][new_x] == 10): # JUST flashed + to_flash.append(tuple([new_y, new_x])) + + # PART TWO ANSWER + if(flashed - prior_flash == len(grid) * len(grid[0])): + print('all flash step #', step + 1) + break + + # Step 3: Reset (if flashed) + for y in range(0, len(grid)): + for x in range(0, len(grid[y])): + if(grid[y][x] > 9): + grid[y][x] = 0 + + print('Step #', (step + 1), ': ', flashed) + step += 1