Created
December 12, 2020 23:57
-
-
Save orendon/24628b77b6adddbd983bc0eca4a8418d to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Day 12 Rain Risk
This file contains 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 <algorithm> | |
#include <fstream> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
#define forn(i, j, n) for (int i = j; i < (int)n; i++) | |
#define forr(i, j, n) for (int i = j; i >= (int)n; i--) | |
#define fore(i, coll) for (auto i : coll) | |
#define endl '\n' | |
#define pb push_back | |
#define all(x) x.begin(), x.end() | |
typedef long long ll; | |
typedef pair<int, int> pii; | |
typedef vector<int> vi; | |
typedef vector<string> vs; | |
int new_dir (vector<char> &, char, int); | |
int main() { | |
// read inputs | |
ifstream fin("inputs/12.txt"); | |
char act; short val; | |
vector<pair<char, short>> inputs; | |
while (fin >> act >> val) inputs.pb({act, val}); | |
// part 1 | |
vector<char> rights = {'N', 'E', 'S', 'W'}; | |
vector<char> lefts = {'N', 'W', 'S', 'E'}; | |
char curr='E'; | |
int x=0, y=0; | |
for (auto &[action, value] : inputs) { | |
if (action == 'F'){ | |
if (curr=='N') y+=value; | |
if (curr=='S') y-=value; | |
if (curr=='E') x+=value; | |
if (curr=='W') x-=value; | |
} | |
else if (action=='N') y+=value; | |
else if (action=='S') y-=value; | |
else if (action=='E') x+=value; | |
else if (action=='W') x-=value; | |
else if (action=='R') curr = new_dir(rights, curr, value/90); | |
else if (action=='L') curr = new_dir(lefts, curr, value/90); | |
} | |
cout << "Part 1: " << abs(x) + abs(y) << endl; | |
// part 2 | |
curr='E'; | |
x=0, y=0; | |
int wx=10, wy=1; | |
for (auto &[action, value] : inputs) { | |
if (action == 'F'){ | |
x += value*wx; | |
y += value*wy; | |
} | |
else if (action=='N') wy+=value; | |
else if (action=='S') wy-=value; | |
else if (action=='E') wx+=value; | |
else if (action=='W') wx-=value; | |
else if (action=='R') { | |
if (value == 90) { swap(wx, wy); wy=-wy; } | |
if (value == 180) { wx=-wx; wy=-wy; } | |
if (value == 270) { swap(wx, wy); wx=-wx; } | |
} | |
else if (action=='L') { | |
if (value == 90) { swap(wx, wy); wx=-wx; } | |
if (value == 180) { wx=-wx; wy=-wy; } | |
if (value == 270) { swap(wx, wy); wy=-wy; } | |
} | |
} | |
cout << "Part 2: " << abs(x) + abs(y) << endl; | |
return 0; | |
} | |
int new_dir (vector<char> &dirs, char current, int moves){ | |
short curr_pos=-1; | |
forn(i,0,4) if(dirs[i]==current) curr_pos=i; | |
short facing_to = (curr_pos + moves) % 4; | |
return dirs[facing_to]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment