aboutsummaryrefslogtreecommitdiff
path: root/2022/src/day04.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-04-28 11:48:01 +0200
committerThomas Schmucker <ts@its1.de>2024-04-28 11:48:01 +0200
commit3a7ad6ec3c4ab88fa59ca613cbbfc8531d23cc58 (patch)
tree662f8970f8d35f03cbb322fe4902a9016dad6a6e /2022/src/day04.cpp
parentfcf4708efd24461ea71e3086d5fbd672d53621d2 (diff)
downloadadvent-of-code-3a7ad6ec3c4ab88fa59ca613cbbfc8531d23cc58.tar.gz
advent-of-code-3a7ad6ec3c4ab88fa59ca613cbbfc8531d23cc58.tar.bz2
advent-of-code-3a7ad6ec3c4ab88fa59ca613cbbfc8531d23cc58.zip
day 4, advent of code 2022
Diffstat (limited to '2022/src/day04.cpp')
-rw-r--r--2022/src/day04.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/2022/src/day04.cpp b/2022/src/day04.cpp
new file mode 100644
index 0000000..e3e4cd1
--- /dev/null
+++ b/2022/src/day04.cpp
@@ -0,0 +1,50 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <string>
5#include <tuple>
6#include <vector>
7using namespace std;
8
9vector<tuple<int, int, int, int>>
10read_file(string_view filename)
11{
12 fstream input{ filename };
13 vector<tuple<int, int, int, int>> data;
14
15 for ( string line; getline(input, line); ) {
16 int a, b, c, d;
17 (void) sscanf(line.c_str(), "%d-%d,%d-%d", &a, &b, &c, &d);
18 data.emplace_back(a, b, c, d);
19 }
20
21 return data;
22}
23
24void
25part1(const vector<tuple<int, int, int, int>>& data)
26{
27 auto sum = count_if(begin(data), end(data), [](auto& tuple) {
28 const auto [a, b, c, d] = tuple;
29 return (a <= c && b >= d) || (c <= a && d >= b);
30 });
31 cout << sum << endl;
32}
33
34void
35part2(const vector<tuple<int, int, int, int>>& data)
36{
37 auto sum = count_if(begin(data), end(data), [](auto& tuple) {
38 const auto [a, b, c, d] = tuple;
39 return max(a, c) <= min(b, d);
40 });
41 cout << sum << endl;
42}
43
44int
45main()
46{
47 const auto data = read_file("data/day04.txt");
48 part1(data);
49 part2(data);
50}