From b794684673605fd02a6c99bd57406490aeb8f65f Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Sat, 11 Dec 2021 09:08:18 +0100 Subject: [PATCH] added day 11 --- 11/input01.txt | 10 ++++++++ 11/input01_sample.txt | 10 ++++++++ 11/solve01.py | 52 ++++++++++++++++++++++++++++++++++++++++ 11/solve02.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 11/input01.txt create mode 100644 11/input01_sample.txt create mode 100644 11/solve01.py create mode 100644 11/solve02.py diff --git a/11/input01.txt b/11/input01.txt new file mode 100644 index 0000000..c4a08de --- /dev/null +++ b/11/input01.txt @@ -0,0 +1,10 @@ +7777838353 +2217272478 +3355318645 +2242618113 +7182468666 +5441641111 +4773862364 +5717125521 +7542127721 +4576678341 \ No newline at end of file diff --git a/11/input01_sample.txt b/11/input01_sample.txt new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/11/input01_sample.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/11/solve01.py b/11/solve01.py new file mode 100644 index 0000000..9941afe --- /dev/null +++ b/11/solve01.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +def print_cavern(cavern): + for l in cavern: + print("".join([str(i) for i in l])) + +def energy_simulate(cavern): + # increase energy level + for x in range(10): + for y in range(10): + cavern[y][x] += 1 + + # check flashes + flashed = [] + while True: + flashed_round = 0 + for x in range(10): + for y in range(10): + if cavern[y][x] < 10: + continue + if (x,y) in flashed: + continue + flashed_round += 1 + flashed.append((x,y)) + + for d in ((0,1), (0,-1),(1,0),(-1,0), (1,1),(1,-1),(-1,1),(-1,-1)): + dx = x + d[0] + dy = y + d[1] + if (dx < 0) or (dy < 0): + continue + if (dx > 9) or (dy > 9): + continue + cavern[dy][dx] += 1 + if flashed_round == 0: + break + + # flashed set to 0 + for d in flashed: + cavern[d[1]][d[0]] = 0 + + return len(flashed) + +cavern = [] +with open("input01.txt","r") as f: + for line in f: + cavern.append([int(x) for x in line.strip()]) + +flashes = 0 +for i in range(100): + flashes += energy_simulate(cavern) + +print(flashes) diff --git a/11/solve02.py b/11/solve02.py new file mode 100644 index 0000000..fe52a1e --- /dev/null +++ b/11/solve02.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +def print_cavern(cavern): + for l in cavern: + print("".join([str(i) for i in l])) + +def energy_simulate(cavern): + # increase energy level + for x in range(10): + for y in range(10): + cavern[y][x] += 1 + + # check flashes + flashed = [] + while True: + flashed_round = 0 + for x in range(10): + for y in range(10): + if cavern[y][x] < 10: + continue + if (x,y) in flashed: + continue + flashed_round += 1 + flashed.append((x,y)) + + for d in ((0,1), (0,-1),(1,0),(-1,0), (1,1),(1,-1),(-1,1),(-1,-1)): + dx = x + d[0] + dy = y + d[1] + if (dx < 0) or (dy < 0): + continue + if (dx > 9) or (dy > 9): + continue + cavern[dy][dx] += 1 + if flashed_round == 0: + break + + # flashed set to 0 + for d in flashed: + cavern[d[1]][d[0]] = 0 + + return len(flashed) + +cavern = [] +with open("input01.txt","r") as f: + for line in f: + cavern.append([int(x) for x in line.strip()]) + +round = 0 +while True: + round += 1 + flashes = energy_simulate(cavern) + if flashes == 100: + break + +print(round) \ No newline at end of file