Skip to content

Commit 5d577fb

Browse files
committed
2024 day 21: solve part 2
1 parent 45e6749 commit 5d577fb

File tree

6 files changed

+472
-140
lines changed

6 files changed

+472
-140
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Day 21:
22
126384
3+
154115708116294

2024/src/day21.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,31 @@ int main(int argc, char **argv) {
1616
using namespace aoc::day21;
1717
auto codes = read_input(args.infile);
1818

19-
long total = 0;
19+
long part_1 = 0;
20+
long part_2 = 0;
2021
for (const auto &code : codes) {
2122
// one numeric keypad that a robot is using
22-
std::vector<DirKey> keys = control_numeric(code.keys);
23+
std::vector<Key> keys = code.keys;
2324
// two directional keypads that robots are using
24-
for (int i = 0; i < 2; ++i) {
25-
keys = control_directional(keys);
26-
}
27-
long complexity = keys.size() * code.numeric_value();
28-
total += complexity;
25+
long part_1_size = count_presses_memo(keys, 1 + 2);
26+
// 25 directional keypads that robots are using
27+
long part_2_size = count_presses_memo(keys, 1 + 25);
28+
long complexity_1 = part_1_size * code.numeric_value();
29+
part_1 += complexity_1;
30+
long complexity_2 = part_2_size * code.numeric_value();
31+
part_2 += complexity_2;
2932
if constexpr (aoc::DEBUG) {
3033
std::cerr << code.code << ": ";
3134
for (const auto key : keys) {
3235
std::cerr << key;
3336
}
34-
std::cerr << "\n complexity = " << keys.size() << " * "
35-
<< code.numeric_value() << " = " << complexity << "\n";
37+
std::cerr << "\n part 1 complexity = " << part_1_size << " * "
38+
<< code.numeric_value() << " = " << complexity_1 << "\n";
39+
std::cerr << " part 2 complexity = " << part_2_size << " * "
40+
<< code.numeric_value() << " = " << complexity_2 << "\n";
3641
}
3742
}
38-
std::cout << total << "\n";
43+
std::cout << part_1 << "\n" << part_2 << "\n";
3944

4045
return 0;
4146
}

0 commit comments

Comments
 (0)