diff --git a/day11/day11.cpp b/day11/day11.cpp new file mode 100644 index 0000000..d3c8483 --- /dev/null +++ b/day11/day11.cpp @@ -0,0 +1,101 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +constexpr char EMPTY_CHAR = 'L'; +constexpr char FLOOR_CHAR = '.'; +constexpr char OCCUPIED_CHAR = '#'; + +std::vector readInput(const std::string &filename) { + std::vector input; + std::string line; + std::ifstream file(filename); + while (std::getline(file, line)) { + input.push_back(line); + } + + return input; +} + +std::vector getNeighbors(const std::vector &state, int row, int column) { + std::vector neighbors; + for (int dRow = -1; dRow <= 1; dRow++) { + for (int dCol = -1; dCol <= 1; dCol++) { + if (dRow == 0 && dCol == 0) { + continue; + } + + int candidateRow = row + dRow; + int candidateCol = column + dCol; + if (candidateRow < 0 || candidateRow >= state.size() || candidateRow < 0 || + candidateCol >= state.at(candidateRow).size()) { + continue; + } + + char neighbor = state.at(candidateRow).at(candidateCol); + neighbors.push_back(neighbor); + } + } + + return neighbors; +} + +char applyRules(const std::vector &state, int row, int column) { + std::vector neighbors = getNeighbors(state, row, column); + char item = state.at(row).at(column); + int numOccupied = + std::count_if(neighbors.begin(), neighbors.end(), [](char neighbor) { return neighbor == OCCUPIED_CHAR; }); + if (item == EMPTY_CHAR && numOccupied == 0) { + return OCCUPIED_CHAR; + } else if (item == OCCUPIED_CHAR && numOccupied >= 4) { + return EMPTY_CHAR; + } else { + return item; + } +} + +void printState(const std::vector &state) { + for (const std::string &row : state) { + std::cout << row << std::endl; + } + + std::cout << " " << std::endl; +} + +int part1(const std::vector &input) { + std::vector state(input); + std::vector nextState(input.size(), std::string(input.at(0).size(), ' ')); + while (state != nextState) { + for (int i = 0; i < state.size(); i++) { + for (int j = 0; j < input.at(i).size(); j++) { + char res = applyRules(state, i, j); + nextState.at(i).at(j) = res; + } + } + + std::swap(state, nextState); + } + + return std::accumulate(state.cbegin(), state.cend(), 0, [](int total, const std::string &row) { + return total + std::count_if(row.cbegin(), row.cend(), [](char item) { return item == OCCUPIED_CHAR; }); + }); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + std::cerr << argv[0] << " " << std::endl; + return 1; + } + + auto input = readInput(argv[1]); + + std::cout << part1(input) << std::endl; +}