-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.cpp
46 lines (42 loc) · 1.02 KB
/
day06.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "day06.h"
#include "common/stringutils.h"
#include <ranges>
ResultBase* Day06::SolveFirst() const
{
int sum = 0;
std::ranges::for_each(std::views::transform(ParseInput(), [&](const std::string& n) { return CalcAnswerSum(n); }), [&](int n)
{
sum += n;
});
return new Result(sum);
}
ResultBase* Day06::SolveSecond() const
{
int sum = 0;
std::ranges::for_each(std::views::transform(ParseInput(), [&](const std::string& n) { return CalcAnswerSum(n, true); }), [&](int n)
{
sum += n;
});
return new Result(sum);
}
std::vector<std::string> Day06::ParseInput() const
{
std::string input = common::ReplaceStr(this->input.raw, "\r\n", "\n");
return common::SplitStr(input, "\n\n");
}
int Day06::CalcAnswerSum(const std::string& answer, bool all) const
{
int questions[26] = {};
for(char c : answer)
{
if(c >= 'a' && c <= 'z')
{
questions['z' - c]++;
}
}
int groupSize = std::ranges::count(answer, '\n') + 1;
return std::ranges::count_if(questions, [&](int n)
{
return all ? n == groupSize : n > 0;
});
}