diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-17 12:01:02 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-17 12:01:02 +0800 |
commit | 0d390f7adf2790a154fcfb7ff8e1bbac5b13611c (patch) | |
tree | bb6c1bfa5137ed62ba50b334d57c2c9cb49e0f94 /src | |
parent | d8ae7408506fa54f698c5d0538883be1dfa44bd6 (diff) | |
download | advent-of-code-0d390f7adf2790a154fcfb7ff8e1bbac5b13611c.tar.gz advent-of-code-0d390f7adf2790a154fcfb7ff8e1bbac5b13611c.zip |
2020 day6 part1
Diffstat (limited to 'src')
-rw-r--r-- | src/2020/day6/README.md | 35 | ||||
-rw-r--r-- | src/2020/day6/aoc.cpp | 21 | ||||
-rw-r--r-- | src/2020/day6/aoc.h | 27 | ||||
-rw-r--r-- | src/2020/day6/input0 | 15 |
4 files changed, 97 insertions, 1 deletions
diff --git a/src/2020/day6/README.md b/src/2020/day6/README.md index c3e09dc..3f124ec 100644 --- a/src/2020/day6/README.md +++ b/src/2020/day6/README.md @@ -38,4 +38,39 @@ In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11. For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts? +--- Part Two --- + +As you finish the last group's customs declaration, you notice that you misread one word in the instructions: + +You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions to which everyone answered "yes"! + +Using the same example as above: + +abc + +a +b +c + +ab +ac + +a +a +a +a + +b + +This list represents answers from five groups: + + In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c. + In the second group, there is no question to which everyone answered "yes". + In the third group, everyone answered yes to only 1 question, a. Since some people did not answer "yes" to b or c, they don't count. + In the fourth group, everyone answered yes to only 1 question, a. + In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b. + +In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6. + +For each group, count the number of questions to which everyone answered "yes". What is the sum of those counts? diff --git a/src/2020/day6/aoc.cpp b/src/2020/day6/aoc.cpp index 849bad9..d64262e 100644 --- a/src/2020/day6/aoc.cpp +++ b/src/2020/day6/aoc.cpp @@ -1,5 +1,26 @@ #include "aoc.h" +#include <algorithm> +#include <vector> namespace aoc2020 { +int day6(line_view file) { + std::vector<question_group> gs; + + const char* p1 = file.line; + const char* p = p1; + while (p < file.line + file.length) { + if (*p == '\n' && *(p + 1) == '\n') { + gs.emplace_back(line_view{p1, p}); + p1 = p + 2; + } + p++; + } + gs.emplace_back(line_view{p1, p}); + + int total{0}; + std::for_each(gs.begin(), gs.end(), [&total](const question_group& g) { total += g.summary(); }); + return total; } + +} // namespace aoc2020 diff --git a/src/2020/day6/aoc.h b/src/2020/day6/aoc.h index c5ad3d1..dc2989d 100644 --- a/src/2020/day6/aoc.h +++ b/src/2020/day6/aoc.h @@ -3,4 +3,29 @@ namespace aoc2020 { -} +struct question_group { + line_view people; + int count[26] = {0}; + + int summary() const noexcept { + int total{0}; + for (int x : count) { + total += int(x > 0); + } + return total; + } + + question_group(line_view lv) : people(lv) { + const char* p = lv.line; + while (p < lv.line + lv.length) { + if (*p >= 'a' && *p <= 'z') { + count[*p - 'a'] += 1; + } + p++; + } + } +}; + +int day6(line_view); + +} // namespace aoc2020 diff --git a/src/2020/day6/input0 b/src/2020/day6/input0 new file mode 100644 index 0000000..0f5b3bc --- /dev/null +++ b/src/2020/day6/input0 @@ -0,0 +1,15 @@ +abc + +a +b +c + +ab +ac + +a +a +a +a + +b |