aoc24: day02b complete
This commit is contained in:
24
sol/24/CMakeLists.txt
Normal file
24
sol/24/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
set(HEADERS
|
||||
aoc.hpp
|
||||
)
|
||||
set(SOURCES
|
||||
"aoc.cpp"
|
||||
"day01.cpp"
|
||||
"day02.cpp"
|
||||
"day03.cpp"
|
||||
"day04.cpp"
|
||||
"day05.cpp"
|
||||
"day06.cpp"
|
||||
"day07.cpp"
|
||||
"day08.cpp"
|
||||
)
|
||||
add_library(aoc24 STATIC ${HEADERS} ${SOURCES})
|
||||
target_include_directories(aoc24
|
||||
PRIVATE
|
||||
${PROJECT_SOURCE_DIR}
|
||||
)
|
||||
target_compile_features(aoc24 PRIVATE cxx_std_23)
|
||||
target_compile_options(aoc24 PRIVATE ${BASE_OPTIONS})
|
||||
target_compile_definitions(aoc24 PRIVATE ${BASE_DEFINITIONS})
|
||||
target_link_libraries(aoc24 PRIVATE aoclib)
|
||||
source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES})
|
||||
34
sol/24/aoc.cpp
Normal file
34
sol/24/aoc.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <span>
|
||||
#include <expected>
|
||||
|
||||
#include "aoc.hpp"
|
||||
#include "ctre.hpp"
|
||||
|
||||
namespace aoc24 {
|
||||
auto entry([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
using namespace aoc::types;
|
||||
|
||||
u32 day = 1;
|
||||
if (args.size() > 1) {
|
||||
if (ctre::match<"^[0-9]+$">(args[1]))
|
||||
day = std::stoul(args[1]);
|
||||
else
|
||||
fmt::print(stderr, "arg: {} is not a number\n", args[1]);
|
||||
}
|
||||
|
||||
switch (day) {
|
||||
case 1: return day01(args);
|
||||
case 2: return day02(args);
|
||||
case 3: return day03(args);
|
||||
case 4: return day04(args);
|
||||
case 5: return day05(args);
|
||||
case 6: return day06(args);
|
||||
case 7: return day07(args);
|
||||
case 8: return day08(args);
|
||||
default:
|
||||
return aoc::make_error(fmt::format("day {}", day), std::errc::not_supported);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
40
sol/24/aoc.hpp
Normal file
40
sol/24/aoc.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef SOL_24_AOC_HPP
|
||||
#define SOL_24_AOC_HPP
|
||||
|
||||
#include <span>
|
||||
#include <expected>
|
||||
#include "aoc/utils.hpp"
|
||||
|
||||
#include "fmt/format.h"
|
||||
|
||||
namespace aoc24 {
|
||||
auto entry(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
|
||||
auto day01(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day02(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day03(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day04(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day05(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day06(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day07(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day08(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day09(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day10(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day11(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day12(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day13(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day14(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day15(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day16(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day17(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day18(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day19(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day20(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day21(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day22(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day23(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day24(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
auto day25(std::span<char const*> const& args) -> std::expected<void, aoc::error>;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
auto aoc24::day01([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
constexpr auto filename = "./dat/24/re/01.txt";
|
||||
std::ifstream strm{filename, std::ios::in};
|
||||
if (!strm.is_open()) {
|
||||
return aoc::make_error(
|
||||
fmt::format("Error opening file: {}\n", filename),
|
||||
std::errc::file_exists
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<std::uint32_t> a{};
|
||||
std::vector<std::uint32_t> b{};
|
||||
|
||||
std::string str{};
|
||||
while (strm) {
|
||||
auto const c = char(strm.peek());
|
||||
if (std::isdigit(c)) {
|
||||
str += char(strm.get());
|
||||
continue;
|
||||
}
|
||||
if (c == ' ') {
|
||||
if (!str.empty()) {
|
||||
a.emplace_back(std::stoi(str));
|
||||
str.clear();
|
||||
}
|
||||
}
|
||||
if (c == '\n') {
|
||||
if (!str.empty()) {
|
||||
b.emplace_back(std::stoi(str));
|
||||
str.clear();
|
||||
}
|
||||
}
|
||||
[[discard]]strm.get();
|
||||
}
|
||||
|
||||
fmt::print("a: {}, b: {}\n", a.size(), b.size());
|
||||
|
||||
std::sort(std::begin(a), std::end(a));
|
||||
std::sort(std::begin(b), std::end(b));
|
||||
|
||||
auto diff_view = std::views::zip(a, b) | std::views::transform([](auto const& p) {
|
||||
auto const [x, y] = p;
|
||||
return x > y ? x - y : y - x;
|
||||
});
|
||||
auto const sum = std::accumulate(std::begin(diff_view), std::end(diff_view), 0);
|
||||
|
||||
fmt::print("Part A: {}\n", std::abs(sum));
|
||||
|
||||
auto values = a | std::views::transform([&b](auto v) {
|
||||
return std::pair{v, b | std::views::filter([v](auto c) {
|
||||
return v == c; // Filter elements in `b` equal to `v`
|
||||
}) | std::ranges::to<std::vector>()};
|
||||
}) | std::ranges::to<std::vector>();
|
||||
|
||||
auto const meow = std::accumulate(std::begin(values), std::end(values), 0, [](auto const acc, auto const& pair) {
|
||||
return acc + std::int32_t(pair.first * pair.second.size());
|
||||
});
|
||||
|
||||
fmt::print("Part B: {}\n", meow);
|
||||
return {};
|
||||
}
|
||||
|
||||
104
sol/24/day02.cpp
104
sol/24/day02.cpp
@@ -0,0 +1,104 @@
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <numeric>
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
using namespace std::string_view_literals;
|
||||
using namespace aoc::types;
|
||||
|
||||
auto part_a(std::vector<std::vector<i32>> const& reports) -> void {
|
||||
auto const diffvec = reports | std::views::transform([](auto const& levels) {
|
||||
return levels | std::views::adjacent_transform<2>([](auto const& a, auto const& b) {
|
||||
return b - a;
|
||||
}) | std::ranges::to<std::vector>();
|
||||
}) | std::ranges::to<std::vector>();
|
||||
|
||||
auto answer = diffvec | std::views::filter([](auto const& levels) {
|
||||
auto const n = std::ranges::all_of(levels, [](auto v) { return v < 0; });
|
||||
auto const p = std::ranges::all_of(levels, [](auto v) { return v > 0; });
|
||||
auto const c = std::ranges::all_of(levels, [](auto v) { return std::abs(v) > 0 && std::abs(v) <= 3; });
|
||||
return (n || p) && c;
|
||||
});
|
||||
|
||||
auto const sum = std::ranges::distance(answer);
|
||||
fmt::print("Part A: {}\n", sum);
|
||||
|
||||
}
|
||||
|
||||
auto part_b(std::vector<std::vector<i32>> const& reports) -> void {
|
||||
auto const diffvec = reports | std::views::transform([](auto const& levels) {
|
||||
return levels | std::views::adjacent_transform<2>([](auto const& a, auto const& b) {
|
||||
return b - a;
|
||||
}) | std::ranges::to<std::vector>();
|
||||
}) | std::ranges::to<std::vector>();
|
||||
|
||||
auto const test_rule = [](std::vector<i32> const& levels) {
|
||||
auto const n = std::ranges::all_of(levels, [](auto v) { return v < 0; });
|
||||
auto const p = std::ranges::all_of(levels, [](auto v) { return v > 0; });
|
||||
auto const c = std::ranges::all_of(levels, [](auto v) { return std::abs(v) > 0 && std::abs(v) <= 3; });
|
||||
return (n || p) && c;
|
||||
};
|
||||
|
||||
auto nochange = diffvec | std::views::filter(test_rule);
|
||||
|
||||
auto unmet = std::views::zip(reports, diffvec) | std::views::filter([](auto const& vecs) {
|
||||
auto const& [levels, diffs] = vecs;
|
||||
auto const n = std::ranges::all_of(diffs, [](auto v) { return v < 0; });
|
||||
auto const p = std::ranges::all_of(diffs, [](auto v) { return v > 0; });
|
||||
auto const c = std::ranges::all_of(diffs, [](auto v) { return std::abs(v) > 0 && std::abs(v) <= 3; });
|
||||
return !(n || p) || !c;
|
||||
}) | std::views::transform([](auto const& vecs) {
|
||||
auto const& [levels, diffs] = vecs;
|
||||
return levels;
|
||||
});
|
||||
|
||||
auto refit = unmet | std::views::filter([&test_rule](auto const& levels) {
|
||||
std::vector<i32> vec{};
|
||||
for (std::size_t i = 0; i < levels.size(); ++i) {
|
||||
vec = levels;
|
||||
vec.erase(std::begin(vec) + std::ptrdiff_t(i));
|
||||
vec = vec
|
||||
| std::views::adjacent_transform<2>([](auto const& a, auto const& b) { return b - a; })
|
||||
| std::ranges::to<std::vector>();
|
||||
if (test_rule(vec)) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
auto const first = std::ranges::distance(nochange);
|
||||
auto const second = std::ranges::distance(refit);
|
||||
|
||||
fmt::print("Part B: {}\n", first + second);
|
||||
|
||||
}
|
||||
|
||||
auto aoc24::day02([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
auto txtres = aoc::read_text("./dat/24/re/02.txt");
|
||||
// auto txtres = aoc::read_text("./dat/24/ex/02.txt");
|
||||
if (!txtres) return std::unexpected(txtres.error());
|
||||
auto const txt = *txtres;
|
||||
|
||||
auto const reports = txt | std::views::split("\n"sv)
|
||||
| std::views::transform([](auto const& str) { return std::string_view(str); })
|
||||
| std::views::filter([](auto const& str) { return !str.empty(); })
|
||||
| std::views::transform([](auto const& str) {
|
||||
return str | std::views::split(" "sv)
|
||||
| std::views::filter([](auto str) { return !str.empty(); })
|
||||
| std::views::transform([](auto const& num_str) {
|
||||
return std::stoi(std::string(std::begin(num_str), std::end(num_str)));
|
||||
})
|
||||
| std::ranges::to<std::vector>();
|
||||
}) | std::ranges::to<std::vector>();
|
||||
|
||||
part_a(reports);
|
||||
part_b(reports);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
#include <string>
|
||||
#include <ranges>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
#include "aoc/aoc.hpp"
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
#include "ctre.hpp"
|
||||
|
||||
@@ -157,8 +159,8 @@ private:
|
||||
if (peek() == '\n') {
|
||||
peek_consume();
|
||||
m_line = m_line + 1;
|
||||
m_col = 1;
|
||||
str += "\\n";
|
||||
m_col = 1;
|
||||
auto const& type = token_type::invalid;
|
||||
return token(str, type, token_type_category(type), m_line, col);
|
||||
}
|
||||
@@ -387,7 +389,7 @@ private:
|
||||
};
|
||||
}
|
||||
|
||||
auto aoc::entry([[maybe_unused]]std::vector<std::string_view> const& args) -> void {
|
||||
auto aoc24::day03([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
npr::lexer lexer{"./dat/24/re/03.txt"};
|
||||
npr::parser parser{};
|
||||
auto const tokens = lexer.tokenize();
|
||||
@@ -438,5 +440,6 @@ auto aoc::entry([[maybe_unused]]std::vector<std::string_view> const& args) -> vo
|
||||
auto const sumb = std::accumulate(std::begin(opb), std::end(opb), 0);
|
||||
|
||||
fmt::print("Part B: {}\n", sumb);
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <vector>
|
||||
#include <span>
|
||||
|
||||
#include "aoc/aoc.hpp"
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
#include "ctre.hpp"
|
||||
|
||||
@@ -121,7 +122,7 @@ auto read_text_matnxn(std::string const& path) -> matnxn {
|
||||
}
|
||||
}
|
||||
|
||||
auto aoc::entry([[maybe_unused]]std::span<char const*> const& args) -> void {
|
||||
auto aoc24::day04([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
auto source = cms::read_text_matnxn("./dat/24/ex/04.txt");
|
||||
|
||||
cms::vecn hpat{'X', 'M', 'A', 'S'};
|
||||
@@ -155,5 +156,6 @@ auto aoc::entry([[maybe_unused]]std::span<char const*> const& args) -> void {
|
||||
// }
|
||||
// fmt::print("\n");
|
||||
// }
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
7
sol/24/day05.cpp
Normal file
7
sol/24/day05.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
auto aoc24::day05([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
return aoc::make_error("not implemented", std::errc::not_supported);
|
||||
}
|
||||
7
sol/24/day06.cpp
Normal file
7
sol/24/day06.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
auto aoc24::day06([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
return aoc::make_error("not implemented", std::errc::not_supported);
|
||||
}
|
||||
7
sol/24/day07.cpp
Normal file
7
sol/24/day07.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
auto aoc24::day07([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
return aoc::make_error("not implemented", std::errc::not_supported);
|
||||
}
|
||||
7
sol/24/day08.cpp
Normal file
7
sol/24/day08.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "aoc.hpp"
|
||||
#include "aoc/utils.hpp"
|
||||
#include "fmt/format.h"
|
||||
|
||||
auto aoc24::day08([[maybe_unused]]std::span<char const*> const& args) -> std::expected<void, aoc::error> {
|
||||
return aoc::make_error("not implemented", std::errc::not_supported);
|
||||
}
|
||||
Reference in New Issue
Block a user