Line data Source code
1 : #include "PuzzleImpl.h"
2 :
3 : #include "LinewiseInput.h"
4 : #include "Parsing.h"
5 :
6 : #include <string_view>
7 : #include <vector>
8 :
9 : namespace {
10 :
11 : constexpr int dialSize = 100;
12 : constexpr int startPos = 50;
13 :
14 : std::vector<int> parse(std::string_view const input) {
15 : LinewiseInput lwi(input);
16 : std::vector<int> result;
17 : result.reserve(lwi.size());
18 8538 : std::ranges::transform(lwi, std::back_inserter(result), [](std::string_view const line) {
19 8538 : int const number = parseInt<int>(line.substr(1));
20 8538 : return line.front() == 'L' ? -number : number;
21 8538 : });
22 : return result;
23 : }
24 :
25 : } // namespace
26 :
27 1 : template <> std::string solvePart1<2025, 1>(std::string_view const input) {
28 1 : std::vector<int> rotations = parse(input);
29 1 : unsigned ctr = 0;
30 1 : int pos = startPos;
31 4269 : for (int const r : rotations) {
32 4269 : pos += r % dialSize;
33 4269 : if (pos < 0)
34 1081 : pos += dialSize;
35 3188 : else if (pos >= dialSize)
36 1051 : pos -= dialSize;
37 4269 : if (pos == 0)
38 1064 : ++ctr;
39 4269 : }
40 1 : return std::to_string(ctr);
41 1 : }
42 :
43 1 : template <> std::string solvePart2<2025, 1>(std::string_view const input) {
44 1 : std::vector<int> rotations = parse(input);
45 1 : unsigned ctr = 0;
46 1 : int pos = startPos;
47 4269 : for (int const r : rotations) {
48 4269 : int const oldPos = pos;
49 4269 : auto const div = std::div(r, dialSize);
50 4269 : ctr += std::abs(div.quot);
51 4269 : pos += div.rem;
52 4269 : if (oldPos != 0 && (pos <= 0 || pos >= dialSize))
53 2112 : ++ctr;
54 4269 : if (pos < 0)
55 1081 : pos += dialSize;
56 3188 : else if (pos >= dialSize)
57 1051 : pos -= dialSize;
58 4269 : }
59 1 : return std::to_string(ctr);
60 1 : }
|