diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-12-04 02:16:03 +0200 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-12-04 02:16:03 +0200 |
| commit | c4424072e08da92a2dbe6e99f0135371368c7866 (patch) | |
| tree | f3c43c8a5e6c2cf87def71c754fc1076d565d6aa /day01 | |
| parent | add skel/ directory (diff) | |
| download | aoc25-c4424072e08da92a2dbe6e99f0135371368c7866.tar.gz aoc25-c4424072e08da92a2dbe6e99f0135371368c7866.zip | |
day01
Diffstat (limited to 'day01')
| -rw-r--r-- | day01/Makefile | 1 | ||||
| -rw-r--r-- | day01/solution.cpp | 69 | ||||
| -rw-r--r-- | day01/tests/test1.input | 10 | ||||
| -rw-r--r-- | day01/tests/test1.output | 2 | ||||
| -rw-r--r-- | day01/tests/test2.input | 1 | ||||
| -rw-r--r-- | day01/tests/test2.output | 2 |
6 files changed, 85 insertions, 0 deletions
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 @@ | |||
| 1 | #include <bits/stdc++.h> | ||
| 2 | |||
| 3 | namespace views = std::views; | ||
| 4 | namespace ranges = std::ranges; | ||
| 5 | |||
| 6 | auto parse_input(std::istream& is) { | ||
| 7 | const std::string input = { | ||
| 8 | std::istreambuf_iterator<char>(is), | ||
| 9 | std::istreambuf_iterator<char>() | ||
| 10 | }; | ||
| 11 | |||
| 12 | auto lines = views::split(input, '\n') | views::filter([](auto line) { return not line.empty(); }); | ||
| 13 | std::vector<int> document; | ||
| 14 | for (auto const line : lines) { | ||
| 15 | char direction = line[0]; | ||
| 16 | std::string out; | ||
| 17 | ranges::copy(line | views::drop(1), std::back_inserter(out)); | ||
| 18 | int length = std::stoi(out); | ||
| 19 | document.push_back(direction == 'R' ? length : -length); | ||
| 20 | } | ||
| 21 | return document; | ||
| 22 | } | ||
| 23 | |||
| 24 | int const DIAL_MAX = 100; | ||
| 25 | |||
| 26 | void part1(auto const& input) { | ||
| 27 | // Write first part solution here | ||
| 28 | int answer = 0; | ||
| 29 | int dial = 50; | ||
| 30 | |||
| 31 | for (auto const offset : input) { | ||
| 32 | dial += offset; | ||
| 33 | dial %= DIAL_MAX; | ||
| 34 | if (dial == 0) { | ||
| 35 | ++answer; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | std::println("{}", answer); | ||
| 40 | } | ||
| 41 | |||
| 42 | void part2(auto const& input) { | ||
| 43 | // Write second part solution here | ||
| 44 | int answer = 0; | ||
| 45 | int dial = 50; | ||
| 46 | int sign = 1; | ||
| 47 | |||
| 48 | for (auto const offset : input) { | ||
| 49 | int old_dial = dial; | ||
| 50 | dial += offset; | ||
| 51 | answer += std::abs(dial / DIAL_MAX); | ||
| 52 | if (old_dial * dial < 0 or dial == 0) { | ||
| 53 | ++answer; | ||
| 54 | } | ||
| 55 | dial %= DIAL_MAX; | ||
| 56 | old_dial = dial; | ||
| 57 | std::cerr << answer << ' ' << dial << '\n'; | ||
| 58 | } | ||
| 59 | std::println("{}", answer); | ||
| 60 | } | ||
| 61 | |||
| 62 | int main() { | ||
| 63 | auto const input = parse_input(std::cin); | ||
| 64 | |||
| 65 | part1(input); | ||
| 66 | part2(input); | ||
| 67 | |||
| 68 | return 0; | ||
| 69 | } | ||
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 @@ | |||
| 1 | L68 | ||
| 2 | L30 | ||
| 3 | R48 | ||
| 4 | L5 | ||
| 5 | R60 | ||
| 6 | L55 | ||
| 7 | L1 | ||
| 8 | L99 | ||
| 9 | R14 | ||
| 10 | 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 @@ | |||
| 1 | 3 | ||
| 2 | 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 | 1 | ||
| 2 | 2 | ||
