-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay09.cpp
More file actions
28 lines (26 loc) · 687 Bytes
/
Day09.cpp
File metadata and controls
28 lines (26 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "Solution.hpp"
long
decode(std::string::const_iterator s0, std::string::const_iterator s1, bool part2)
{
auto i1 = s0;
while (i1 != s1 && *i1 != '(')
++i1;
if (i1 == s1)
return std::distance(s0, s1);
auto i0 = i1;
int l{0}, r{0};
while (*++i1 != 'x')
l *= 10, l += (*i1 - '0');
while (*++i1 != ')')
r *= 10, r += (*i1 - '0');
auto s = ++i1 + l;
return std::distance(s0, i0) + r * (part2 ? decode(i1, s, true) : std::distance(i1, s)) + decode(s, s1, part2);
}
template <>
void
solve<Day09>(bool part2, std::istream& is, std::ostream& os)
{
std::string input;
is >> input;
os << decode(input.begin(), input.end(), part2) << std::endl;
}