From abecf6cfb7bba4675cd6b72ef6b5cc1080a2e3f9 Mon Sep 17 00:00:00 2001 From: Pratchaya Khansomboon Date: Tue, 9 Dec 2025 16:12:19 +0100 Subject: [PATCH] Add function in cmake to create targets --- 2025/CMakeLists.txt | 42 +++++++++++++++++++++++++++-------------- 2025/day02.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ aoc/utils.hpp | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 2025/day02.cpp diff --git a/2025/CMakeLists.txt b/2025/CMakeLists.txt index 7bd3961..d16a275 100644 --- a/2025/CMakeLists.txt +++ b/2025/CMakeLists.txt @@ -2,18 +2,32 @@ find_package(fmt REQUIRED) find_package(utf8cpp REQUIRED) find_package(ctre REQUIRED) -set(HEADERS "") -set(SOURCES "day01.cpp") -add_executable(day01 ${HEADERS} ${SOURCES}) -target_include_directories(day01 PUBLIC ${PROJECT_SOURCE_DIR}) -target_compile_features(day01 PRIVATE cxx_std_23) -target_link_libraries(day01 - PRIVATE - ${PLATFORM_LINK_LIBRARIES} - fmt - utf8cpp - ctre +function(add_day_executables) + foreach(src IN LISTS ARGN) + get_filename_component(name "${src}" NAME_WE) + + add_executable(${name} "${src}") + + target_include_directories(${name} PUBLIC "${PROJECT_SOURCE_DIR}") + target_compile_features(${name} PRIVATE cxx_std_23) + + target_link_libraries(${name} + PRIVATE + ${PLATFORM_LINK_LIBRARIES} + fmt + utf8cpp + ctre + ) + + target_compile_definitions(${name} PRIVATE ${PLATFORM_DEFINITIONS}) + target_compile_options(${name} PRIVATE ${BASE_OPTIONS}) + + source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES "${src}") + endforeach() +endfunction() + + +add_day_executables( + "day01.cpp" + "day02.cpp" ) -target_compile_definitions(day01 PRIVATE ${PLATFORM_DEFINITIONS}) -target_compile_options(day01 PRIVATE ${BASE_OPTIONS}) -source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES}) diff --git a/2025/day02.cpp b/2025/day02.cpp new file mode 100644 index 0000000..5fcf0ab --- /dev/null +++ b/2025/day02.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "fmt/std.h" + +#include "aoc/types.hpp" +#include "aoc/utils.hpp" + +using namespace std::string_view_literals; +using namespace aoc::types; + +// starts at 50 +[[maybe_unused]]constexpr auto raw_str = R"( +11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124 +)"; + +static auto entry([[maybe_unused]]std::span const& args) -> void { + std::string str{raw_str}; + // auto str = aoc::read_text("./input.txt").value_or(""); + str = aoc::trim(str); + + auto ids_str = str | std::views::split(","sv) | std::views::transform([](auto const& str) { + return aoc::trim({std::begin(str), std::end(str)}); + }) | std::ranges::to(); + + for (auto const& id : ids_str) { + std::print("{}\n", id); + } +} + +auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int { + try { + entry({argv, std::next(argv, argc)}); + } catch (std::exception const& e) { + fmt::print(stderr, "{}\n", e.what()); + return 1; + } + return 0; +} diff --git a/aoc/utils.hpp b/aoc/utils.hpp index 6e5582d..ebce5ee 100644 --- a/aoc/utils.hpp +++ b/aoc/utils.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "aoc/types.hpp" @@ -66,6 +67,41 @@ inline constexpr auto split = std::views::split; [[maybe_unused]]constexpr auto filter_non_empty = std::views::filter([](auto const& str) { return !str.empty(); }); + +template +struct pipe { + F f; +}; + +template +pipe(F) -> pipe; + +template +auto operator|(std::string s, pipe p) -> decltype(auto) { + return p.f(std::move(s)); +} + +inline auto ltrim(std::string s) -> std::string { + s.erase( + std::begin(s), + std::find_if(s.begin(), s.end(), [](char ch) { + return !std::isspace(static_cast(ch)); + })); + return s; +} + +inline auto rtrim(std::string s) -> std::string { + s.erase( + std::find_if(s.rbegin(), s.rend(), [](char ch) { + return !std::isspace(static_cast(ch)); + }).base(), + s.end()); + return s; +} + +inline auto trim(std::string s) -> std::string { + return ltrim(rtrim(std::move(s))); +} } #endif // !AOC_UTILS_HPP