Created
December 5, 2020 05:59
-
-
Save orendon/466a86609b1add2b016673263471ac04 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Day 5 Binary Boarding
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 <iostream> | |
#include <string> | |
#include <queue> | |
using namespace std; | |
int process(string&); | |
int main() { | |
string pass; | |
priority_queue<int> ids; | |
while(getline(cin, pass)) { | |
ids.push(process(pass)); | |
} | |
// part 1 | |
cout << "Part 1: " << ids.top() << "\n"; | |
// part 2 | |
int last = ids.top(); | |
while (!ids.empty()) { | |
ids.pop(); | |
if (ids.top() != last-1){ | |
cout << "Part 2: " << last-1 << "\n"; | |
break; | |
} | |
last = ids.top(); | |
} | |
return 0; | |
} | |
short search(string &str, short upper, short n, char left){ | |
short lower=0, mid; | |
for(short i=0; i<=n; i++) { | |
mid = (lower + upper) / 2; | |
if (str[i] == left) upper = mid; | |
else lower = mid+1; | |
} | |
return mid; | |
} | |
int process(string &pass){ | |
short row, col; | |
row = search(pass, 127, 7, 'F'); | |
string column = pass.substr(7); | |
col = search(column, 7, 3, 'L'); | |
return row*8 + col; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment