From a29568f0302325153f7be4758ae08f938e38732d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Fri, 1 Dec 2023 09:49:07 +0100 Subject: [PATCH] Rename solvebase and implement load() --- 2023/cmd/cmd.cpp | 14 ++++++++++ 2023/cmd/cmd.hpp | 2 +- 2023/compile_commands.json | 18 ++++++------- 2023/data/01/example.txt | 4 +++ 2023/data/01/input.txt | 4 +++ 2023/solvelib/01/day01.cpp | 6 ++++- 2023/solvelib/01/day01.hpp | 3 ++- 2023/xmaslib/CMakeLists.txt | 2 +- 2023/xmaslib/registry/registry.hpp | 2 +- 2023/xmaslib/solution/solution.cpp | 40 ++++++++++++++++++++++++++++ 2023/xmaslib/solution/solution.hpp | 27 +++++++++++++++++++ 2023/xmaslib/solvebase/solvebase.hpp | 40 ---------------------------- 12 files changed, 108 insertions(+), 54 deletions(-) create mode 100644 2023/data/01/example.txt create mode 100644 2023/data/01/input.txt create mode 100644 2023/xmaslib/solution/solution.cpp create mode 100644 2023/xmaslib/solution/solution.hpp delete mode 100644 2023/xmaslib/solvebase/solvebase.hpp diff --git a/2023/cmd/cmd.cpp b/2023/cmd/cmd.cpp index 0b84374..adbb6e9 100644 --- a/2023/cmd/cmd.cpp +++ b/2023/cmd/cmd.cpp @@ -108,9 +108,23 @@ void usage(std::ostream &s) { << " Runs all solutions\n\n"; } +std::string datafile(int day) { + return std::format("./data/{:02d}/input.txt", day); +} + bool solve_day( std::map>::value_type const &solution) { + + try { + solution.second.get()->load(datafile(solution.second->day())); + } catch (std::runtime_error &e) { + log_error("day {} could not load: {}\n", solution.second.get()->day(), + e.what()); + return false; + } + + try { solution.second.get()->run(); return true; diff --git a/2023/cmd/cmd.hpp b/2023/cmd/cmd.hpp index b7b5007..5ebc3b1 100644 --- a/2023/cmd/cmd.hpp +++ b/2023/cmd/cmd.hpp @@ -5,7 +5,7 @@ #include #include -#include "xmaslib/solvebase/solvebase.hpp" +#include "xmaslib/solution/solution.hpp" int run(std::vector &args); diff --git a/2023/compile_commands.json b/2023/compile_commands.json index d603855..6bf8fa7 100644 --- a/2023/compile_commands.json +++ b/2023/compile_commands.json @@ -7,10 +7,16 @@ }, { "directory": "/home/edu/AdventOfCode/2023/build/Release/solvelib", - "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/xmaslib/.. -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/solvelib.dir/01/day01.cpp.o -c /home/edu/AdventOfCode/2023/solvelib/01/day01.cpp", + "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/xmaslib/.. -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/solvelib.dir/01/day01.cpp.o -c /home/edu/AdventOfCode/2023/solvelib/01/day01.cpp", "file": "/home/edu/AdventOfCode/2023/solvelib/01/day01.cpp", "output": "solvelib/CMakeFiles/solvelib.dir/01/day01.cpp.o" }, +{ + "directory": "/home/edu/AdventOfCode/2023/build/Release/xmaslib", + "command": "/usr/bin/c++ -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/xmaslib.dir/solution/solution.cpp.o -c /home/edu/AdventOfCode/2023/xmaslib/solution/solution.cpp", + "file": "/home/edu/AdventOfCode/2023/xmaslib/solution/solution.cpp", + "output": "xmaslib/CMakeFiles/xmaslib.dir/solution/solution.cpp.o" +}, { "directory": "/home/edu/AdventOfCode/2023/build/Release/xmaslib", "command": "/usr/bin/c++ -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/xmaslib.dir/registry/registry.cpp.o -c /home/edu/AdventOfCode/2023/xmaslib/registry/registry.cpp", @@ -19,20 +25,14 @@ }, { "directory": "/home/edu/AdventOfCode/2023/build/Release/cmd", - "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/aoc2023.dir/main.cpp.o -c /home/edu/AdventOfCode/2023/cmd/main.cpp", + "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/aoc2023.dir/main.cpp.o -c /home/edu/AdventOfCode/2023/cmd/main.cpp", "file": "/home/edu/AdventOfCode/2023/cmd/main.cpp", "output": "cmd/CMakeFiles/aoc2023.dir/main.cpp.o" }, { "directory": "/home/edu/AdventOfCode/2023/build/Release/cmd", - "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/aoc2023.dir/cmd.cpp.o -c /home/edu/AdventOfCode/2023/cmd/cmd.cpp", + "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/aoc2023.dir/cmd.cpp.o -c /home/edu/AdventOfCode/2023/cmd/cmd.cpp", "file": "/home/edu/AdventOfCode/2023/cmd/cmd.cpp", "output": "cmd/CMakeFiles/aoc2023.dir/cmd.cpp.o" -}, -{ - "directory": "/home/edu/AdventOfCode/2023/build/Release/test", - "command": "/usr/bin/c++ -I/home/edu/AdventOfCode/2023/build/Release/_deps/doctest-src -I/home/edu/AdventOfCode/2023/solvelib/.. -I/home/edu/AdventOfCode/2023/xmaslib/.. -Wall -Werror -Wextra -Wpedantic -Wconversion -O3 -DNDEBUG -std=gnu++20 -o CMakeFiles/test.dir/test.cpp.o -c /home/edu/AdventOfCode/2023/test/test.cpp", - "file": "/home/edu/AdventOfCode/2023/test/test.cpp", - "output": "test/CMakeFiles/test.dir/test.cpp.o" } ] \ No newline at end of file diff --git a/2023/data/01/example.txt b/2023/data/01/example.txt new file mode 100644 index 0000000..1ba8437 --- /dev/null +++ b/2023/data/01/example.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet \ No newline at end of file diff --git a/2023/data/01/input.txt b/2023/data/01/input.txt new file mode 100644 index 0000000..1ba8437 --- /dev/null +++ b/2023/data/01/input.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet \ No newline at end of file diff --git a/2023/solvelib/01/day01.cpp b/2023/solvelib/01/day01.cpp index 206aa81..606c1aa 100644 --- a/2023/solvelib/01/day01.cpp +++ b/2023/solvelib/01/day01.cpp @@ -1,5 +1,9 @@ #include "day01.hpp" +#include -std::int64_t Day01::part1() { return 1; } +std::int64_t Day01::part1() { + std::cout << this->input << std::endl; + return 1; +} std::int64_t Day01::part2() { return 2; } diff --git a/2023/solvelib/01/day01.hpp b/2023/solvelib/01/day01.hpp index b70ba2a..44e215b 100644 --- a/2023/solvelib/01/day01.hpp +++ b/2023/solvelib/01/day01.hpp @@ -1,6 +1,6 @@ #pragma once -#include "xmaslib/solvebase/solvebase.hpp" +#include "xmaslib/solution/solution.hpp" class Day01 : public xmas::solution { public: @@ -9,4 +9,5 @@ class Day01 : public xmas::solution { public: std::int64_t part1() override; std::int64_t part2() override; + }; diff --git a/2023/xmaslib/CMakeLists.txt b/2023/xmaslib/CMakeLists.txt index 516c7f2..12a9b87 100644 --- a/2023/xmaslib/CMakeLists.txt +++ b/2023/xmaslib/CMakeLists.txt @@ -1,5 +1,5 @@ add_library(xmaslib - solvebase/solvebase.hpp + solution/solution.hpp solution/solution.cpp registry/registry.hpp registry/registry.cpp ) diff --git a/2023/xmaslib/registry/registry.hpp b/2023/xmaslib/registry/registry.hpp index f97d582..3a4f0b9 100644 --- a/2023/xmaslib/registry/registry.hpp +++ b/2023/xmaslib/registry/registry.hpp @@ -5,7 +5,7 @@ #include #include -#include "../solvebase/solvebase.hpp" +#include "../solution/solution.hpp" namespace xmas { diff --git a/2023/xmaslib/solution/solution.cpp b/2023/xmaslib/solution/solution.cpp new file mode 100644 index 0000000..45f085a --- /dev/null +++ b/2023/xmaslib/solution/solution.cpp @@ -0,0 +1,40 @@ +#include "solution.hpp" + +#include +#include +#include +#include +#include + +namespace xmas { + +void solution::run() { + std::cout << "Day 1" << std::endl; + try { + const auto p1 = this->part1(); + std::cout << " Result 1: " << p1 << '\n'; + } catch (std::runtime_error &err) { + std::cerr << "Part 1 failed with message: " << err.what() << std::endl; + } + + try { + const auto p2 = this->part2(); + std::cout << " Result 2: " << p2 << '\n'; + } catch (std::runtime_error &err) { + std::cerr << "Part 2 failed with message: " << err.what() << std::endl; + } +} + +void solution::load(std::string_view path) { + std::ifstream f(path.data()); + std::stringstream buff; + buff << f.rdbuf(); + input = std::move(buff).str(); + + if (input.size() == 0) { + throw std::runtime_error( + std::format("did not load any data from {}", path)); + } +} + +} // namespace xmas \ No newline at end of file diff --git a/2023/xmaslib/solution/solution.hpp b/2023/xmaslib/solution/solution.hpp new file mode 100644 index 0000000..fdc7da6 --- /dev/null +++ b/2023/xmaslib/solution/solution.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +namespace xmas { + +class solution { +public: + virtual int day() = 0; + + virtual void run(); + virtual void load(std::string_view path); + +protected: + virtual std::int64_t part1() { throw std::runtime_error("not implemented"); } + virtual std::int64_t part2() { throw std::runtime_error("not implemented"); } + + std::string input; + +private: + std::optional p1; + std::optional p2; +}; + +} // namespace xmas \ No newline at end of file diff --git a/2023/xmaslib/solvebase/solvebase.hpp b/2023/xmaslib/solvebase/solvebase.hpp deleted file mode 100644 index ea5873b..0000000 --- a/2023/xmaslib/solvebase/solvebase.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace xmas { - -class solution { -public: - virtual int day() = 0; - - virtual void run() { - std::cout << "Day 1" << std::endl; - try { - const auto p1 =this->part1(); - std::cout << " Result 1: " << p1 << '\n'; - } catch (std::runtime_error &err) { - std::cerr << "Part 1 failed with message: " << err.what() << std::endl; - } - - try { - const auto p2 = this->part2(); - std::cout << " Result 2: " << p2 << '\n'; - } catch (std::runtime_error &err) { - std::cerr << "Part 2 failed with message: " << err.what() << std::endl; - } - } - -protected: - virtual std::int64_t part1() { throw std::runtime_error("not implemented"); } - virtual std::int64_t part2() { throw std::runtime_error("not implemented"); } - -private: - std::optional p1; - std::optional p2; -}; - -} // namespace xmas \ No newline at end of file