diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-07 22:39:15 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-07 22:39:15 +0100 |
commit | 42e3a9ce16fc988133f6ca85f8e35a94a2dc305e (patch) | |
tree | 34c13508766f9143bf38778019ff3486788b8225 /2021 | |
parent | 0cadb2319cf7b937918df7071a810d28d769a465 (diff) |
day11
Diffstat (limited to '2021')
-rwxr-xr-x | 2021/day11/day11 | bin | 0 -> 68808 bytes | |||
-rw-r--r-- | 2021/day11/day11.cpp | 115 | ||||
-rw-r--r-- | 2021/day11/day11_input.txt | 10 | ||||
-rw-r--r-- | 2021/day11/day11_input_ez.txt | 10 | ||||
-rw-r--r-- | 2021/day11/makefile | 2 |
5 files changed, 137 insertions, 0 deletions
diff --git a/2021/day11/day11 b/2021/day11/day11 Binary files differnew file mode 100755 index 0000000..a39baa6 --- /dev/null +++ b/2021/day11/day11 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 <iostream> +#include <fstream> +#include <vector> +#include <string> + +class State { + public: State() { iter = 0; booms = 0; } + int size; + std::vector<int> 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<light.size(); i++) { + if (light[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<size; y++) { + for (int x=0; x<size; x++) { + int i = xytoi(x, y); + if (light[i] > 9) { + propagate(x, y); + booms ++; + light[i] = -10000000; + solve(); + return; + } + } + } + if (!calm()) solve(); + } + + void step () { + for (int y=0; y<size; y++) { + for (int x=0; x<size; x++) { + int i = xytoi(x, y); + light[i] ++; + } + } + solve(); + for (std::size_t i=0; i<light.size(); i++) { + if (light[i] < 0) light[i] = 0; + } + iter ++; + } + + void show () { + std::cout << "iter: " << iter << std::endl; + for (int y=0; y<size; y++) { + for (int x=0; x<size; x++) { + std::cout << light[xytoi(x, y)]; + } + std::cout << std::endl; + } + std::cout << std::endl; + } +}; + +int main (int argc, char * argv[]) { + std::string line; + std::vector<std::string> 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<iters; i++) { + state.step(); + state.show(); + + if (state.booms - last == 100) { + break; + } + last = state.booms; + } + + std::cout << "flashes: " << state.booms << std::endl; + + return 0; +} diff --git a/2021/day11/day11_input.txt b/2021/day11/day11_input.txt new file mode 100644 index 0000000..d33c53b --- /dev/null +++ b/2021/day11/day11_input.txt @@ -0,0 +1,10 @@ +6227618536 +2368158384 +5385414113 +4556757523 +6746486724 +4881323884 +4648263744 +4871332872 +4724128228 +4316512167 diff --git a/2021/day11/day11_input_ez.txt b/2021/day11/day11_input_ez.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/2021/day11/day11_input_ez.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/2021/day11/makefile b/2021/day11/makefile new file mode 100644 index 0000000..7614d42 --- /dev/null +++ b/2021/day11/makefile @@ -0,0 +1,2 @@ +all day11.cpp: + g++ -std=c++20 -o day11 day11.cpp |