#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