Stuff
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
std::vector<std::string> read_input_file() {
|
||||
std::ifstream file("input");
|
||||
@@ -14,32 +15,30 @@ std::vector<std::string> read_input_file() {
|
||||
return data;
|
||||
}
|
||||
|
||||
struct slope{ int x; int y; };
|
||||
|
||||
int count_trees(std::vector<std::string> map, const slope &s){
|
||||
int count_trees(const std::vector<std::string> &map, const std::pair<int, int> &slope){
|
||||
int trees = 0;
|
||||
|
||||
int x = s.x;
|
||||
int y = s.y;
|
||||
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 += s.x;
|
||||
y += s.y;
|
||||
x += slope.first;
|
||||
y += slope.second;
|
||||
}
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
long solve_b(std::vector<std::string> map) {
|
||||
std::vector<slope> slopes {{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}};
|
||||
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);
|
||||
Reference in New Issue
Block a user