This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6-tuning-trouble.cc
70 lines (55 loc) · 1.71 KB
/
day6-tuning-trouble.cc
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "helper.hh"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <type_traits>
static constexpr size_t signalLengthPart1 = 4U;
static constexpr size_t signalLengthPart2 = 14U;
int main(const int argc, const char *argv[]) {
if (argc < 3) {
return 1;
}
std::ifstream fileStream(argv[1], std::ios::in);
if (!fileStream) {
std::cerr << "Failed to open or read file \'" << argv[1] << "\'\n";
return 1;
}
[[maybe_unused]] bool part1 = (*argv[2] == '0');
const size_t signalLength =
(part1) ? (signalLengthPart1) : (signalLengthPart2);
std::string line;
std::getline(fileStream, line);
if (line.size() < signalLength) {
return 1;
}
using Signal_t = unsigned char;
std::array<int, std::numeric_limits<Signal_t>::max()> countInWindow{};
auto left = line.cbegin();
auto right = std::next(line.cbegin(), signalLength);
std::for_each(left, right, [&countInWindow](const Signal_t signal) {
countInWindow[signal]++;
});
const auto begin = line.cbegin();
auto isWindowUnique = [&countInWindow, &left, &right]() -> bool {
return find_if(left, right, [&countInWindow](const Signal_t signal) {
return countInWindow[signal] > 1;
}) == right;
};
auto printAnswer = [&left, &right, &begin] {
std::copy(left, right, std::ostream_iterator<char>{std::cout});
std::cout << '\n' << std::distance(begin, right) << std::endl;
};
auto slideWindow = [&left, &right, &countInWindow] {
countInWindow[*left]--;
++left;
countInWindow[*right]++;
++right;
};
for (const auto end = line.cend(); right != end; slideWindow()) {
if (isWindowUnique()) {
printAnswer();
break;
}
}
}