day 3 solution on c++ :3
This commit is contained in:
@@ -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())
|
||||
|
||||
57
2020/day_3/toboggan_trayectory.cc
Normal file
57
2020/day_3/toboggan_trayectory.cc
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user