Compare commits
3 Commits
60e78353d7
...
ed04586b1d
| Author | SHA1 | Date | |
|---|---|---|---|
| ed04586b1d | |||
| 11ed18a199 | |||
| 65f6175bc9 |
@@ -32,4 +32,7 @@ add_day_executables(
|
|||||||
"day02.cpp"
|
"day02.cpp"
|
||||||
"day03.cpp"
|
"day03.cpp"
|
||||||
"day04.cpp"
|
"day04.cpp"
|
||||||
|
"day05.cpp"
|
||||||
|
"day06.cpp"
|
||||||
|
"day07.cpp"
|
||||||
)
|
)
|
||||||
|
|||||||
90
2025/day05.cpp
Normal file
90
2025/day05.cpp
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#include <span>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <ranges>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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"(
|
||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
|
)";
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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>();
|
||||||
|
|
||||||
|
if (id_spec.size() < 2) throw std::runtime_error("ID specification is not valid");
|
||||||
|
|
||||||
|
auto id_range = id_spec[0] | std::views::split("\n"sv) | std::views::transform([](auto const& str) {
|
||||||
|
return std::ranges::fold_left(str | std::views::split("-"sv), std::vector<f64>{}, [](auto a, auto const& v) {
|
||||||
|
a.push_back(std::stod(std::string({std::begin(v), std::end(v)})));
|
||||||
|
return a;
|
||||||
|
});
|
||||||
|
}) | std::ranges::to<std::vector>();
|
||||||
|
|
||||||
|
auto ids = id_spec[1] | std::views::split("\n"sv) | std::views::transform([](auto const& str) {
|
||||||
|
return std::stod(std::string{std::begin(str), std::end(str)});
|
||||||
|
}) | std::ranges::to<std::vector>();
|
||||||
|
|
||||||
|
f64 sum = 0;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt::print("res: {}\n", sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
84
2025/day06.cpp
Normal file
84
2025/day06.cpp
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include <span>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <ranges>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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"(
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
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)};
|
||||||
|
})
|
||||||
|
| std::ranges::to<std::vector>();
|
||||||
|
}) | std::ranges::to<std::vector>();
|
||||||
|
|
||||||
|
std::vector<std::vector<f64>> nums{};
|
||||||
|
std::vector<std::string> ops{};
|
||||||
|
|
||||||
|
for (usize i = 0; i < vec.size(); ++i) {
|
||||||
|
for (usize j = 0; j < vec[i].size(); ++j) {
|
||||||
|
auto const& value = vec[i][j];
|
||||||
|
if (nums.size() <= j) nums.push_back({});
|
||||||
|
if (value == "*" || value == "+") {
|
||||||
|
ops.push_back(value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
nums[j].push_back(std::stod(vec[i][j]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt::print("{} {}\n", nums.size(), ops.size());
|
||||||
|
|
||||||
|
f64 sum = 0.0;
|
||||||
|
for (usize i = 0; i < nums.size(); ++i) {
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
// fmt::print("sum: {}\n", row_sum);
|
||||||
|
sum += row_sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt::print("res: {}\n", sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
108
2025/day07.cpp
Normal file
108
2025/day07.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
#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"(
|
||||||
|
.......S.......
|
||||||
|
...............
|
||||||
|
.......^.......
|
||||||
|
...............
|
||||||
|
......^.^......
|
||||||
|
...............
|
||||||
|
.....^.^.^.....
|
||||||
|
...............
|
||||||
|
....^.^...^....
|
||||||
|
...............
|
||||||
|
...^.^...^.^...
|
||||||
|
...............
|
||||||
|
..^...^.....^..
|
||||||
|
...............
|
||||||
|
.^.^.^.^.^...^.
|
||||||
|
...............
|
||||||
|
)";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user