#include #include #include using Schematic = std::vector; bool check(const Schematic& schematic, int x, int y) { return x >= 0 and y >= 0 and x < schematic.size() and y < schematic[x].size() and schematic[x][y] != '.' and not std::isdigit(schematic[x][y]); } bool check_adj(const Schematic& schematic, int x, int y) { return check(schematic, x - 1, y - 1) or check(schematic, x, y - 1) or check(schematic, x + 1, y - 1) or check(schematic, x - 1, y) or check(schematic, x + 1, y); } int find_nums(const Schematic& schematic) { int sum{}; for (int x = 0; x < schematic.size(); ++x) { const std::string& line = schematic[x]; int cur{}; bool adj{ false }; for (int y = 0; y < line.size(); ++y) { if (std::isdigit(line[y])) { cur = 10 * cur + (line[y] - '0'); adj = adj or check_adj(schematic, x, y); } else { if (cur > 0) { adj = adj or check(schematic, x - 1, y) or check(schematic, x, y) or check(schematic, x + 1, y); sum += adj ? cur : 0; } cur = 0; adj = false; } } } return sum; } int main(void) { int answer{}; Schematic schematic{}; std::ifstream input{ "resources/input.txt" }; if (input.is_open()) { std::string line; while (not std::getline(input,line).eof()) { line.push_back('.'); schematic.push_back(std::move(line)); } } input.close(); answer = find_nums(schematic); std::cout << answer << std::endl; return 0; }