Skip to content

Commit

Permalink
Merge pull request #62 from EduardGomezEscandell/load-file
Browse files Browse the repository at this point in the history
Rename solvebase and implement load()
  • Loading branch information
EduardGomezEscandell authored Dec 1, 2023
2 parents 5e1a00f + a29568f commit d3a9a52
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 54 deletions.
14 changes: 14 additions & 0 deletions 2023/cmd/cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const int, std::unique_ptr<xmas::solution>>::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;
Expand Down
2 changes: 1 addition & 1 deletion 2023/cmd/cmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <map>
#include <memory>

#include "xmaslib/solvebase/solvebase.hpp"
#include "xmaslib/solution/solution.hpp"


int run(std::vector<std::string_view> &args);
Expand Down
18 changes: 9 additions & 9 deletions 2023/compile_commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
]
4 changes: 4 additions & 0 deletions 2023/data/01/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
4 changes: 4 additions & 0 deletions 2023/data/01/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
6 changes: 5 additions & 1 deletion 2023/solvelib/01/day01.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "day01.hpp"
#include <iostream>

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; }
3 changes: 2 additions & 1 deletion 2023/solvelib/01/day01.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "xmaslib/solvebase/solvebase.hpp"
#include "xmaslib/solution/solution.hpp"

class Day01 : public xmas::solution {
public:
Expand All @@ -9,4 +9,5 @@ class Day01 : public xmas::solution {
public:
std::int64_t part1() override;
std::int64_t part2() override;

};
2 changes: 1 addition & 1 deletion 2023/xmaslib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(xmaslib
solvebase/solvebase.hpp
solution/solution.hpp solution/solution.cpp
registry/registry.hpp registry/registry.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion 2023/xmaslib/registry/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <map>
#include <memory>

#include "../solvebase/solvebase.hpp"
#include "../solution/solution.hpp"

namespace xmas {

Expand Down
40 changes: 40 additions & 0 deletions 2023/xmaslib/solution/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "solution.hpp"

#include <format>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

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
27 changes: 27 additions & 0 deletions 2023/xmaslib/solution/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <cstdint>
#include <optional>
#include <stdexcept>

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<std::int64_t> p1;
std::optional<std::int64_t> p2;
};

} // namespace xmas
40 changes: 0 additions & 40 deletions 2023/xmaslib/solvebase/solvebase.hpp

This file was deleted.

0 comments on commit d3a9a52

Please sign in to comment.