-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_boarding.cpp
More file actions
50 lines (44 loc) · 1.19 KB
/
Copy pathbinary_boarding.cpp
File metadata and controls
50 lines (44 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Created by giuseppe on 05/12/2020.
//
#include "line.h"
#include <cassert>
#include <filesystem>
#include <fstream>
#include <ranges>
#include <algorithm>
#include <iostream>
#include <set>
int id(std::string const& boardPass)
{
int ID {0};
for (int power {9}; auto&& c : boardPass) {
if (c == 'B' || c == 'R') {
ID += (1 << power);
}
--power;
}
return ID;
}
int main()
{
const std::string example {"FBFBBFFRLR"};
assert(example.length() == 10);
int const exampleID {id(example)};
assert(exampleID == 357);
std::ifstream fileStream {std::filesystem::path {"../../files/input5.txt"}};
auto idsView {
std::ranges::istream_view<Line>(fileStream)
| std::ranges::views::transform(id)
};
std::set<int> seats;
std::ranges::copy(idsView, std::inserter(seats, seats.begin()));
std::cout << std::ranges::max(seats) << '\n';
std::erase_if(seats, [](auto const& s){ return s >= (1 << 10) - 8 || s <= 7; });
auto const found {
std::ranges::adjacent_find(seats, [](auto const& s1, auto const& s2){
return s2 - s1 == 2;
})
};
std::cout << *(found) + 1 << '\n';
}