Skip to content

Commit cc691ff

Browse files
committed
2
1 parent 48b4b8e commit cc691ff

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

Diff for: cpp/2.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "common.h"
2+
3+
namespace {
4+
long p1(const auto &input) {
5+
std::unordered_map<std::string, std::string> wins{{"AY", "Y"}, {"AZ", "A"},
6+
{"BX", "B"}, {"BZ", "Z"},
7+
{"CX", "X"}, {"CY", "C"}};
8+
long s{0};
9+
for (const auto &l : input) {
10+
std::string o, p;
11+
std::istringstream{l} >> o >> p;
12+
s += p[0] - 'W';
13+
if (const auto &w = wins.find(o + p); w != wins.end()) {
14+
s += (w->second == o) ? 0 : 6;
15+
} else {
16+
s += 3;
17+
}
18+
}
19+
return s;
20+
}
21+
22+
long p2(const auto &input) {
23+
std::unordered_map<std::string, int> plays{
24+
{"AX", 3 + 0}, {"AY", 1 + 3}, {"AZ", 2 + 6}, {"BX", 1 + 0}, {"BY", 2 + 3},
25+
{"BZ", 3 + 6}, {"CX", 2 + 0}, {"CY", 3 + 3}, {"CZ", 1 + 6}};
26+
long s{0};
27+
for (const auto &l : input) {
28+
std::string o, w;
29+
std::istringstream{l} >> o >> w;
30+
if (const auto &p = plays.at(o + w)) {
31+
s += p;
32+
}
33+
}
34+
return s;
35+
}
36+
} // namespace
37+
38+
int main() {
39+
const auto &input = gb::advent2021::readIn();
40+
gb::advent2021::writeOut(std::to_string(p1(input)));
41+
gb::advent2021::writeOut(std::to_string(p2(input)));
42+
}

Diff for: cpp/meson.build

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ executable('1',
66
'1.cpp',
77
dependencies: [])
88

9-
# executable('2',
10-
# '2.cpp',
11-
# dependencies: [])
9+
executable('2',
10+
'2.cpp',
11+
dependencies: [])
1212

1313
# executable('3',
1414
# '3.cpp',

0 commit comments

Comments
 (0)