Skip to content

Instantly share code, notes, and snippets.

Avatar

RNDr. Simon Toth HappyCerberus

View GitHub Profile
View .bazelrc
build --color=yes
build --cxxopt=-std=c++20
build --cxxopt=-pedantic
build --cxxopt=-Wall
build --cxxopt=-Wextra
build --cxxopt=-Werror
build:debug --cxxopt=-ggdb3
View WORKSPACE
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"],
)
View BUILD
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",
View aoc-2021-solution-day1-part2-forloop.cc
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++;
View aoc-2021-solution-day1-part2-main.cc
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;
}
View aoc-2021-solution-day1-part1.cc
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 {
View test-aoc2021-day1-part2.cc
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
View aoc-2021-solution-day1-part1-main.cc
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;
}
View aoc-2021-solution-day1-part1.cc
#include <algorithm>
#include <istream>
#include <limits>
#include <ranges>
#include <utility>
uint32_t count_increasing(std::istream &input) {
return std::ranges::count_if(
std::ranges::istream_view<uint32_t>(input),
[prev = std::numeric_limits<uint32_t>::max()](uint32_t curr) mutable
View test-aoc2021-day1-part1.cc
#include "trivial.h"
#include <gtest/gtest.h>
#include <sstream>
TEST(TrivialTest, Simple) {
std::vector<std::pair<std::string, uint32_t>> inputs = {
{"", 0}, // empty input 0 increasing
{"0", 0}, // single element 0 increasing
{"0 1", 1}, // two elements, with increase