Compare commits

2 Commits

Author SHA1 Message Date
1079b2536c 2025 day06 p2 2025-12-14 22:04:04 +01:00
8062f9a537 2025 day 05 p2 2025-12-14 20:43:34 +01:00
4 changed files with 364 additions and 31 deletions

View File

@@ -5,6 +5,7 @@
#include <string_view>
#include <utility>
#include <vector>
#include <set>
#include "fmt/std.h"
@@ -34,11 +35,6 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
auto str = aoc::read_text("./input.txt").value_or("");
str = aoc::trim(str);
fmt::print("INPUT\n");
fmt::print("⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼\n");
fmt::print("{}\n", str);
fmt::print("{}\n", std::string(80, '-'));
auto id_spec = str | std::views::split("\r\n\r\n"sv) | std::views::transform([](auto const& str) {
return aoc::trim({std::begin(str), std::end(str)});
}) | std::ranges::to<std::vector>();
@@ -56,27 +52,94 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
return std::stod(std::string{std::begin(str), std::end(str)});
}) | std::ranges::to<std::vector>();
f64 sum = 0;
struct range {
f64 start;
f64 end;
};
for (usize i = 0; i < ids.size(); ++i) {
auto const id = ids[i];
bool is_valid = false;
for (usize j = 0; j < id_range.size(); ++j) {
auto const a = id_range[j][0];
auto const b = id_range[j][1];
// f64 sum = 0;
if (id >= a && id <= b) {
is_valid = true;
break;
}
// for (usize i = 0; i < ids.size(); ++i) {
// auto const id = ids[i];
// bool is_valid = false;
// for (usize j = 0; j < id_range.size(); ++j) {
// auto const a = id_range[j][0];
// auto const b = id_range[j][1];
//
// if (id >= a && id <= b) {
// is_valid = true;
// break;
// }
// }
//
// fmt::print("id: {} is {}\n", id, is_valid ? "valid" : "not valid");
//
// if (is_valid) ++sum;
// }
std::vector<range> range_vec{};
for (usize i = 0; i < id_range.size(); ++i) {
auto const a = id_range[i][0];
auto const b = id_range[i][1];
range_vec.push_back({a, b});
}
fmt::print("id: {} is {}\n", id, is_valid ? "valid" : "not valid");
std::ranges::sort(range_vec, [](auto a, auto b) {
return a.start < b.start;
});
fmt::print("----------\n");
if (is_valid) ++sum;
// for (auto const& r : range_vec) {
// fmt::print("{}-{}\n", r.start, r.end);
// }
//
// fmt::print("----------\n");
std::vector<range> ranges{};
for (usize i = 0; i < range_vec.size(); i += 1) {
auto const& r = range_vec[i];
if (ranges.empty()) {
ranges.push_back(r);
continue;
}
if (ranges.back().start <= r.start && ranges.back().end >= r.end) {
continue;
}
if (ranges.back().end >= r.start) {
ranges.back().end = r.end;
continue;
}
ranges.push_back(r);
}
fmt::print("\n");
f64 sum = 0.0;
for (auto const& r : ranges) {
// fmt::print("{}-{} ", r.start, r.end);
auto const d = (r.end - r.start) + 1;
fmt::print("{}\n", d);
sum += d;
}
fmt::print("res: {}\n", sum);
//
// std::vector<f64> ranges{};
// for (auto const& value : set) ranges.push_back(value);
// set = {};
// std::ranges::sort(ranges);
//
//
// // for (auto const& id : valid_ids) {
// // fmt::print("{} ", id);
// // }
// fmt::print("\n");
//
// fmt::print("res: {}\n", sum);
}
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {

View File

@@ -15,33 +15,31 @@ using namespace std::string_view_literals;
using namespace aoc::types;
// starts at 50
[[maybe_unused]]constexpr auto raw_str = R"(
123 328 51 64
[[maybe_unused]]constexpr auto raw_str = R"(123 328 51 64
45 64 387 23
6 98 215 314
* + * +
)";
* + * + )";
static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
// std::string str{raw_str};
auto str = aoc::read_text("./input.txt").value_or("");
str = aoc::trim(str);
// str = aoc::trim(str);
// fmt::print("INPUT\n");
// fmt::print("⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼⎽⎼⎻⎺⎻⎼\n");
// fmt::print("{}\n", str);
// fmt::print("{}\n", std::string(80, '-'));
auto vec = str | std::views::split("\n"sv) | std::views::transform([](auto&& line) {
auto vec = str | std::views::split("\r\n"sv) | std::views::transform([](auto&& line) {
return line | std::views::split(" "sv)
| std::views::filter([](auto&& tok) { return !std::ranges::empty(tok); })
| std::views::transform([](auto&& tok) {
return std::string{std::begin(tok), std::end(tok)};
return aoc::trim(std::string{std::begin(tok), std::end(tok)});
})
| std::ranges::to<std::vector>();
}) | std::ranges::to<std::vector>();
std::vector<std::vector<f64>> nums{};
std::vector<std::vector<std::string>> nums{};
std::vector<std::string> ops{};
for (usize i = 0; i < vec.size(); ++i) {
@@ -52,7 +50,7 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
ops.push_back(value);
continue;
}
nums[j].push_back(std::stod(vec[i][j]));
nums[j].push_back(vec[i][j]);
}
}
@@ -63,14 +61,62 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
auto const& op = ops[i];
f64 row_sum = op == "*" ? 1.0 : 0.0;
for (usize j = 0; j < nums[i].size(); ++j) {
if (op == "*") row_sum *= nums[i][j];
else if (op == "+") row_sum += nums[i][j];
if (op == "*") row_sum *= std::stod(nums[i][j]);
else if (op == "+") row_sum += std::stod(nums[i][j]);
}
// fmt::print("sum: {}\n", row_sum);
sum += row_sum;
}
fmt::print("res: {}\n", sum);
auto lines = str | std::views::split("\r\n"sv) | std::views::transform([](auto&& line) {
return std::string{std::begin(line), std::end(line)};
}) | std::ranges::to<std::vector>();
// for (usize i = 0; i < lines.size(); ++i) {
// fmt::print("| {} |\n", lines[i]);
// }
for (usize i = 0; i < lines.size(); ++i) {
fmt::print("size: {}\n", lines[i].size());
}
std::vector<std::vector<std::string>> mat{};
auto const w = lines[0].size();
fmt::print("w: {}\n", w);
for (isize j = w - 1; j >= 0; --j) {
std::string tmp{};
if (mat.empty()) mat.push_back({});
for (usize i = 0; i < lines.size() - 1; ++i) {
tmp += lines[i][j];
}
mat.back().push_back(aoc::trim(tmp));
auto const ch = lines[lines.size() - 2][j];
if (ch == '+' || ch == '*') {
mat.push_back({});
continue;
}
}
f64 new_sum = 0.0;
for (usize i = 0; i < mat.size() - 1; ++i) {
auto const& op = ops[mat.size() - 2 - i];
f64 row_sum = op == "*" ? 1.0 : 0.0;
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j].empty()) continue;
fmt::print("{},", aoc::trim(mat[i][j]));
if (op == "*") row_sum *= std::stod(aoc::trim(mat[i][j]));
else if (op == "+") row_sum += std::stod(aoc::trim(mat[i][j]));
}
new_sum += row_sum;
fmt::print("\n");
}
fmt::print("res: {}, {}\n", sum, new_sum);
}
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int {

112
2025/day08.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include <span>
#include <algorithm>
#include <ranges>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <tuple>
#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"(
162,817,812
57,618,57
906,360,560
592,479,940
352,342,300
466,668,158
542,29,236
431,825,988
739,650,466
52,470,668
216,146,977
819,987,18
117,168,530
805,96,715
346,949,466
970,615,88
941,993,340
862,61,35
984,92,344
425,690,689
)";
static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
// std::string str{raw_str};
auto str = aoc::read_text("./input.txt").value_or("");
str = aoc::trim(str);
auto mat = str | std::views::split("\n"sv) | std::views::transform([](auto line) {
return line | std::views::split(""sv)
| std::views::transform([](auto tok) {
return std::string{std::begin(tok), std::end(tok)};
})
| std::ranges::to<std::vector>();
}) | std::ranges::to<std::vector>();
struct beam {
usize row;
usize col;
};
std::vector<beam> beams{};
for (usize i = 0; i < mat.size(); ++i) {
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j] == "S") {
beams.push_back({i, j});
break;
}
auto const size = beams.size();
for (usize k = 0; k < size; ++k) {
if (beams[k].row == i - 1 && beams[k].col == j) {
if (mat[i][j] == ".") {
mat[i][j] = "|";
beams[k] = {i, j};
} else if (mat[i][j] == "^") {
beams[k] = {i, j - 1};
beams.push_back({i, j + 1});
mat[i][j - 1] = "|";
mat[i][j + 1] = "|";
}
}
}
}
}
f64 sum = 0.0;
for (usize i = 0; i < mat.size(); ++i) {
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j] == "^") {
if (mat[i - 1][j] == "|") sum += 1;
}
}
}
fmt::print("res: {}\n", sum);
// for (auto const& row : mat) {
// for (auto const& col : row) {
// fmt::print("{}", col);
// }
// fmt::print("\n");
// }
}
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;
}

112
2025/day09.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include <span>
#include <algorithm>
#include <ranges>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <tuple>
#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"(
162,817,812
57,618,57
906,360,560
592,479,940
352,342,300
466,668,158
542,29,236
431,825,988
739,650,466
52,470,668
216,146,977
819,987,18
117,168,530
805,96,715
346,949,466
970,615,88
941,993,340
862,61,35
984,92,344
425,690,689
)";
static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
// std::string str{raw_str};
auto str = aoc::read_text("./input.txt").value_or("");
str = aoc::trim(str);
auto mat = str | std::views::split("\n"sv) | std::views::transform([](auto line) {
return line | std::views::split(""sv)
| std::views::transform([](auto tok) {
return std::string{std::begin(tok), std::end(tok)};
})
| std::ranges::to<std::vector>();
}) | std::ranges::to<std::vector>();
struct beam {
usize row;
usize col;
};
std::vector<beam> beams{};
for (usize i = 0; i < mat.size(); ++i) {
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j] == "S") {
beams.push_back({i, j});
break;
}
auto const size = beams.size();
for (usize k = 0; k < size; ++k) {
if (beams[k].row == i - 1 && beams[k].col == j) {
if (mat[i][j] == ".") {
mat[i][j] = "|";
beams[k] = {i, j};
} else if (mat[i][j] == "^") {
beams[k] = {i, j - 1};
beams.push_back({i, j + 1});
mat[i][j - 1] = "|";
mat[i][j + 1] = "|";
}
}
}
}
}
f64 sum = 0.0;
for (usize i = 0; i < mat.size(); ++i) {
for (usize j = 0; j < mat[i].size(); ++j) {
if (mat[i][j] == "^") {
if (mat[i - 1][j] == "|") sum += 1;
}
}
}
fmt::print("res: {}\n", sum);
// for (auto const& row : mat) {
// for (auto const& col : row) {
// fmt::print("{}", col);
// }
// fmt::print("\n");
// }
}
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;
}