This commit is contained in:
Daniel Cortés
2020-12-03 06:26:47 -03:00
parent 85b305265e
commit 95b86cbd78

View File

@@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <utility>
std::vector<std::string> read_input_file() { std::vector<std::string> read_input_file() {
std::ifstream file("input"); std::ifstream file("input");
@@ -14,32 +15,30 @@ std::vector<std::string> read_input_file() {
return data; return data;
} }
struct slope{ int x; int y; }; int count_trees(const std::vector<std::string> &map, const std::pair<int, int> &slope){
int count_trees(std::vector<std::string> map, const slope &s){
int trees = 0; int trees = 0;
int x = s.x; int x = slope.first;
int y = s.y; int y = slope.second;
int w = map[0].size(); int w = map[0].size();
int h = map.size(); int h = map.size();
while(y < h) { while(y < h) {
if(map[y][x%w] == '#') trees++; if(map[y][x%w] == '#') trees++;
x += s.x; x += slope.first;
y += s.y; y += slope.second;
} }
return trees; return trees;
} }
int solve_a(std::vector<std::string> map) { int solve_a(const std::vector<std::string> &map) {
return count_trees(map, {3, 1}); return count_trees(map, {3, 1});
} }
long solve_b(std::vector<std::string> map) { long solve_b(const std::vector<std::string> &map) {
std::vector<slope> slopes {{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}}; std::vector<std::pair<int, int>> slopes {{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}};
long result = 1; long result = 1;
for(auto& slope: slopes) result *= count_trees(map, slope); for(auto& slope: slopes) result *= count_trees(map, slope);