-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.cpp
More file actions
38 lines (34 loc) · 1.45 KB
/
Copy pathday02.cpp
File metadata and controls
38 lines (34 loc) · 1.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
#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>
#include <memory>
#include "SubmarineSystems.h"
std::vector<DiveCommand> read_input_file(const std::string& file_path) {
std::ifstream input_file{file_path};
if(!input_file.is_open()) {
throw std::runtime_error{"Could not open input file: " + file_path};
}
std::string direction{};
int distance{};
std::vector<DiveCommand> puzzle_input{};
while(input_file >> direction >> distance) {
puzzle_input.push_back({direction, distance});
}
return puzzle_input;
}
int process_commands(const std::vector<DiveCommand>& puzzle_input, std::unique_ptr<Submarine> submarine) {
for(const auto& command: puzzle_input) {
if(command.direction == "up") submarine->up(command.value);
else if(command.direction == "down") submarine->down(command.value);
else if(command.direction == "forward") submarine->forward(command.value);
else throw std::runtime_error{"Unknown command: " + command.direction};
}
return submarine->get_depth() * submarine->get_horizontal_position();
}
int main() {
const auto puzzle_input = read_input_file("input.txt");
std::cout << "Part 1: " << process_commands(puzzle_input, std::make_unique<SimpleSubmarine>()) << std::endl;
std::cout << "Part 2: " << process_commands(puzzle_input, std::make_unique<SubmarineWithAim>()) << std::endl;
return 0;
}