From 5ea75fa9d49183b29ae70c30b1c8372e58f4bcf3 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Wed, 16 Feb 2022 19:44:14 +0100 Subject: day20 --- 2021/day20/day20 | Bin 0 -> 104424 bytes 2021/day20/day20.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++ 2021/day20/day20_input.txt | 102 +++++++++++++++++++++++++ 2021/day20/makefile | 2 + 2021/day20/test0.txt | 7 ++ 5 files changed, 294 insertions(+) create mode 100755 2021/day20/day20 create mode 100644 2021/day20/day20.cpp create mode 100644 2021/day20/day20_input.txt create mode 100644 2021/day20/makefile create mode 100644 2021/day20/test0.txt diff --git a/2021/day20/day20 b/2021/day20/day20 new file mode 100755 index 0000000..347a7ec Binary files /dev/null and b/2021/day20/day20 differ diff --git a/2021/day20/day20.cpp b/2021/day20/day20.cpp new file mode 100644 index 0000000..6393d4c --- /dev/null +++ b/2021/day20/day20.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include + +using vec = std::pair; +using lattice = std::map; + + +std::vector get_bounds (lattice img) { + std::vector bounds { 999999, -999999, 999999, -999999 }; + for (auto k : img) { + bounds[0] = std::min(bounds[0], k.first.first); + bounds[1] = std::max(bounds[1], k.first.first); + bounds[2] = std::min(bounds[2], k.first.second); + bounds[3] = std::max(bounds[3], k.first.second); + } + return bounds; +} + +void show_lattice (lattice img) { + auto bounds = get_bounds(img); + std::cout << "lattice [" << + bounds[0] <<", " << bounds[1] << ", " << + bounds[2] <<", " << bounds[3] << "]" << std::endl; + for (int y=bounds[2]; y<=bounds[3]; y++) { + for (int x=bounds[0]; x<=bounds[1]; x++) { + if (img[vec(x,y)] == 1) + std::cout << "#"; + else std::cout << "."; + } + std::cout << std::endl; + } +} + +lattice parse_lattice (std::string raw) { + lattice img; + int y = 0; + while (raw.size() > 0) { + auto newline = raw.find("\n"); + std::string line = raw; + if (newline != std::string::npos) { + line = raw.substr(0, newline); + raw = raw.substr(newline+1); + } + for (int x=0; x=sx || y<0 || y>=sy) return background; + return cells[at(x, y)]; + } + + std::vector cells; + int sx, sy; + int offx, offy; + int background; + + Matrix trim (int shrink) { + Matrix trimmed; + trimmed.offx = offx + shrink; + trimmed.offy = offy + shrink; + trimmed.sx = sx - shrink*2; + trimmed.sy = sy - shrink*2; + trimmed.background = background; + for (int y=0; y bounds, lattice img) { + int sum = 0; + for (int y = bounds[2]; y <= bounds[3]; y++) { + for (int x = bounds[0]; x <= bounds[1]; x++) { + sum += img[vec(x,y)]; + } + } + return sum; +} + +int main (int argc, char *argv[]) { + std::string raw; + std::getline(std::ifstream(argv[1]), raw, '\0'); + auto separator = raw.find("\n\n"); + std::string rules = raw.substr(0, separator); + std::string strimg = raw.substr(separator+2); + lattice img = parse_lattice(strimg); + show_lattice(img); + auto bounds = get_bounds(img); + int maxiter = std::stoi(std::string { argv[2] } ); + + Matrix m { img }; + for (int i=0; i