From 426e2f500169a7fd13a424820ce2cfdd1b292aa8 Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Wed, 7 Dec 2022 09:06:57 -0500 Subject: [PATCH] Day 11 --- .gitignore | 2 + Day 11/part1.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++++ Day 11/part2.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 Day 11/part1.cpp create mode 100644 Day 11/part2.cpp diff --git a/.gitignore b/.gitignore index 722d5e7..5f2f04a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .vscode +*/* +!**/*.cpp \ No newline at end of file diff --git a/Day 11/part1.cpp b/Day 11/part1.cpp new file mode 100644 index 0000000..b8b3f97 --- /dev/null +++ b/Day 11/part1.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + +using namespace std; + +constexpr int grid_size{10}; +int grid[grid_size][grid_size] = {0}; + +void output_grid(); + +std::ostream& bold_on(std::ostream& os); +std::ostream& bold_off(std::ostream& os); + +int main(int argc, char* argv[]) +{ + string line = ""; + + for (int y = 0; y < grid_size; y++) + { + getline(cin, line); + for (int x = 0; x < grid_size; x++) + { + grid[x][y] = (int)(line[x]-'0'); + } + } + + int total_flashes = 0; + for (int t = 0; t < 100; t++) + { + //Step 1, increment + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + ++grid[x][y]; + } + } + + //Step 2, flash + bool flashed[grid_size][grid_size] = {false}; + bool flash_occurred = false; + do + { + flash_occurred = false; + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + if (!flashed[x][y] && grid[x][y] > 9) + { + flashed[x][y] = true; + flash_occurred = true; + ++total_flashes; + + for(int fy = max(0,y-1); fy < min(grid_size, y+2); fy++) + { + for(int fx = max(0,x-1); fx < min(grid_size,x+2); fx++) + { + grid[fx][fy]++; + } + } + } + } + } + } while (flash_occurred); + + //Step 3, flashed are cleared + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + if (flashed[x][y]) + { + grid[x][y] = 0; + } + } + } + } + + cout << total_flashes << endl; +} + +void output_grid() +{ + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + if (0 == grid[x][y]) + { + cout << bold_on << grid[x][y] << bold_off; + } + else + { + cout << grid[x][y]; + } + } + cout << endl; + } + cout << endl; +} + +std::ostream& bold_on(std::ostream& os) +{ + return os << "\e[1m"; +} + +std::ostream& bold_off(std::ostream& os) +{ + return os << "\e[0m"; +} \ No newline at end of file diff --git a/Day 11/part2.cpp b/Day 11/part2.cpp new file mode 100644 index 0000000..f15e02c --- /dev/null +++ b/Day 11/part2.cpp @@ -0,0 +1,119 @@ +#include +#include +#include + +using namespace std; + +constexpr int grid_size{10}; +int grid[grid_size][grid_size] = {0}; + +void output_grid(); + +std::ostream& bold_on(std::ostream& os); +std::ostream& bold_off(std::ostream& os); + +int main(int argc, char* argv[]) +{ + string line = ""; + + for (int y = 0; y < grid_size; y++) + { + getline(cin, line); + for (int x = 0; x < grid_size; x++) + { + grid[x][y] = (int)(line[x]-'0'); + } + } + + int t = 0; + bool all_flashed = false; + while(!all_flashed) + { + all_flashed = true; + //Step 1, increment + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + ++grid[x][y]; + } + } + + //Step 2, flash + bool flashed[grid_size][grid_size] = {false}; + bool flash_occurred = false; + do + { + flash_occurred = false; + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + if (!flashed[x][y] && grid[x][y] > 9) + { + flashed[x][y] = true; + flash_occurred = true; + + for(int fy = max(0,y-1); fy < min(grid_size, y+2); fy++) + { + for(int fx = max(0,x-1); fx < min(grid_size,x+2); fx++) + { + grid[fx][fy]++; + } + } + } + } + } + } while (flash_occurred); + + //Step 3, flashed are cleared + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + if (flashed[x][y]) + { + grid[x][y] = 0; + } + else + { + all_flashed = false; + } + } + } + + t++; + } + + cout << t << endl; +} + +void output_grid() +{ + for (int y = 0; y < grid_size; y++) + { + for (int x = 0; x < grid_size; x++) + { + if (0 == grid[x][y]) + { + cout << bold_on << grid[x][y] << bold_off; + } + else + { + cout << grid[x][y]; + } + } + cout << endl; + } + cout << endl; +} + +std::ostream& bold_on(std::ostream& os) +{ + return os << "\e[1m"; +} + +std::ostream& bold_off(std::ostream& os) +{ + return os << "\e[0m"; +} \ No newline at end of file