From 2c309668f39112d45ac4898cb2ab25f90784a9c1 Mon Sep 17 00:00:00 2001 From: kleph Date: Sat, 11 Dec 2021 19:15:12 +0100 Subject: [PATCH] Day 11 part 1 --- 11/11.py | 95 ++++++++++++++++++++++++++++++++++++++++++++ 11/input.txt | 10 +++++ 11/input_example.txt | 10 +++++ 3 files changed, 115 insertions(+) create mode 100755 11/11.py create mode 100644 11/input.txt create mode 100644 11/input_example.txt diff --git a/11/11.py b/11/11.py new file mode 100755 index 0000000..ec06d9f --- /dev/null +++ b/11/11.py @@ -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}') diff --git a/11/input.txt b/11/input.txt new file mode 100644 index 0000000..497e9f0 --- /dev/null +++ b/11/input.txt @@ -0,0 +1,10 @@ +8258741254 +3335286211 +8468661311 +6164578353 +2138414553 +1785385447 +3441133751 +3586862837 +7568272878 +6833643144 diff --git a/11/input_example.txt b/11/input_example.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/11/input_example.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526