From 4454840b6a5043de026507b46ff087a3ae5b1417 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Mon, 13 Dec 2021 20:33:43 +0000 Subject: [PATCH] Do 11 --- 11_1.py | 54 +++++++++++++++++++++++++++++++++++++++++++ 11_2.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11_3.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 11_1.py create mode 100644 11_2.py create mode 100644 11_3.py diff --git a/11_1.py b/11_1.py new file mode 100644 index 0000000..d545d49 --- /dev/null +++ b/11_1.py @@ -0,0 +1,54 @@ +""" Simulate octopus flashing """ + +def increment(octopodes, position): + for x in range(position[0] - 1, position[0] + 2): + if x < 0 or x > 9: + continue + + for y in range(position[1] - 1, position[1] + 2): + if y < 0 or y > 9: + continue + + octopodes[y][x] += 1 + + +def add_one(octopodes): + for y in range(10): + for x in range(10): + octopodes[y][x] += 1 + + +def flash(octopodes): + flashed = [] + + flashed_this_iter = True + while flashed_this_iter: + flashed_this_iter = False + for y in range(10): + for x in range(10): + if octopodes[y][x] > 9 and (x, y) not in flashed: + flashed_this_iter = True + flashed.append((x, y)) + increment(octopodes, (x, y)) + + for x, y in flashed: + octopodes[y][x] = 0 + + return len(flashed) + + +octopodes = [] +with open("11_input.txt", "r") as f: + for line in f: + octopodes_in_line = [] + for char in line.strip(): + octopodes_in_line.append(int(char)) + + octopodes.append(octopodes_in_line) + +total_flashes = 0 +for gen in range(100): + add_one(octopodes) + total_flashes += flash(octopodes) + +print(total_flashes) diff --git a/11_2.py b/11_2.py new file mode 100644 index 0000000..a0256b2 --- /dev/null +++ b/11_2.py @@ -0,0 +1,72 @@ +""" Simulate octopus flashing """ +import time +import random + +def increment(octopodes, position): + for x in range(position[0] - 1, position[0] + 2): + if x < 0 or x > 39: + continue + + for y in range(position[1] - 1, position[1] + 2): + if y < 0 or y > 19: + continue + + octopodes[y][x] += 1 + + +def add_one(octopodes): + for y in range(20): + for x in range(40): + octopodes[y][x] += 1 + + +def flash(octopodes): + flashed = [] + + flashed_this_iter = True + while flashed_this_iter: + flashed_this_iter = False + for y in range(20): + for x in range(40): + if octopodes[y][x] > 9 and (x, y) not in flashed: + flashed_this_iter = True + flashed.append((x, y)) + increment(octopodes, (x, y)) + + for x, y in flashed: + octopodes[y][x] = 0 + + for y in range(20): + display_row = [] + for x in range(40): + if (x, y) in flashed: + display_row.append("X") + else: + display_row.append(".") + print("".join(display_row)) + + + return len(flashed) + + +octopodes = [] +with open("11_input.txt", "r") as f: + for line in f: + octopodes_in_line = [] + for char in line.strip(): + octopodes_in_line.append(int(char)) + + octopodes.append(octopodes_in_line) + + +for gen in range(1, 100000000): + add_one(octopodes) + print("###", gen, "###") + if flash(octopodes) == 100: + break + + print() + print() + time.sleep(1/12) + +print(gen) diff --git a/11_3.py b/11_3.py new file mode 100644 index 0000000..df730e5 --- /dev/null +++ b/11_3.py @@ -0,0 +1,69 @@ +""" Simulate octopus flashing """ +import time +import random + +rows = 3 +cols = 3 + +def increment(octopodes, position): + for x in range(position[0] - 1, position[0] + 2): + if x < 0 or x > cols - 1: + continue + + for y in range(position[1] - 1, position[1] + 2): + if y < 0 or y > rows - 1: + + continue + + octopodes[y][x] += 1 + + +def add_one(octopodes): + for y in range(rows): + for x in range(cols): + octopodes[y][x] += 1 + + +def flash(octopodes): + flashed = [] + + flashed_this_iter = True + while flashed_this_iter: + flashed_this_iter = False + for y in range(rows): + for x in range(cols): + if octopodes[y][x] > 9 and (x, y) not in flashed: + flashed_this_iter = True + flashed.append((x, y)) + increment(octopodes, (x, y)) + + for x, y in flashed: + octopodes[y][x] = 0 + + #for y in range(20): + # display_row = [] + # for x in range(40): + # if (x, y) in flashed: + # display_row.append("X") + # else: + # display_row.append(".") + # print("".join(display_row)) + + + return len(flashed) + + +octopodes = [] +for i in range(rows): + octopodes.append([random.randint(0, 9) for _ in range(cols)]) + +for gen in range(1, 100000000): + add_one(octopodes) + n_flashed = flash(octopodes) + if gen % 100 == 0: + print("###", gen, "###", n_flashed) + + if n_flashed == (rows * cols): + break + +print(gen) -- 2.45.2