day 3 solution on c++ :3

This commit is contained in:
Daniel Cortés
2020-12-03 05:53:45 -03:00
parent 0202f8bc15
commit 85b305265e
2 changed files with 60 additions and 1 deletions

View File

@@ -22,7 +22,9 @@ def solve_a():
def solve_b():
slopes = ((1, 1), (3, 1), (5, 1), (7, 1), (1, 2))
return prod([count_trees(slope) for slope in slopes])
results = [count_trees(slope) for slope in slopes]
print(results)
return prod(results)
print(solve_a())

View File

@@ -0,0 +1,57 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> read_input_file() {
std::ifstream file("input");
std::vector<std::string> data;
std::string line;
while(std::getline(file, line))
data.push_back(line);
return data;
}
struct slope{ int x; int y; };
int count_trees(std::vector<std::string> map, const slope &s){
int trees = 0;
int x = s.x;
int y = s.y;
int w = map[0].size();
int h = map.size();
while(y < h) {
if(map[y][x%w] == '#') trees++;
x += s.x;
y += s.y;
}
return trees;
}
int solve_a(std::vector<std::string> map) {
return count_trees(map, {3, 1});
}
long solve_b(std::vector<std::string> map) {
std::vector<slope> slopes {{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}};
long result = 1;
for(auto& slope: slopes) result *= count_trees(map, slope);
return result;
}
int main() {
auto data = read_input_file();
std::cout << solve_a(data) << std::endl;
std::cout << solve_b(data) << std::endl;
return 0;
}