-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
186 lines (167 loc) · 5.45 KB
/
main.cpp
File metadata and controls
186 lines (167 loc) · 5.45 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
#include <iostream>
#include <chrono>
#include <queue>
#include <format>
#include <cmath>
#include "../Utils/utils.h"
#include "../Utils/types.h"
std::string addSnailfishNumbers(std::string n1, std::string n2){
return std::format("[{},{}]", n1, n2);
}
int replaceNumber(std::string& number, int startIdx, int toAdd){
int endIdx = startIdx;
while(endIdx < number.length() && std::isdigit(number[endIdx])){
endIdx++;
}
int length = endIdx-startIdx;
std::string n = std::to_string(std::stoi(number.substr(startIdx, length)) + toAdd);
number.replace(startIdx, length, n);
return (int)n.length() - length;
}
void explode(std::string& number, int explosionIndex, int idxFirstNumberLeft){
int numberEnd = explosionIndex;
while(number[numberEnd] != ']'){
numberEnd++;
}
int length = numberEnd-explosionIndex+1;
std::vector<int> numbers;
numbers.reserve(2);
aoc_utils::splitString(number.substr(explosionIndex+1, length-1),',', numbers);
number.replace(explosionIndex, length, "0");
explosionIndex+=1;
while(explosionIndex < number.length() && !std::isdigit(number[explosionIndex])){
explosionIndex++;
}
if(explosionIndex < number.length()){
replaceNumber(number, explosionIndex, numbers[1]);
}
if(idxFirstNumberLeft != -1){
replaceNumber(number, idxFirstNumberLeft, numbers[0]);
}
}
bool split(std::string& number, int startIdx){
int endIdx = startIdx;
// if the snailfish number is correctly build, we cannot go oob here
while(std::isdigit(number[endIdx])){
endIdx++;
}
int length = endIdx-startIdx;
if(length <= 1){
return false;
}
double n = std::stof(number.substr(startIdx, length))/2;
int left = (int)std::floor(n);
int right = (int)std::ceil(n);
number.replace(startIdx, length, std::format("[{},{}]", left, right));
return true;
}
void reduceSnailfishNumber(std::string& number){
// test for explosion
bool explosion, changed ;
do{
changed = false;
do {
explosion = false;
int idxFirstRegularNumberLeft = -1;
int depth = 0;
for(int i = 0; i < number.length(); i++){
switch(number[i]){
case '[':
depth++;
break;
case ']':
depth--;
continue;
case ',':
break;
default:
continue;
}
if(depth == 5){
explode(number, i, idxFirstRegularNumberLeft);
explosion = true;
break;
}
if(std::isdigit(number[i+1])){
idxFirstRegularNumberLeft = i+1;
}
}
} while(explosion);
for(int i = 0; i < number.length(); i++){
if(split(number, i)){
changed = true;
break;
}
}
} while(changed);
}
size_t magnitude(const std::string& number){
if(number[0] != '['){
return std::stoull(number);
}
int openedBrackets = 0;
int i = 1;
for(; i < number.length()-1; i++){
switch(number[i]){
case '[':
openedBrackets++;
continue;
case ']':
openedBrackets--;
continue;
case ',':
break;
default:
continue;
}
if(openedBrackets == 0){
break;
}
}
std::string left = number.substr(1, i-1);
std::string right = number.substr(i+1, number.length()-i-1);
return magnitude(left)*3 + magnitude(right)*2;
}
void stage1(const puzzle_t& puzzle) {
std::string curr = *puzzle.begin();
for(auto it = puzzle.begin()+1; it < puzzle.end(); it++){
curr = addSnailfishNumbers(curr, *it);
reduceSnailfishNumber(curr);
}
std::cout << magnitude(curr) << "\n";
}
void stage2(const puzzle_t& puzzle) {
std::string maxSum;
size_t maxMagnitude = 0;
for(int i = 0; i < puzzle.size(); i++){
for(int j = 0; j < puzzle.size(); j++){
if(i == j) {
continue;
}
std::string currSum = addSnailfishNumbers(puzzle.at(i), puzzle.at(j));
reduceSnailfishNumber(currSum);
size_t currMagnitude = magnitude(currSum);
if(currMagnitude > maxMagnitude){
maxSum = currSum;
maxMagnitude = currMagnitude;
}
}
}
std::cout << maxMagnitude << "\n";
}
int main(){
puzzle_t puzzle;
std::string input = "example";
input = "input";
aoc_utils::load_puzzle(input, puzzle);
auto start = std::chrono::high_resolution_clock::now();
stage1(puzzle);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Stage 1 took " << elapsed.count() << " seconds\n";
start = std::chrono::high_resolution_clock::now();
stage2(puzzle);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stage 2 took " << elapsed.count() << " seconds\n";
}