From f0babe89b6157f93af1409589b73d48b2cff7bdc Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Fri, 11 Dec 2020 21:33:26 -0800 Subject: [PATCH] Completed day 12 --- 2020/days/day12.cpp | 104 ++++++ 2020/inputs/day12.input | 748 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 852 insertions(+) create mode 100644 2020/days/day12.cpp create mode 100644 2020/inputs/day12.input diff --git a/2020/days/day12.cpp b/2020/days/day12.cpp new file mode 100644 index 0000000..cc0ee01 --- /dev/null +++ b/2020/days/day12.cpp @@ -0,0 +1,104 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#include +#include +#include +#include + +#include "../misc/types.hpp" +#include "../misc/print.hpp" + +struct Instruction{ char op; i32 num; }; + +auto day12() -> void { + auto instructions = std::vector(); + { + auto line = std::string(); + auto file = std::ifstream("inputs/day12.input"); + while (getline(file, line)) { + instructions.push_back({ line[0], std::stoi(line.substr(1, line.size() - 1)) }); + if (line[0] == 'L' || line[0] == 'R') { + instructions[instructions.size() - 1].num /= 90; + } + } + } + + { + auto ship_x = i32(0); + auto ship_y = i32(0); + auto direction = i32(2); + for (const auto & instruction : instructions) { + switch (instruction.op) { + case 'N': ship_y += instruction.num; break; + case 'E': ship_x += instruction.num; break; + case 'S': ship_y -= instruction.num; break; + case 'W': ship_x -= instruction.num; break; + case 'R': direction = (direction - instruction.num + 4) % 4; break; + case 'L': direction = (direction + instruction.num ) % 4; break; + case 'F': + ship_x += ((direction - 1) % 2) * instruction.num; + ship_y += ((direction - 2) % 2) * instruction.num; + break; + } + } + print((std::abs(ship_x) + std::abs(ship_y))); + } + + { + auto ship_x = i32(0); + auto ship_y = i32(0); + auto waypoint_x = i32(10); + auto waypoint_y = i32( 1); + for (const auto & instruction : instructions) { + switch (instruction.op) { + case 'N': waypoint_y += instruction.num; break; + case 'E': waypoint_x += instruction.num; break; + case 'S': waypoint_y -= instruction.num; break; + case 'W': waypoint_x -= instruction.num; break; + case 'R': + for (auto i = i32(0); i < instruction.num; ++i) { + auto copy = waypoint_x; + waypoint_x = waypoint_y; + waypoint_y = -copy; + } + break; + case 'L': + for (auto i = i32(0); i < instruction.num; ++i) { + auto copy = waypoint_x; + waypoint_x = -waypoint_y; + waypoint_y = copy; + } + break; + case 'F': + ship_x += waypoint_x * instruction.num; + ship_y += waypoint_y * instruction.num; + break; + } + } + print((std::abs(ship_x) + std::abs(ship_y))); + } +} diff --git a/2020/inputs/day12.input b/2020/inputs/day12.input new file mode 100644 index 0000000..31d6bc8 --- /dev/null +++ b/2020/inputs/day12.input @@ -0,0 +1,748 @@ +W5 +R90 +W3 +F98 +F87 +R90 +F57 +R180 +F3 +L90 +F60 +N3 +F94 +N3 +E2 +S2 +W2 +L90 +F26 +R90 +W1 +F58 +S5 +F22 +N3 +F95 +N1 +W1 +F11 +R180 +S3 +R270 +N1 +N1 +E4 +S3 +F81 +W2 +S2 +L90 +S4 +R90 +S1 +E1 +L90 +S4 +E2 +F72 +S1 +W1 +F73 +W4 +L180 +S1 +W5 +S5 +R90 +E3 +N3 +F28 +N1 +F84 +R90 +E5 +F13 +W5 +L90 +F11 +E4 +F63 +S1 +S2 +L90 +N3 +S5 +F20 +W1 +S4 +W1 +S2 +F67 +N1 +R180 +F11 +E1 +R90 +S2 +R90 +F38 +S5 +F27 +S5 +W3 +S3 +L90 +N2 +W2 +S1 +N4 +R90 +E1 +F16 +L90 +E4 +N1 +L90 +F89 +E5 +F90 +E1 +L180 +N1 +E3 +S2 +F58 +S3 +F53 +R90 +F100 +W1 +F53 +W5 +L90 +W3 +N4 +F54 +R180 +S2 +E2 +F63 +L90 +S4 +F40 +F90 +N3 +F83 +E5 +F35 +W4 +W5 +S3 +E2 +S2 +S1 +F12 +L90 +S1 +F41 +R90 +S3 +R90 +F66 +S2 +F34 +N5 +R90 +E5 +R90 +F25 +N5 +R90 +W1 +S2 +S2 +R90 +E3 +R90 +F95 +N3 +W2 +S4 +R90 +E3 +L90 +E3 +R90 +N2 +F84 +L90 +N2 +R90 +S1 +L90 +F93 +L90 +F60 +S4 +F85 +S2 +F84 +R180 +W5 +N4 +W5 +R270 +S2 +E2 +L180 +W4 +R180 +W5 +F56 +E1 +F45 +W4 +R90 +L180 +S1 +W4 +S5 +F87 +R180 +S2 +F76 +R90 +F76 +S1 +E4 +F6 +S1 +E2 +F47 +S3 +W2 +F16 +F75 +E3 +F75 +E4 +R90 +N5 +W3 +F1 +S1 +F8 +E2 +F64 +R90 +W4 +S5 +R90 +N5 +R90 +E2 +N1 +E1 +L180 +F31 +L180 +E5 +L90 +N3 +R90 +F77 +E3 +F65 +E4 +R90 +W1 +N3 +E3 +F4 +R90 +E3 +N4 +F28 +R180 +N2 +L90 +S2 +L90 +N1 +W1 +L180 +E4 +F51 +W4 +F9 +S4 +R90 +W5 +S4 +R90 +E3 +W2 +F44 +R90 +E1 +L180 +S4 +F93 +S2 +F58 +R90 +F80 +L90 +E2 +F20 +R90 +F19 +S4 +F22 +W1 +S2 +F62 +N2 +E5 +F21 +L90 +F16 +W2 +F58 +E2 +F54 +N1 +F83 +N3 +E2 +F62 +S3 +L90 +E3 +L270 +F29 +N5 +L90 +S2 +F19 +E5 +R180 +F87 +R180 +S2 +F22 +W3 +S5 +F35 +E3 +N5 +R180 +E2 +R270 +N3 +F5 +L90 +W3 +S1 +L90 +S2 +R270 +N5 +L180 +F79 +N3 +F82 +N3 +F73 +N4 +F57 +L90 +W3 +F26 +N4 +E5 +N4 +F48 +R90 +F62 +R90 +F36 +E1 +F76 +R90 +N3 +F83 +E5 +L90 +S1 +F1 +E1 +L90 +F67 +W3 +L90 +F42 +E4 +S2 +L180 +F89 +N1 +E4 +S4 +W1 +S2 +L90 +F91 +R90 +F78 +N5 +F29 +W2 +R90 +W1 +R90 +E2 +F40 +E2 +F76 +R90 +E4 +L90 +W3 +S1 +W3 +N4 +F81 +W4 +F22 +N1 +W1 +F47 +E3 +R90 +N5 +W4 +L90 +F44 +L90 +F58 +S1 +R90 +E3 +F91 +N1 +W4 +N5 +L90 +F60 +F8 +S4 +F17 +E5 +N2 +L90 +F37 +L180 +W4 +L180 +W4 +F93 +S5 +F71 +R90 +N5 +E3 +F20 +R90 +N5 +E5 +R90 +S2 +R180 +S5 +L90 +F26 +E4 +F49 +E5 +S4 +E4 +N1 +L180 +F33 +E1 +L90 +S4 +E5 +N5 +L180 +F60 +S1 +F53 +W1 +F34 +E2 +N3 +E2 +S5 +F61 +F32 +F18 +L90 +W4 +N4 +R90 +E4 +L90 +F26 +S3 +W4 +S5 +E5 +S2 +R90 +S5 +R90 +E2 +R90 +N2 +S3 +L90 +S3 +S2 +L90 +E5 +F31 +R180 +E2 +F42 +R90 +W4 +L90 +E5 +S3 +F79 +R90 +F76 +W1 +F9 +N1 +F91 +R90 +N3 +F32 +L180 +N4 +W2 +F18 +N5 +L90 +S4 +L180 +F22 +E4 +R180 +S2 +L90 +W5 +F31 +L180 +E1 +F15 +W1 +S5 +E4 +F56 +L90 +F7 +S3 +E4 +F13 +R90 +S4 +F78 +R180 +E3 +N1 +R90 +W4 +F29 +S4 +L90 +S4 +W4 +L180 +F6 +F84 +E4 +L90 +F29 +E2 +F75 +E3 +N4 +F33 +L270 +S4 +F62 +E2 +N4 +F50 +E1 +E2 +L270 +F100 +R180 +S2 +F89 +N2 +W3 +F5 +E5 +N2 +F75 +S3 +F2 +S4 +W3 +F33 +S5 +R90 +E5 +F20 +S1 +L90 +S4 +F10 +W5 +S2 +L90 +E3 +E1 +S3 +F41 +E5 +L90 +F28 +S2 +F40 +S2 +S4 +N1 +F15 +W5 +W2 +R270 +E5 +R90 +E4 +F50 +R90 +S4 +E1 +N4 +F73 +L90 +W2 +L270 +E4 +S1 +F30 +S3 +W4 +L180 +W4 +F6 +R90 +F43 +N5 +W3 +W3 +R90 +S2 +R90 +F50 +W2 +S4 +L90 +L180 +W1 +L270 +E3 +N2 +E2 +F48 +W1 +L90 +N4 +L90 +E3 +F86 +L180 +F10 +S1 +F36 +S4 +F33 +N5 +L90 +F14 +S2 +E4 +N4 +R90 +S2 +W2 +N2 +L180 +N4 +R90 +L90 +E5 +S1 +F82 +W1 +S1 +F19 +E3 +F19 +S2 +W1 +F18 +L90 +F43 +W1 +N2 +E3 +L90 +S1 +F75 +L90 +E4 +F80 +N5 +F15 +F53 +N4 +E5 +L90 +E1 +W5 +R270 +F17 +F63 +W2 +F20 +E1 +S3 +F19 +E1 +F99 +S1 +W1 +F23 +L180 +S3 +W4 +F76