From 5d5d24168c07ea4b080b6ef736911cab076f0133 Mon Sep 17 00:00:00 2001 From: Firewire Date: Mon, 1 Dec 2025 03:09:53 -0500 Subject: [PATCH] Cleanup - removing the overhead that my rust brain wants to add. --- src/day1/p1/main.cpp | 66 +++++++++------------------------------ src/day1/p2/main.cpp | 74 +++++++++++--------------------------------- 2 files changed, 32 insertions(+), 108 deletions(-) diff --git a/src/day1/p1/main.cpp b/src/day1/p1/main.cpp index c55bc23..56766cd 100644 --- a/src/day1/p1/main.cpp +++ b/src/day1/p1/main.cpp @@ -61,86 +61,48 @@ #include #include -#include #include -#include +#include -struct instruction { - // Left = false - // Right = true - bool dir; - int number; -}; - -auto get_dir(const std::string& line) -> std::expected { +bool get_dir(const std::string& line) { if (line.find('R') != std::string::npos) { return true; } else if (line.find('L') != std::string::npos) { return false; } else { - return std::unexpected("ERROR: Failed to read direction | " + line); + std::cerr << "ERROR: Failed to read direction | " << line << std::endl; + exit(1); } } -auto get_number(const std::string& line) -> std::expected { +int get_number(const std::string& line) { const std::string number = line.substr(1, line.size()); int ret; try { ret = std::stoi(number); } catch (const std::invalid_argument& e) { - return std::unexpected("ERROR: Number was an invalid argument | " + number + " | " + e.what()); + std::cerr << "ERROR: Number was an invalid argument | " << number << " | " << e.what() << std::endl; + exit(1); } catch (const std::out_of_range& e) { - return std::unexpected("ERROR: Number out of range | " + number + " | " + e.what()); + std::cerr << "ERROR: Number was out of range | " << number << " | " << e.what() << std::endl; + exit(1); } return ret; } -auto parse_line(const std::string& line) -> std::expected { - instruction ins{}; - if (const auto dir = get_dir(line); dir.has_value()) { - ins.dir = dir.value(); - } else { - return std::unexpected(dir.error()); - } - if (const auto number = get_number(line); number.has_value()) { - ins.number = number.value(); - } else { - return std::unexpected(number.error()); - } - return ins; -} - -auto parse(const std::string& filename) -> std::expected, std::string> { - std::ifstream stream(filename); - std::string line; - std::vector instructions; - - while (std::getline(stream, line)) { - if (const auto temp = parse_line(line); temp.has_value()) { - instructions.push_back(temp.value()); - } else { - return std::unexpected("Failed to parse file | " + filename + " | " + temp.error()); - } - } - return instructions; -} int rotate (const int state, const bool direction, const int number) { if (direction) { return (state + 100 + number) % 100; } else { return (state + 100 - number) % 100; } } int main() { - std::vector instructions; - if (auto temp = parse("input/day1/p1.txt")) { - instructions = temp.value(); - } else { - std::cerr << temp.error() << std::endl; - } - + std::ifstream stream("input/day1/p1.txt"); + std::string line; int state = 50; int password = 0; - for (auto [dir, number] : instructions) { - state = rotate(state, dir, number); + + while (std::getline(stream, line)) { + state = rotate(state, get_dir(line), get_number(line)); if (state == 0) { password++; } diff --git a/src/day1/p2/main.cpp b/src/day1/p2/main.cpp index 1a7576f..3509c6f 100644 --- a/src/day1/p2/main.cpp +++ b/src/day1/p2/main.cpp @@ -94,70 +94,36 @@ #include #include -#include #include -#include +#include -struct instruction { - // Left = false - // Right = true - bool dir; - int number; -}; -auto get_dir(const std::string& line) -> std::expected { +bool get_dir(const std::string& line) { if (line.find('R') != std::string::npos) { return true; - } else if (line.find('L') != std::string::npos) { - return false; - } else { - return std::unexpected("ERROR: Failed to read direction | " + line); } + if (line.find('L') != std::string::npos) { + return false; + } + std::cerr << "ERROR: Failed to read direction | " << line << std::endl; + exit(1); } -auto get_number(const std::string& line) -> std::expected { +int get_number(const std::string& line) { const std::string number = line.substr(1, line.size()); int ret; try { ret = std::stoi(number); } catch (const std::invalid_argument& e) { - return std::unexpected("ERROR: Number was an invalid argument | " + number + " | " + e.what()); + std::cerr << "ERROR: Number was an invalid argument | " << number << " | " << e.what() << std::endl; + exit(1); } catch (const std::out_of_range& e) { - return std::unexpected("ERROR: Number out of range | " + number + " | " + e.what()); + std::cerr << "ERROR: Number was out of range | " << number << " | " << e.what() << std::endl; + exit(1); } return ret; } -auto parse_line(const std::string& line) -> std::expected { - instruction ins{}; - if (const auto dir = get_dir(line); dir.has_value()) { - ins.dir = dir.value(); - } else { - return std::unexpected(dir.error()); - } - if (const auto number = get_number(line); number.has_value()) { - ins.number = number.value(); - } else { - return std::unexpected(number.error()); - } - return ins; -} - -auto parse(const std::string& filename) -> std::expected, std::string> { - std::ifstream stream(filename); - std::string line; - std::vector instructions; - - while (std::getline(stream, line)) { - if (const auto temp = parse_line(line); temp.has_value()) { - instructions.push_back(temp.value()); - } else { - return std::unexpected("Failed to parse file | " + filename + " | " + temp.error()); - } - } - return instructions; -} - std::tuple rotate (const int state, const bool direction, const int number, const int password) { int new_password = password + (number / 100); const int remainder = number % 100; @@ -174,17 +140,13 @@ auto parse(const std::string& filename) -> std::expected instructions; - if (auto temp = parse("input/day1/p1.txt")) { - instructions = temp.value(); - } else { - std::cerr << temp.error() << std::endl; - } - + std::ifstream stream("input/day1/p1.txt"); + std::string line; int state = 50; int password = 0; - for (auto [dir, number] : instructions) { - std::tuple temp = rotate(state, dir, number, password); + + while (std::getline(stream, line)) { + std::tuple temp = rotate(state, get_dir(line), get_number(line), password); state = std::get<0>(temp); password = std::get<1>(temp); } @@ -192,4 +154,4 @@ int main() { std::cout << "Password: " << password << std::endl; return 0; -} +} \ No newline at end of file