-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
74 lines (64 loc) · 2 KB
/
main.cpp
File metadata and controls
74 lines (64 loc) · 2 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
#include <iostream>
#include <chrono>
#include "../Utils/utils.h"
void stage(const std::vector<std::string>& puzzle, int stage = 1){
int horizontal = 0, depth = 0, aim = 0;
for(auto& line : puzzle){
std::stringstream command(line.c_str());
std::string direction;
int units;
command >> direction >> units;
if(direction == "forward"){
horizontal += units;
if(stage == 2){
depth += (aim * units);
}
}
if(direction == "down"){
aim += units;
if(stage == 1){
depth += units;
}
}
if(direction == "up"){
aim -= units;
if(stage == 1){
depth -= units;
}
}
}
std::cout << horizontal * depth << "\n";
}
void stage2(const std::vector<std::string>& puzzle){
int horizontal = 0, depth = 0, aim = 0;
for(auto& line : puzzle){
std::stringstream command(line.c_str());
std::string direction;
int units;
command >> direction >> units;
if(direction == "forward"){
horizontal += units;
}
if(direction == "down"){
}
if(direction == "up"){
aim -= units;
}
}
std::cout << horizontal * depth << "\n";
}
int main(){
std::vector<std::string> puzzle;
std::string input = "input";
aoc_utils::load_puzzle(input, puzzle);
auto start = std::chrono::high_resolution_clock::now();
stage(puzzle, 1);
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();
stage(puzzle, 2);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "Stage 2 took " << elapsed.count() << " seconds\n";
}