-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path06.cpp
66 lines (55 loc) · 1.51 KB
/
06.cpp
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
#include <algorithm>
#include <chrono>
#include <iostream>
#include <unordered_map>
#include <vector>
using std::vector;
inline unsigned long hash_banks(const vector<int> &banks) noexcept {
unsigned long h = 0;
for (const auto &b : banks) {
h <<= 4;
h += static_cast<unsigned long>(b);
}
return h;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
unsigned int pt1 = 0;
unsigned int pt2 = 0;
std::string input;
std::vector<int> banks;
while (std::getline(std::cin, input, '\t')) {
banks.push_back(std::stoi(input));
}
// perform redistribution cycles
std::unordered_map<unsigned long, unsigned int> seen;
const auto begin = banks.begin();
const auto end = banks.end();
while (true) {
auto max = std::max_element(begin, end);
for (auto it = max + 1; *max > 0; it++) {
if (it == end) {
it = begin;
}
(*it)++;
(*max)--;
}
pt1++;
const unsigned long h = hash_banks(banks);
if (seen.contains(h)) [[unlikely]] {
pt2 = pt1 - seen[h] + 1;
break;
}
seen[h] = pt1;
}
std::cout << "--- Day 6: Memory Reallocation ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}