46 lines
1.1 KiB
C++
46 lines
1.1 KiB
C++
#include <algorithm>
|
|
#include <vector>
|
|
#include <set>
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
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;
|
|
}
|
|
|
|
template <typename T>
|
|
T peek_back(T it) {
|
|
return --it;
|
|
}
|
|
|
|
int parse_id(std::string ticket) {
|
|
std::replace(ticket.begin(), ticket.end(), 'F', '0');
|
|
std::replace(ticket.begin(), ticket.end(), 'L', '0');
|
|
std::replace(ticket.begin(), ticket.end(), 'B', '1');
|
|
std::replace(ticket.begin(), ticket.end(), 'R', '1');
|
|
|
|
return stoi(ticket, nullptr, 2);
|
|
}
|
|
|
|
int solve_a(const std::set<int> &ids) { return *ids.rbegin(); }
|
|
|
|
int solve_b(const std::set<int> &ids) {
|
|
for(auto it = ++ids.begin(); it != ids.end(); it++){
|
|
if(*peek_back(it) + 2 == *it) return *it - 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
std::set<int> ids;
|
|
for(auto &ticket: read_input_file()) ids.insert(parse_id(ticket));
|
|
std::cout << solve_a(ids) << " " << solve_b(ids) << std::endl;
|
|
}
|