-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (128 loc) · 3.63 KB
/
main.cpp
File metadata and controls
141 lines (128 loc) · 3.63 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
#include <iostream>
#include <chrono>
#include <regex>
#include <unordered_set>
#include <cmath>
#include "../Utils/utils.h"
int isLineLegal(const std::string& line){
std::stack<char> symbols;
for(const auto& symbol : line){
switch(symbol){
case '(':
case '{':
case '<':
case '[':
symbols.push(symbol);
break;
case ')':
if(symbols.top() != '('){
return 3;
}
symbols.pop();
break;
case ']':
if(symbols.top() != '['){
return 57;
}
symbols.pop();
break;
case '}':
if(symbols.top() != '{'){
return 1197;
}
symbols.pop();
break;
case '>':
if(symbols.top() != '<'){
return 25137;
}
symbols.pop();
break;
default:
std::cout << "invalid char\n";
exit(-1);
}
}
return 0;
}
size_t autocomplete(const std::string& line){
std::stack<char> symbols;
int character = 0;
for(;character < line.size(); character++){
const char& symbol = line.at(character);
switch(symbol){
case '(':
case '{':
case '<':
case '[':
symbols.push(symbol);
break;
case ')':
case ']':
case '}':
case '>':
symbols.pop();
break;
default:
std::cout << "invalid char\n";
exit(-1);
}
}
size_t points = 0;
while(!symbols.empty()){
char top = symbols.top();
symbols.pop();
points *= 5;
switch(top){
case '(':
points += 1;
break;
case '[':
points += 2;
break;
case '{':
points += 3;
break;
case '<':
points += 4;
break;
default:
std::cerr << "Unexpected character on stack\n";
exit(-1);
}
}
return points;
}
void stage1(const std::vector<std::string>& puzzle){
int points = 0;
for(const auto& line : puzzle){
points += isLineLegal(line);
}
std::cout << points << "\n";
}
void stage2(const std::vector<std::string>& puzzle){
std::vector<size_t> incompleteLines;
for(const auto& line : puzzle){
if(isLineLegal(line) == 0){
incompleteLines.push_back(autocomplete(line));
}
}
std::sort(incompleteLines.begin(), incompleteLines.end(), std::greater<>());
std::cout << incompleteLines.at(incompleteLines.size()/ 2) << "\n";
}
int main(){
std::vector<std::string> 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";
}