Created
December 3, 2020 16:42
-
-
Save orendon/8f6198f5d54cfc037679e66f210bb233 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Day 3 (Toboggan Trajectory)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
typedef long long ll; | |
typedef pair<int, int> pii; | |
const char TREE = '#'; | |
int slope(vector<string> &forest, int right, int down){ | |
int row=0, col=0, cols_max=forest[0].size(); | |
int row_target=forest.size()-1, trees_count=0; | |
while(row < row_target){ | |
col += right; | |
row += down; | |
if(col >= cols_max) col %= cols_max; // also col -= cols_max | |
if(forest[row][col] == TREE) trees_count++; | |
} | |
return trees_count; | |
} | |
int main() { | |
ios::sync_with_stdio(0);cin.tie(0); | |
// read inputs | |
string line; | |
vector<string> forest; | |
while (getline(cin, line)) { | |
forest.push_back(line); | |
} | |
// part 1 | |
int slope31 = slope(forest, 3, 1); | |
cout << "Part 1: " << slope31 << "\n"; | |
// part 2 | |
ll answer=slope31; | |
vector<pii> moves = {{1,1}, {5,1}, {7,1}, {1,2}}; | |
for(auto &[right, left]: moves){ | |
answer *= slope(forest, right, left); | |
} | |
cout << "Part 2: " << answer << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment