Repository navigation
Expand file tree
/
Copy path20b.cpp
More file actions
122 lines (101 loc) · 2.19 KB
/
Copy path20b.cpp
File metadata and controls
122 lines (101 loc) · 2.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdint.h>
const std::string INPUT_FILE = "./data/20.txt";
const int64_t KEY = 811589153;
class Node {
public:
int64_t val;
Node* prev;
Node* next;
void print() {
printf("Node: { val: %ld, prev: %ld, next: %ld }\n", val, prev->val, next->val);
}
};
class Head {
public:
Node* head;
std::vector<Node*> nodes;
void print() {
Node* n = head;
do {
std::cout << n->val << ", ";
n = n->next;
} while (n != head);
std::cout << std::endl;
}
Node* walk(Node* node, int64_t steps) {
int64_t mod = nodes.size() - 1;
steps = ((steps % mod) + mod) % mod;
while (steps < 0) {
node = node->prev;
++steps;
}
while (steps > 0) {
node = node->next;
--steps;
}
return node;
}
void mix_step(uint num) {
Node* n = nodes.at(num);
Node* other = walk(n, n->val);
if (other == n) {
return;
}
Node* a = n->prev;
Node* b = n->next;
a->next = b;
b->prev = a;
Node* succ = other->next;
succ->prev = n;
other->next = n;
n->prev = other;
n->next = succ;
}
};
Head read_list() {
std::ifstream infile(INPUT_FILE);
std::string line;
std::getline(infile, line);
Node* head = new Node({std::stoi(line) * KEY, nullptr, nullptr});
auto nodes = std::vector<Node*>();
nodes.push_back(head);
Node* prev = head;
while (std::getline(infile, line)) {
int64_t val = std::stoi(line);
Node* n = new Node({
val * KEY,
prev,
nullptr
});
prev->next = n;
prev = n;
nodes.push_back(n);
}
head->prev = prev;
prev->next = head;
return Head({head, nodes});
}
int main() {
auto head = read_list();
head.head->print();
for (int i = 0; i < 10; ++i) {
for (uint x = 0; x < head.nodes.size(); ++x) {
head.mix_step(x);
}
}
head.print();
Node* zero = head.head;
while (zero->val != 0) {
zero = zero->next;
}
int64_t a = head.walk(zero, 1000)->val;
int64_t b = head.walk(zero, 2000)->val;
int64_t c = head.walk(zero, 3000)->val;
int64_t res = a + b + c;
std::cout << "Result: " << res << std::endl;
return 0;
}