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/Makefile | 1 + day01/solution.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ day01/tests/test1.input | 10 +++++++ day01/tests/test1.output | 2 ++ day01/tests/test2.input | 1 + day01/tests/test2.output | 2 ++ 6 files changed, 85 insertions(+) create mode 100644 day01/Makefile create mode 100644 day01/solution.cpp create mode 100644 day01/tests/test1.input create mode 100644 day01/tests/test1.output create mode 100644 day01/tests/test2.input create mode 100644 day01/tests/test2.output (limited to 'day01') diff --git a/day01/Makefile b/day01/Makefile new file mode 100644 index 0000000..2fa98c0 --- /dev/null +++ b/day01/Makefile @@ -0,0 +1 @@ +include ../aoc.mk 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; +} diff --git a/day01/tests/test1.input b/day01/tests/test1.input new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/day01/tests/test1.input @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 diff --git a/day01/tests/test1.output b/day01/tests/test1.output new file mode 100644 index 0000000..2559e5c --- /dev/null +++ b/day01/tests/test1.output @@ -0,0 +1,2 @@ +3 +6 diff --git a/day01/tests/test2.input b/day01/tests/test2.input new file mode 100644 index 0000000..af158bd --- /dev/null +++ b/day01/tests/test2.input @@ -0,0 +1 @@ +L150 \ No newline at end of file diff --git a/day01/tests/test2.output b/day01/tests/test2.output new file mode 100644 index 0000000..1191247 --- /dev/null +++ b/day01/tests/test2.output @@ -0,0 +1,2 @@ +1 +2 -- cgit v1.2.3