Skip to content

Commit b29b6cc

Browse files
committed
10
1 parent 9f30f08 commit b29b6cc

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

cpp/10.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "common.h"
2+
3+
namespace {
4+
long p1(const auto &input) {
5+
long out{0};
6+
for (auto [i, ins, inscycle, val, cy, reg] =
7+
std::tuple<size_t, std::string, int, long, long, long>{
8+
{0}, {}, {}, {}, {}, {1}};
9+
i < input.size(); ++i, reg += val) {
10+
std::istringstream ss{input[i]};
11+
ss >> ins >> val;
12+
inscycle = (ins == "addx" ? 2 : 1);
13+
val = (ins == "noop" ? 0 : val);
14+
while (inscycle--) {
15+
++cy;
16+
if (cy <= 220 && (((cy - 20) % 40) == 0)) {
17+
out += (cy * reg);
18+
}
19+
}
20+
}
21+
return out;
22+
}
23+
24+
void p2(const auto &input) {
25+
std::array<char, 40 * 6> crt{};
26+
for (auto [i, ins, inscycle, val, cy, px, reg] =
27+
std::tuple<size_t, std::string, int, long, long, long, long>{
28+
{0}, {}, {}, {}, {}, {}, {1}};
29+
i < input.size(); ++i, reg += val) {
30+
std::istringstream ss{input[i]};
31+
ss >> ins >> val;
32+
inscycle = (ins == "addx" ? 2 : 1);
33+
val = (ins == "noop" ? 0 : val);
34+
while (inscycle--) {
35+
++cy;
36+
px = cy - 1;
37+
crt[px] = ((reg - 1 <= (px % 40)) && (reg + 1 >= (px % 40))) ? '#' : '.';
38+
}
39+
}
40+
41+
for (size_t i = 0, j = 0; i < 40 * 6; ++i) {
42+
if ((i != 0) && (i % 40 == 0)) {
43+
++j;
44+
std::cout << std::endl;
45+
}
46+
std::cout << crt[(j * 40) + (i % 40)];
47+
}
48+
}
49+
} // namespace
50+
51+
int main() {
52+
const auto &input = gb::advent2021::readIn();
53+
gb::advent2021::writeOut(std::to_string(p1(input)));
54+
p2(input);
55+
}

cpp/meson.build

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ executable('9',
3838
'9.cpp',
3939
dependencies: [])
4040

41-
# executable('10',
42-
# '10.cpp',
43-
# dependencies: [])
41+
executable('10',
42+
'10.cpp',
43+
dependencies: [])
4444

4545
# executable('11',
4646
# '11.cpp',

0 commit comments

Comments
 (0)