-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_19b.cpp
60 lines (57 loc) · 1.59 KB
/
day_19b.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
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Elf {
int prev;
int next;
bool active = true;
};
int main(int argc, char* argv[]) {
std::string input = "../input/day_19_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::getline(file, line);
const int n_elves = std::stoi(line);
std::vector<Elf> elves (n_elves);
for (int i = 1; i <= n_elves;i++) {
elves[i].next = i+1;
elves[i].prev = i-1;
elves[i].active = true;
}
elves[n_elves].next = 1;
elves[1].prev = n_elves;
int current = 1;
int to_remove = current;
int n_removed = 0;
{
const int jump = n_elves / 2;
for (int i = 0; i < jump; i++) {
to_remove = elves[to_remove].next;
}
}
while(elves[current].next != current) {
{
// Remove
auto prev = elves[to_remove].prev;
auto next = elves[to_remove].next;
while (!elves[prev].active) prev = elves[prev].prev;
while (!elves[next].active) next = elves[next].next;
elves[prev].next = next;
elves[next].prev = prev;
// std::cout << to_remove << " removed" << '\n';
elves[to_remove].active = false;
n_removed++;
}
current = elves[current].next;
to_remove = elves[to_remove].next;
while (!elves[to_remove].active) to_remove = elves[to_remove].next;
if (n_elves % 2 == 0 && n_removed % 2 == 0) to_remove = elves[to_remove].next;
else if (n_elves % 2 == 1 && n_removed % 2 == 1) to_remove = elves[to_remove].next;
}
std::cout << current << '\n';
return 0;
}