Skip to content

Instantly share code, notes, and snippets.

View HappyCerberus's full-sized avatar
📘
Moar books...

RNDr. Simon Toth HappyCerberus

📘
Moar books...
View GitHub Profile
uint64_t top_three(const std::vector<std::string>& data) {
auto by_elf = data |
// group by elf: range{range{string}}
std::views::lazy_split(std::string{}) |
// sum up the calories for each elf: range{uint64_t}
std::views::transform([](const auto& elf) -> uint64_t {
// std::string -> uint64_t
auto to_unsigned =
[](const auto& in) { return std::stoull(in); };
// make a view of uint64_t: range{string} -> range{uint64_t}
uint64_t max_calories(const std::vector<std::string>& data) {
auto by_elf = data |
// group by elf: range{range{string}}
std::views::lazy_split(std::string{}) |
// sum up the calories for each elf: range{uint64_t}
std::views::transform([](const auto& elf) -> uint64_t {
// std::string -> uint64_t
auto to_unsigned =
[](const auto& in) { return std::stoull(in); };
// make a view of uint64_t: range{string} -> range{uint64_t}
build --color=yes
build --cxxopt=-std=c++20
build --cxxopt=-pedantic
build --cxxopt=-Wall
build --cxxopt=-Wextra
build --cxxopt=-Werror
build:debug --cxxopt=-ggdb3
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_google_googletest",
strip_prefix = "googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5",
urls = ["https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip"],
)
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
cc_library(
name = "trivial",
srcs = ["trivial.cc"],
hdrs = ["trivial.h"],
)
cc_library(
name = "sliding",
uint32_t count_increasing_windows(std::istream &input) {
uint32_t e1 = 0, e2 = 0, e3 = 0, prev_sum = std::numeric_limits<uint32_t>::max(), drop = 2, cnt = 0;
for (uint32_t v : std::ranges::istream_view<uint32_t>(input)) {
e1 = std::exchange(e2, std::exchange(e3, v));
if (drop > 0) {
drop--;
continue;
}
if (std::exchange(prev_sum, e1 + e2 + e3) < e1 + e2 + e3)
cnt++;
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "This program expects one parameter that is the input file to read.\n";
return 1;
}
std::ifstream input(argv[1]);
if (!input.is_open()) {
std::cerr << "Failed to open file.\n";
return 1;
}
uint32_t count_increasing_windows(std::istream &input) {
auto sliding_window_view = std::ranges::istream_view<uint32_t>(input)
| std::ranges::views::transform([e1 = 0, e2 = 0, e3 = 0](uint32_t curr) mutable {
e1 = std::exchange(e2, std::exchange(e3, curr));
return e1 + e2 + e3;
});
return std::ranges::count_if(sliding_window_view,
[prev = std::numeric_limits<uint32_t>::max(), drop = 2]
(uint32_t curr) mutable {
TEST(SlidingTest, Simple) {
std::vector<std::pair<std::string, uint32_t>> inputs = {
{"", 0}, // not enough elements for a window
{"0 1", 0}, // not enough elements for a window
{"0 1 0", 0}, // a single window
{"0 0 0 0 0 0", 0}, // monotonic, no increase
{"0 1 2 3", 1}, // two windows, increasing
{"3 2 1 0", 0}, // two windows, decreasing
{"0 1 0 1 0 1", 2}, // 1, 2, 1, 2 - two increasing
{"199 200 208 210 200 207 240 269 260 263", 5}, // from aoc
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "This program expects one parameter that is the input file to read.\n";
return 1;
}
std::ifstream input(argv[1]);
if (!input.is_open()) {
std::cerr << "Failed to open file.\n";
return 1;
}