Its cute how the c++ version of day 5 is shorter
This commit is contained in:
44
2020/day_5/binary_boarding.cc
Normal file
44
2020/day_5/binary_boarding.cc
Normal file
@@ -0,0 +1,44 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
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(int id = 0; id < 1024; id++)
|
||||
if(!ids.contains(id) && ids.contains(id+1) && ids.contains(id-1))
|
||||
return id;
|
||||
|
||||
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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -18,7 +18,9 @@ def from_binary(bsp):
|
||||
|
||||
return int(bsp.replace('F', '0').replace('L', '0').replace('B', '1').replace('R', '1'), 2)
|
||||
|
||||
def get_id(row, col): return row * 8 + col
|
||||
def get_id(row, col):
|
||||
# return row * 8 + col
|
||||
return row << 3 | col
|
||||
|
||||
def parse_seats(data):
|
||||
seats = []
|
||||
@@ -45,6 +47,3 @@ def solve_b(seats):
|
||||
seats = parse_seats(data)
|
||||
print(solve_a(seats))
|
||||
print(solve_b(seats))
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user