From b0a39f2a03d4ab5493bfc46525a856b3092dd500 Mon Sep 17 00:00:00 2001 From: mid-kid Date: Fri, 3 Dec 2021 01:49:16 +0100 Subject: [PATCH] Add day 2 --- Makefile | 2 +- d01.cc | 11 +- d02.cc | 79 ++++ d02_input.txt | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++ d02_output.txt | 2 + 5 files changed, 1089 insertions(+), 5 deletions(-) create mode 100644 d02.cc create mode 100644 d02_input.txt create mode 100644 d02_output.txt diff --git a/Makefile b/Makefile index 7f3951b..1d7fdfe 100644 --- a/Makefile +++ b/Makefile @@ -1 +1 @@ -CXXFLAGS := -std=c++17 -O2 -Wall -Wextra +CXXFLAGS := -std=c++20 -O2 -Wall -Wextra diff --git a/d01.cc b/d01.cc index 077b955..dbcd88b 100644 --- a/d01.cc +++ b/d01.cc @@ -5,12 +5,15 @@ using namespace std; int main(int argc, char *argv[]) { if (argc <= 1) return 1; - ifstream input(argv[1]); vector numbers; - unsigned num; - while (input >> num) { - numbers.push_back(num); + { + ifstream input(argv[1]); + if (!input.is_open()) return 1; + unsigned num; + while (input >> num) { + numbers.push_back(num); + } } unsigned part1 = 0; diff --git a/d02.cc b/d02.cc new file mode 100644 index 0000000..78abe94 --- /dev/null +++ b/d02.cc @@ -0,0 +1,79 @@ +#include +#include +#include +using namespace std; + +enum cmd { + CMD_FORWARD, + CMD_DOWN, + CMD_UP, +}; + +struct commands { + enum cmd cmd; + int pos; +}; + +int main(int argc, char *argv[]) { + if (argc <= 1) return 1; + + vector commands; + { + ifstream input(argv[1]); + if (!input.is_open()) return 1; + for (string line; getline(input, line);) { + struct commands command; + + unsigned split = line.find(' '); + string cmd = line.substr(0, split); + line.erase(0, split + 1); + if (cmd == "forward") { + command.cmd = CMD_FORWARD; + } else if (cmd == "down") { + command.cmd = CMD_DOWN; + } else if (cmd == "up") { + command.cmd = CMD_UP; + } + command.pos = stoi(line); + commands.push_back(command); + } + } + + int pos_h = 0; + int pos_d = 0; + for (struct commands command : commands) { + switch (command.cmd) { + case CMD_FORWARD: + pos_h += command.pos; + break; + case CMD_DOWN: + pos_d += command.pos; + break; + case CMD_UP: + pos_d -= command.pos; + break; + } + } + + cout << pos_h * pos_d << endl; + + int aim = 0; + pos_h = 0; + pos_d = 0; + for (struct commands command : commands) { + switch (command.cmd) { + case CMD_FORWARD: + pos_h += command.pos; + pos_d += aim * command.pos; + break; + case CMD_DOWN: + aim += command.pos; + break; + case CMD_UP: + aim -= command.pos; + break; + } + } + + cout << pos_h * pos_d << endl; +} diff --git a/d02_input.txt b/d02_input.txt new file mode 100644 index 0000000..64f1ce2 --- /dev/null +++ b/d02_input.txt @@ -0,0 +1,1000 @@ +forward 1 +down 9 +down 4 +forward 4 +down 2 +down 7 +up 9 +down 5 +forward 9 +forward 9 +down 2 +forward 7 +forward 9 +down 5 +down 2 +up 9 +down 7 +forward 6 +down 6 +down 6 +up 1 +down 2 +forward 8 +forward 8 +forward 9 +up 1 +forward 4 +up 1 +forward 3 +forward 5 +up 3 +up 7 +down 4 +down 1 +forward 3 +forward 4 +up 3 +forward 9 +down 2 +forward 5 +up 2 +up 1 +up 5 +forward 1 +up 9 +forward 6 +up 4 +down 4 +forward 3 +up 7 +down 1 +down 1 +down 8 +forward 3 +forward 1 +up 7 +up 1 +down 4 +up 6 +down 6 +forward 8 +down 3 +down 9 +down 6 +up 8 +forward 8 +down 9 +up 7 +down 7 +forward 8 +down 8 +up 6 +down 7 +down 4 +up 7 +forward 7 +forward 7 +forward 2 +forward 5 +down 5 +down 9 +down 8 +down 5 +up 3 +down 3 +down 6 +down 6 +forward 1 +down 2 +forward 2 +down 5 +up 2 +forward 5 +down 1 +down 3 +forward 4 +down 2 +forward 8 +down 6 +forward 3 +forward 3 +forward 9 +down 9 +down 4 +down 8 +down 7 +down 3 +up 6 +down 7 +forward 9 +up 9 +down 8 +forward 9 +forward 8 +down 2 +up 4 +forward 8 +up 7 +down 2 +forward 6 +forward 7 +down 5 +up 4 +down 3 +down 3 +down 5 +down 2 +down 5 +forward 8 +forward 8 +down 5 +up 4 +down 6 +forward 5 +up 6 +up 5 +down 4 +forward 7 +forward 5 +down 7 +up 9 +up 4 +up 9 +forward 8 +down 8 +down 8 +down 2 +down 8 +down 8 +up 6 +forward 2 +down 9 +forward 5 +forward 4 +down 8 +up 5 +forward 3 +forward 1 +down 6 +forward 9 +forward 2 +forward 1 +up 5 +down 2 +forward 5 +up 6 +up 6 +forward 9 +up 3 +forward 2 +forward 9 +down 9 +down 5 +up 4 +up 7 +up 8 +forward 4 +forward 3 +down 7 +down 5 +forward 5 +down 6 +forward 9 +up 4 +up 6 +down 2 +forward 6 +forward 7 +up 9 +up 5 +down 4 +up 5 +down 2 +forward 4 +up 9 +down 1 +forward 7 +down 9 +forward 5 +down 5 +down 2 +up 4 +up 8 +down 8 +down 7 +up 7 +down 1 +forward 5 +forward 8 +down 2 +down 1 +forward 8 +down 7 +up 6 +forward 9 +forward 4 +down 8 +forward 1 +up 8 +forward 3 +up 4 +forward 9 +down 6 +forward 5 +forward 1 +forward 8 +forward 5 +forward 8 +down 4 +forward 8 +forward 2 +forward 8 +forward 3 +down 3 +forward 6 +up 6 +up 9 +down 4 +down 6 +forward 6 +down 9 +up 2 +down 8 +down 6 +down 1 +forward 1 +down 2 +down 3 +forward 5 +forward 6 +down 5 +up 7 +up 2 +forward 2 +down 4 +down 4 +forward 3 +down 4 +up 4 +forward 1 +down 4 +forward 9 +forward 7 +forward 6 +down 3 +down 4 +forward 3 +forward 3 +forward 3 +up 6 +forward 4 +forward 7 +forward 3 +forward 5 +forward 7 +forward 4 +down 1 +up 2 +up 7 +forward 4 +down 2 +forward 2 +up 7 +down 7 +forward 5 +forward 9 +down 8 +forward 5 +forward 5 +down 9 +forward 1 +forward 6 +down 2 +down 3 +down 1 +down 7 +down 3 +up 3 +down 3 +forward 5 +down 4 +forward 6 +forward 2 +down 4 +forward 4 +up 5 +up 3 +forward 2 +forward 5 +down 7 +up 9 +up 5 +down 3 +forward 8 +forward 4 +down 8 +up 4 +forward 8 +forward 7 +forward 1 +down 2 +down 6 +up 7 +forward 9 +down 2 +down 4 +down 3 +down 8 +up 4 +down 2 +up 3 +forward 1 +down 2 +up 6 +down 9 +up 3 +down 1 +down 1 +forward 4 +down 2 +forward 3 +forward 5 +forward 7 +down 7 +up 3 +up 5 +down 1 +down 2 +forward 4 +down 1 +down 1 +down 7 +up 4 +forward 3 +down 3 +forward 2 +down 3 +up 7 +down 1 +up 5 +down 7 +forward 4 +up 2 +forward 5 +up 4 +down 4 +down 3 +forward 1 +up 9 +down 5 +forward 9 +forward 1 +down 7 +down 2 +down 4 +down 2 +forward 3 +down 1 +down 2 +up 9 +forward 2 +down 8 +forward 1 +forward 3 +down 5 +down 4 +forward 2 +forward 8 +forward 9 +down 9 +down 6 +up 3 +forward 1 +down 6 +down 7 +up 3 +forward 9 +down 2 +up 4 +forward 3 +forward 6 +forward 6 +down 7 +forward 7 +forward 6 +down 7 +up 9 +forward 7 +down 1 +down 8 +down 2 +down 2 +down 9 +down 6 +forward 1 +down 9 +forward 9 +forward 8 +forward 6 +forward 9 +down 5 +forward 9 +forward 4 +forward 4 +down 6 +forward 6 +forward 5 +forward 7 +down 7 +down 9 +forward 4 +down 9 +up 5 +forward 6 +down 6 +forward 9 +up 7 +forward 6 +up 6 +up 9 +forward 9 +up 4 +down 3 +up 9 +down 8 +down 2 +forward 4 +down 8 +down 2 +forward 2 +down 5 +up 3 +down 1 +forward 4 +down 3 +forward 3 +down 9 +forward 6 +forward 6 +down 9 +forward 6 +forward 1 +down 5 +up 1 +forward 3 +forward 1 +forward 5 +down 8 +forward 7 +forward 2 +up 3 +forward 8 +forward 1 +forward 7 +down 1 +down 4 +forward 7 +forward 9 +forward 4 +down 2 +forward 5 +forward 3 +forward 9 +forward 3 +up 3 +forward 9 +down 3 +forward 9 +forward 3 +forward 7 +down 5 +forward 6 +down 6 +forward 5 +forward 1 +down 1 +forward 8 +down 3 +up 1 +down 6 +forward 7 +up 5 +up 7 +down 3 +forward 8 +up 8 +down 4 +up 6 +up 7 +forward 1 +forward 2 +forward 7 +down 1 +forward 4 +down 8 +up 6 +up 7 +up 4 +down 7 +down 8 +down 6 +forward 5 +forward 9 +down 7 +forward 8 +down 9 +down 9 +forward 5 +forward 2 +up 4 +down 2 +forward 3 +down 2 +forward 1 +down 7 +down 6 +forward 5 +up 2 +forward 4 +down 4 +forward 5 +forward 7 +up 5 +down 7 +forward 9 +forward 3 +forward 6 +up 8 +down 9 +forward 1 +up 8 +forward 3 +down 6 +down 1 +down 3 +down 4 +up 2 +up 3 +forward 8 +up 7 +down 2 +forward 1 +down 6 +forward 1 +down 8 +forward 3 +forward 5 +forward 5 +up 5 +forward 4 +forward 6 +down 8 +up 7 +forward 8 +up 6 +forward 5 +down 4 +forward 8 +down 3 +forward 7 +forward 9 +forward 8 +up 9 +forward 4 +up 5 +up 8 +down 8 +forward 1 +down 1 +forward 3 +forward 9 +down 9 +up 2 +forward 5 +down 1 +down 6 +forward 2 +forward 2 +down 2 +down 8 +forward 5 +up 2 +up 2 +forward 3 +down 6 +up 2 +down 2 +down 1 +up 9 +forward 6 +up 4 +forward 4 +down 2 +forward 8 +up 9 +down 1 +forward 9 +up 3 +up 7 +forward 5 +down 9 +down 2 +down 5 +up 4 +up 8 +up 2 +down 1 +up 4 +forward 8 +forward 2 +down 8 +forward 9 +forward 1 +down 5 +forward 3 +down 6 +forward 3 +up 2 +down 9 +down 6 +down 4 +down 9 +down 8 +forward 2 +forward 7 +up 4 +forward 8 +up 9 +up 6 +forward 2 +down 1 +down 5 +down 5 +down 3 +up 9 +up 9 +forward 9 +forward 5 +down 1 +down 3 +down 8 +down 4 +forward 5 +up 4 +down 2 +down 5 +down 4 +down 8 +down 2 +forward 9 +forward 7 +up 9 +forward 8 +down 9 +down 2 +forward 5 +down 9 +forward 7 +down 2 +down 9 +forward 4 +up 7 +down 9 +forward 6 +forward 2 +down 1 +down 9 +down 3 +down 7 +down 8 +down 5 +down 9 +down 1 +up 9 +down 7 +forward 7 +up 4 +down 2 +down 4 +forward 6 +forward 8 +forward 1 +forward 2 +up 2 +up 2 +forward 9 +down 9 +forward 8 +forward 6 +forward 5 +down 2 +forward 7 +up 3 +up 8 +forward 1 +forward 5 +down 7 +down 7 +forward 5 +down 1 +up 3 +up 8 +forward 5 +forward 7 +forward 3 +down 6 +up 7 +forward 8 +down 5 +up 3 +forward 7 +down 2 +down 1 +down 1 +forward 1 +down 1 +down 8 +down 3 +up 1 +forward 8 +forward 9 +forward 5 +forward 8 +down 2 +forward 3 +down 5 +up 9 +down 9 +down 5 +forward 1 +up 9 +up 4 +up 7 +up 9 +up 4 +down 2 +forward 6 +forward 4 +up 5 +up 6 +down 8 +down 6 +down 6 +up 3 +forward 3 +down 9 +down 5 +down 5 +forward 7 +down 6 +forward 7 +down 9 +down 8 +down 3 +down 7 +down 4 +down 1 +forward 6 +up 7 +down 1 +forward 7 +down 2 +forward 4 +down 3 +forward 6 +up 7 +forward 3 +down 3 +up 6 +down 2 +down 4 +down 8 +forward 6 +down 4 +forward 7 +down 2 +forward 9 +down 3 +down 6 +down 3 +down 6 +forward 4 +down 8 +up 5 +down 6 +forward 6 +down 6 +forward 9 +up 8 +down 7 +up 1 +forward 4 +up 3 +forward 9 +up 6 +forward 4 +up 8 +forward 2 +down 7 +down 7 +forward 1 +up 9 +up 7 +forward 3 +down 1 +up 5 +forward 3 +forward 1 +forward 3 +forward 7 +down 6 +down 1 +forward 9 +up 1 +up 1 +down 8 +down 7 +forward 1 +up 8 +up 6 +down 8 +forward 1 +up 3 +down 7 +forward 7 +down 7 +forward 4 +forward 6 +up 2 +down 9 +forward 9 +down 4 +up 2 +forward 7 +forward 4 +up 8 +up 5 +down 8 +forward 7 +forward 6 +down 1 +up 5 +up 5 +down 3 +forward 7 +forward 1 +forward 7 +forward 1 +down 3 +forward 3 +forward 4 +forward 6 +down 3 +up 4 +up 5 +down 8 +up 3 +forward 4 +forward 6 +forward 8 +forward 4 +forward 7 +down 9 +up 3 +down 1 +up 4 +forward 8 +forward 5 +forward 4 +forward 1 +down 2 +down 7 +down 6 +forward 4 +forward 2 +up 2 +down 2 +forward 2 +forward 2 +up 6 +forward 6 +forward 1 +forward 8 +up 7 +forward 4 +forward 2 +down 2 +down 1 +forward 4 +down 4 +up 7 +up 2 +up 3 +down 5 +forward 2 +forward 9 +down 2 +down 9 +down 4 +down 8 +up 9 +up 5 +up 2 +forward 6 +down 6 +forward 9 +forward 7 +forward 8 +forward 4 +up 9 +forward 9 +down 8 +down 2 +forward 5 +down 8 +down 8 +forward 8 +forward 6 +forward 7 +down 8 +forward 1 +forward 6 +down 4 +forward 1 +down 3 +forward 1 +down 8 +forward 2 +down 4 +down 6 +down 4 +up 9 +forward 6 +up 3 +down 7 +forward 3 +down 8 +forward 6 +forward 3 +down 1 +forward 1 +up 6 +down 1 +down 2 +forward 5 +down 3 +up 5 +forward 6 +forward 7 +forward 2 +forward 9 +down 7 +up 4 +forward 6 +forward 8 +down 6 +down 7 +down 4 +down 5 +down 6 +down 3 +up 1 +forward 2 +forward 3 +down 1 +down 7 +up 8 +forward 1 +forward 6 +up 3 +down 1 +up 6 +down 2 +up 1 +up 7 +forward 3 +up 8 +forward 7 +forward 5 +forward 5 diff --git a/d02_output.txt b/d02_output.txt new file mode 100644 index 0000000..971c600 --- /dev/null +++ b/d02_output.txt @@ -0,0 +1,2 @@ +1524750 +1592426537