Repository navigation
Expand file tree
/
Copy path13b.cpp
More file actions
195 lines (164 loc) · 4.95 KB
/
Copy path13b.cpp
File metadata and controls
195 lines (164 loc) · 4.95 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <fstream>
#include <iostream>
#include <vector>
#include <assert.h>
#include <variant>
#include <memory>
#include <algorithm>
const std::string INPUT_FILE = "./data/13.txt";
class Packet;
typedef std::variant<int, std::unique_ptr<Packet>> val;
size_t get_arg(std::string_view s) {
assert(s.size() != 0);
int count = 0;
for (size_t i = 0; i < s.size(); ++i) {
assert (count >= 0);
switch (s[i]) {
case '[': ++count; break;
case ']': --count; break;
case ',': if (count == 0) return i;
default: ;
}
}
return s.size();
}
class Packet {
public:
std::vector<val> content;
Packet(std::string_view s) {
content = std::vector<val>();
std::string word;
size_t i = 0;
while (i < s.size()) {
auto j = get_arg(s.substr(i, s.size()));
auto t = s.substr(i, j);
if (t.at(0) == '[') {
val value = std::make_unique<Packet>(Packet(t.substr(1, t.size() - 2)));
content.push_back(std::move(value));
} else {
int a = std::stoi(std::string(t));
content.push_back(a);
}
i = i + j + 1;
}
}
void print() const {
std::cout << "[";
for (size_t i = 0; i < content.size(); ++i) {
auto value = &content[i];
if (std::holds_alternative<int>(*value)) {
std::cout << std::get<int>(*value);
} else {
std::get<1>(*value)->print();
}
if (i != content.size() - 1) std::cout << ",";
}
std::cout << "]";
std::cout.flush();
}
std::weak_ordering operator<=>(const Packet &other) const {
for (size_t i = 0; i < std::min(content.size(), other.content.size()); ++i) {
bool a = std::holds_alternative<int>(content[i]);
bool b = std::holds_alternative<int>(other.content[i]);
std::weak_ordering comp = 0 <=> 0;
if (a && b) {
comp = std::get<int>(content[i]) <=> std::get<int>(other.content[i]);
} else if (!a && !b) {
comp = *std::get<1>(content[i]) <=> *std::get<1>(other.content[i]);
} else if (a) {
Packet temp = Packet(std::to_string(std::get<int>(content[i])));
comp = temp <=> *std::get<1>(other.content[i]);
} else {
Packet temp = Packet(std::to_string(std::get<int>(other.content[i])));
comp = *std::get<1>(content[i]) <=> temp;
}
if (comp == 0) continue;
return comp;
}
return content.size() <=> other.content.size();
}
};
std::vector<Packet> read() {
std::ifstream infile(INPUT_FILE);
std::vector<Packet> data;
std::string line;
while (std::getline(infile, line)) {
if (line == "") continue;
data.emplace_back(Packet(line.substr(1, line.size() - 2)));
}
return data;
}
void test1() {
std::string s = "1,1,3,1,1";
auto i = 0;
auto j = get_arg(s.substr(i, s.size()));
auto t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("1" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("1" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("3" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("1" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("1" == t);
}
void test2() {
std::string s = "[1],[2,3,4]";
auto i = 0;
auto j = get_arg(s.substr(i, s.size()));
auto t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("[1]" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("[2,3,4]" == t);
}
void test3() {
std::string s = "1,[2,[3,[4,[5,6,7]]]],8,9";
auto i = 0;
auto j = get_arg(s.substr(i, s.size()));
auto t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("1" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("[2,[3,[4,[5,6,7]]]]" == t);
i = i + j + 1;
j = get_arg(s.substr(i, s.size()));
t = s.substr(i, j);
std::cout << "i: " << i << ", j: " << j << ", t: " << t << std::endl;
assert("8" == t);
}
int main() {
auto input = read();
Packet key_a = Packet("[2]");
const Packet key_b = Packet("[6]");
input.push_back(Packet("[2]"));
input.push_back(Packet("[6]"));
std::sort(input.begin(), input.end());
int res = 1;
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] <=> key_a == 0 || input[i] <=> key_b == 0)
res *= (i+1);
}
std::cout << "Result: " << res << std::endl;
return 0;
}