aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day04.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-11-02 21:41:58 +0100
committerThomas Schmucker <ts@its1.de>2025-11-02 21:41:58 +0100
commit756f22d58bb198b8f34589c112e1003614ccdcd6 (patch)
tree4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day04.cpp
parentfcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff)
downloadadvent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz
advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2
advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day04.cpp')
-rw-r--r--2017/src/day04.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/2017/src/day04.cpp b/2017/src/day04.cpp
new file mode 100644
index 0000000..01e89b2
--- /dev/null
+++ b/2017/src/day04.cpp
@@ -0,0 +1,78 @@
1#include <algorithm>
2#include <filesystem>
3#include <fstream>
4#include <iostream>
5#include <set>
6#include <sstream>
7#include <string>
8
9using namespace std;
10
11namespace {
12
13vector<string>
14read_lines(const filesystem::path& filename)
15{
16 ifstream file{ filename };
17 vector<string> lines;
18
19 for ( string line; getline(file, line); ) {
20 lines.emplace_back(line);
21 }
22
23 return lines;
24}
25
26vector<string>
27split(const string& line)
28{
29 stringstream strm{ line };
30 vector<string> words;
31
32 for ( string word; strm >> word; ) {
33 words.emplace_back(word);
34 }
35
36 return words;
37}
38
39void
40part1(const vector<string>& lines)
41{
42 auto num = ranges::count_if(lines, [](const string& line) {
43 auto words = split(line);
44 set<string> set{ words.begin(), words.end() };
45
46 return set.size() == words.size();
47 });
48
49 cout << "Part1: " << num << '\n';
50}
51
52void
53part2(const vector<string>& lines)
54{
55 auto num = ranges::count_if(lines, [](const string& line) {
56 auto words = split(line);
57 set<string> set;
58
59 for ( auto word: words ) {
60 ranges::sort(word);
61 set.insert(word);
62 }
63
64 return set.size() == words.size();
65 });
66
67 cout << "Part2: " << num << '\n';
68}
69
70} // namespace
71
72int
73main()
74{
75 auto lines = read_lines("data/day04.txt");
76 part1(lines);
77 part2(lines);
78}