Skip to content

Commit 73b906f

Browse files
committed
4
1 parent f3bfe11 commit 73b906f

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

cpp/4.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "common.h"
2+
3+
namespace {
4+
auto parse(const std::string &s) {
5+
auto it = std::find(s.begin(), s.end(), ',');
6+
std::string_view sec1{s.begin(), it};
7+
std::string_view sec2{std::next(it, 1), s.end()};
8+
std::tuple<long, long, long, long> out{};
9+
{
10+
size_t hy = sec1.find('-');
11+
std::from_chars(sec1.data(), sec1.data() + hy, std::get<0>(out));
12+
std::from_chars(sec1.data() + hy + 1, sec1.data() + sec1.size(),
13+
std::get<1>(out));
14+
}
15+
{
16+
size_t hy = sec2.find('-');
17+
std::from_chars(sec2.data(), sec2.data() + hy, std::get<2>(out));
18+
std::from_chars(sec2.data() + hy + 1, sec2.data() + sec2.size(),
19+
std::get<3>(out));
20+
}
21+
return out;
22+
}
23+
24+
long p1(const auto &input) {
25+
long c{0};
26+
for (const auto &l : input) {
27+
auto [s1b, s1e, s2b, s2e] = parse(l);
28+
c += ((s1b >= s2b && s1e <= s2e) || (s1b <= s2b && s1e >= s2e)) ? 1 : 0;
29+
}
30+
return c;
31+
}
32+
33+
long p2(const auto &input) {
34+
long c{0};
35+
for (const auto &l : input) {
36+
auto [s1b, s1e, s2b, s2e] = parse(l);
37+
c += (s1e < s2b || s2e < s1b) ? 0 : 1;
38+
}
39+
return c;
40+
}
41+
} // namespace
42+
43+
int main() {
44+
const auto &input = gb::advent2021::readIn();
45+
gb::advent2021::writeOut(std::to_string(p1(input)));
46+
gb::advent2021::writeOut(std::to_string(p2(input)));
47+
}

cpp/common.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <algorithm>
22
#include <bitset>
33
#include <cctype>
4+
#include <charconv>
45
#include <climits>
56
#include <cmath>
67
#include <cstdint>

cpp/meson.build

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ executable('3',
1414
'3.cpp',
1515
dependencies: [])
1616

17-
# executable('4',
18-
# '4.cpp',
19-
# dependencies: [])
17+
executable('4',
18+
'4.cpp',
19+
dependencies: [])
2020

2121
# executable('5',
2222
# '5.cpp',

0 commit comments

Comments
 (0)