diff --git a/src/day-4-solution.cpp b/src/day-4-solution.cpp new file mode 100644 index 0000000..7825f07 --- /dev/null +++ b/src/day-4-solution.cpp @@ -0,0 +1,126 @@ +#include "spdlog/spdlog.h" +#include +#include +#include +#include +#include +#include + +std::vector> parse_matrix(const std::vector& matrix_lines){ + std::vector> returnable = std::vector> {}; + + for (std::string line : matrix_lines) { + std::vector temp_vector = std::vector{}; + + for (std::string::const_iterator i = line.begin(); i> construct_kernel(uint8_t r){ + std::vector> kernel = std::vector> {}; + + for (int r_1 = int {-1 * r}; r_1<=r; r_1++) { + std::vector row = std::vector {}; + for (int r_2 = int {-1*r}; r_2<=r; r_2++) { + + if(r_1 != 0 || r_2!=0) row.push_back(uint8_t {1}); + else row.push_back(uint8_t {0}); + } + kernel.push_back(row); + } + return kernel; +} + +int convolve( + const std::vector>& matrix, + int r, // radius of the kernel + int i, // row index of the target cell + int j, // column index of the target cell + const std::vector>& kernel +) { + int sum = 0; + + int rows = matrix.size(); + int cols = matrix[i].size(); + + for (int dr = -r; dr <= r; dr++) { + for (int dc = -r; dc <= r; dc++) { + int ni = i + dr; + int nj = j + dc; + + + if (ni < 0 || ni >= rows || nj < 0 || nj >= cols) continue; + + + int kr = dr + r; + int kc = dc + r; + + sum += matrix[ni][nj] * kernel[kr][kc]; + } + } + + return sum; +} + +int solve_part_one( const std::vector>& matrix){ + const std::vector> kernel = construct_kernel(1); + int returnable = int {0}; + for (int i = {0}; i>& matrix){ + static const std::vector> kernel = construct_kernel(1); + int returnable = int {0}; + std::vector> indicies = std::vector> {}; + for (int i = {0}; i{i,j}); + returnable+=1; + } + } + } + + + } + if(indicies.size()==0)return returnable; + for (std::pair pair: indicies) { + matrix[pair.first][pair.second] = 0; + } + returnable+=solve_part_two(matrix); + return returnable; +} + +int main () { + std::ifstream input = std::ifstream{"testcases/day-4.in", std::ios_base::binary}; + std::vector lines = std::vector {}; + std::string current_line = std::string{}; + + while (std::getline(input, current_line)) { + lines.push_back(current_line); + } + std::vector> testcase = parse_matrix(lines); + + int part_one_solution = solve_part_one(testcase); + spdlog::info("Part one solution: {}", part_one_solution); + + int part_two_solution = solve_part_two(testcase); + spdlog::info("Part two solution: {}", part_two_solution); + + + return 0; +}