day 4 done. oat beat me
This commit is contained in:
126
src/day-4-solution.cpp
Normal file
126
src/day-4-solution.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::vector<uint8_t>> parse_matrix(const std::vector<std::string>& matrix_lines){
|
||||
std::vector<std::vector<uint8_t>> returnable = std::vector<std::vector<uint8_t>> {};
|
||||
|
||||
for (std::string line : matrix_lines) {
|
||||
std::vector<uint8_t> temp_vector = std::vector<uint8_t>{};
|
||||
|
||||
for (std::string::const_iterator i = line.begin(); i<line.end(); i++) {
|
||||
if(*i=='@')temp_vector.push_back(uint8_t {1});
|
||||
else temp_vector.push_back(uint8_t {0});
|
||||
}
|
||||
returnable.push_back(temp_vector);
|
||||
}
|
||||
return returnable;
|
||||
}
|
||||
|
||||
std::vector<std::vector<uint8_t>> construct_kernel(uint8_t r){
|
||||
std::vector<std::vector<uint8_t>> kernel = std::vector<std::vector<uint8_t>> {};
|
||||
|
||||
for (int r_1 = int {-1 * r}; r_1<=r; r_1++) {
|
||||
std::vector<uint8_t> row = std::vector<uint8_t> {};
|
||||
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<std::vector<uint8_t>>& 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<std::vector<uint8_t>>& 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<std::vector<uint8_t>>& matrix){
|
||||
const std::vector<std::vector<uint8_t>> kernel = construct_kernel(1);
|
||||
int returnable = int {0};
|
||||
for (int i = {0}; i<matrix.size(); i++) {
|
||||
for (int j = {0}; j<matrix[i].size();j++) {
|
||||
if(matrix[i][j]==1){
|
||||
returnable += convolve(matrix, 1, i, j, kernel)<4 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnable;
|
||||
}
|
||||
|
||||
int solve_part_two( std::vector<std::vector<uint8_t>>& matrix){
|
||||
static const std::vector<std::vector<uint8_t>> kernel = construct_kernel(1);
|
||||
int returnable = int {0};
|
||||
std::vector<std::pair<int,int>> indicies = std::vector<std::pair<int,int>> {};
|
||||
for (int i = {0}; i<matrix.size(); i++) {
|
||||
for (int j = {0}; j<matrix[i].size();j++) {
|
||||
if(matrix[i][j]==1){
|
||||
int convolution = convolve(matrix, 1, i, j, kernel);
|
||||
if(convolution <4){
|
||||
indicies.push_back(std::pair<int, int>{i,j});
|
||||
returnable+=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if(indicies.size()==0)return returnable;
|
||||
for (std::pair<int,int> 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<std::string> lines = std::vector<std::string> {};
|
||||
std::string current_line = std::string{};
|
||||
|
||||
while (std::getline(input, current_line)) {
|
||||
lines.push_back(current_line);
|
||||
}
|
||||
std::vector<std::vector<uint8_t>> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user