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+ }
0 commit comments