-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
50 lines (41 loc) · 1.05 KB
/
main.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
#include <iostream>
#include <unordered_map>
#include "boost/spirit/home/x3.hpp"
#include "utils.h"
using T = std::vector<int>;
T parseFile()
{
auto buffer = AOC::readFile("input.txt");
std::vector<int> r;
using namespace boost::spirit::x3;
auto result = parse(buffer.begin(), buffer.end(), int_ % ",", r);
assert(result);
return r;
}
int f1(const T &t, int turnCount)
{
std::unordered_map<int, int> count;
std::unordered_map<int, int> most;
std::unordered_map<int, int> more;
for (int i = 0; i < t.size(); ++i) {
++count[t[i]];
most[t[i]] = i;
}
int last = t.back();
for (int i = t.size(); i < turnCount + t.size(); ++i) {
if (count[last] <= 1) {
last = 0;
} else {
last = most[last] - more[last];
}
++count[last];
more[last] = most[last];
most[last] = i;
}
return last;
}
int main() {
const auto input = parseFile();
const long iter = 30000000;
std::cout << f1(input, iter - input.size()) << std::endl;
}