Stuff
This commit is contained in:
56
2020/day_3/toboggan_trajectory.cc
Normal file
56
2020/day_3/toboggan_trajectory.cc
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int count_trees(const std::vector<std::string> &map, const std::pair<int, int> &slope){
|
||||
int trees = 0;
|
||||
|
||||
int x = slope.first;
|
||||
int y = slope.second;
|
||||
int w = map[0].size();
|
||||
int h = map.size();
|
||||
|
||||
while(y < h) {
|
||||
if(map[y][x%w] == '#') trees++;
|
||||
|
||||
x += slope.first;
|
||||
y += slope.second;
|
||||
}
|
||||
|
||||
return trees;
|
||||
}
|
||||
|
||||
int solve_a(const std::vector<std::string> &map) {
|
||||
return count_trees(map, {3, 1});
|
||||
}
|
||||
|
||||
long solve_b(const std::vector<std::string> &map) {
|
||||
std::vector<std::pair<int, int>> 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