From c4424072e08da92a2dbe6e99f0135371368c7866 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Thu, 4 Dec 2025 02:16:03 +0200 Subject: day01 --- day01/solution.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 day01/solution.cpp (limited to 'day01/solution.cpp') diff --git a/day01/solution.cpp b/day01/solution.cpp new file mode 100644 index 0000000..9fb9ec1 --- /dev/null +++ b/day01/solution.cpp @@ -0,0 +1,69 @@ +#include + +namespace views = std::views; +namespace ranges = std::ranges; + +auto parse_input(std::istream& is) { + const std::string input = { + std::istreambuf_iterator(is), + std::istreambuf_iterator() + }; + + auto lines = views::split(input, '\n') | views::filter([](auto line) { return not line.empty(); }); + std::vector document; + for (auto const line : lines) { + char direction = line[0]; + std::string out; + ranges::copy(line | views::drop(1), std::back_inserter(out)); + int length = std::stoi(out); + document.push_back(direction == 'R' ? length : -length); + } + return document; +} + +int const DIAL_MAX = 100; + +void part1(auto const& input) { + // Write first part solution here + int answer = 0; + int dial = 50; + + for (auto const offset : input) { + dial += offset; + dial %= DIAL_MAX; + if (dial == 0) { + ++answer; + } + } + + std::println("{}", answer); +} + +void part2(auto const& input) { + // Write second part solution here + int answer = 0; + int dial = 50; + int sign = 1; + + for (auto const offset : input) { + int old_dial = dial; + dial += offset; + answer += std::abs(dial / DIAL_MAX); + if (old_dial * dial < 0 or dial == 0) { + ++answer; + } + dial %= DIAL_MAX; + old_dial = dial; + std::cerr << answer << ' ' << dial << '\n'; + } + std::println("{}", answer); +} + +int main() { + auto const input = parse_input(std::cin); + + part1(input); + part2(input); + + return 0; +} -- cgit v1.2.3