// C++ code to compute sequence A097995 in the OEIS, by Eric M. Schmidt. // I hereby waive all rights to this code, in accordance with the CC0 // waiver: // http://creativecommons.org/publicdomain/zero/1.0/ #include #include #include typedef std::pair Pnt; typedef std::vector CellList; Pnt operator+(Pnt pnt1, Pnt pnt2) { return {pnt1.first + pnt2.first, pnt1.second + pnt2.second}; } std::array const neighOffsets {{ {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} }}; CellList appliedSeeds(CellList const & cells) { CellList newcells; newcells.reserve(cells.size()); std::map> neighInfo; for (Pnt const & pnt : cells) { neighInfo[pnt].second = true; for (Pnt const & offset : neighOffsets) { ++neighInfo[pnt + offset].first; } } for (auto const & cell : neighInfo) { Pnt const & pnt(cell.first); int const & numNeighbors(cell.second.first); bool const & wasAlive(cell.second.second); if (!wasAlive && numNeighbors == 2) { newcells.push_back(pnt); } } return newcells; } // Main to compute b-file to n=1000 #include int main() { CellList pattern{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}}; for (int ii(0); ii <= 1000; ++ii) { std::cout << ii << ' ' << pattern.size() << std::endl; pattern = appliedSeeds(pattern); } }