From 42e3a9ce16fc988133f6ca85f8e35a94a2dc305e Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Fri, 7 Jan 2022 22:39:15 +0100 Subject: day11 --- 2021/day11/day11 | Bin 0 -> 68808 bytes 2021/day11/day11.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++ 2021/day11/day11_input.txt | 10 ++++ 2021/day11/day11_input_ez.txt | 10 ++++ 2021/day11/makefile | 2 + 5 files changed, 137 insertions(+) create mode 100755 2021/day11/day11 create mode 100644 2021/day11/day11.cpp create mode 100644 2021/day11/day11_input.txt create mode 100644 2021/day11/day11_input_ez.txt create mode 100644 2021/day11/makefile (limited to '2021/day11') diff --git a/2021/day11/day11 b/2021/day11/day11 new file mode 100755 index 0000000..a39baa6 Binary files /dev/null and b/2021/day11/day11 differ diff --git a/2021/day11/day11.cpp b/2021/day11/day11.cpp new file mode 100644 index 0000000..5a88b14 --- /dev/null +++ b/2021/day11/day11.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +class State { + public: State() { iter = 0; booms = 0; } + int size; + std::vector light; + int xytoi (int x, int y) { return x+y*size; } + int itox (int i) { return i % size; } + int itoy (int i) { return i / size; } + int iter; + int booms; + + bool oob (int x, int y) { + if (x < 0 || x >= size) return true; + if (y < 0 || y >= size) return true; + return false; + } + + bool calm () { + for (std::size_t i=0; i 9) return false; + } + return true; + } + + void propagate (int x, int y) { + for (int i=-1; i<2; i++) { + for (int j=-1; j<2; j++) { + if (!oob(x+i, y+j)) { + light[xytoi(x+i, y+j)]++; + } + } + } + } + + void solve () { + for (int y=0; y 9) { + propagate(x, y); + booms ++; + light[i] = -10000000; + solve(); + return; + } + } + } + if (!calm()) solve(); + } + + void step () { + for (int y=0; y lines; + std::ifstream f { argv[1] }; + do { + std::getline(f, line); + if (line.size() > 1) lines.push_back(line); + } while (!f.eof()); + + State state; + for (auto l : lines) { + state.size = 0; + for (char c : l) { + state.light.push_back(std::stoi(std::string { c })); + state.size ++; + } + } + + state.show(); + int last = 0; + int iters = argc > 2 ? atoi(argv[2]) : 100; + for (int i=0; i