aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-17 14:19:44 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-17 14:19:44 +0800
commitf2142ba692d7eed53096441174737ce8f7255faf (patch)
tree1c3a34094947b578891278d0a87ef6a55a11275b /src
parent0d390f7adf2790a154fcfb7ff8e1bbac5b13611c (diff)
downloadadvent-of-code-f2142ba692d7eed53096441174737ce8f7255faf.tar.gz
advent-of-code-f2142ba692d7eed53096441174737ce8f7255faf.zip
2020 day6
Diffstat (limited to 'src')
-rw-r--r--src/2020/day6/aoc.cpp14
-rw-r--r--src/2020/day6/aoc.h44
2 files changed, 52 insertions, 6 deletions
diff --git a/src/2020/day6/aoc.cpp b/src/2020/day6/aoc.cpp
index d64262e..5f44585 100644
--- a/src/2020/day6/aoc.cpp
+++ b/src/2020/day6/aoc.cpp
@@ -4,23 +4,27 @@
namespace aoc2020 {
-int day6(line_view file) {
+std::pair<int, 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});
+ gs.emplace_back(line_view{p1, p + 1});
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;
+ int t0{0};
+ int t1{0};
+ std::for_each(gs.begin(), gs.end(), [&t0, &t1](const question_group& g) {
+ t0 += g.summary();
+ t1 += g.common();
+ });
+ return {t0, t1};
}
} // namespace aoc2020
diff --git a/src/2020/day6/aoc.h b/src/2020/day6/aoc.h
index dc2989d..5f5b2b6 100644
--- a/src/2020/day6/aoc.h
+++ b/src/2020/day6/aoc.h
@@ -1,5 +1,6 @@
#pragma once
#include "common.h"
+#include <vector>
namespace aoc2020 {
@@ -15,6 +16,47 @@ struct question_group {
return total;
}
+ int common(const std::vector<line_view>& vs) const noexcept {
+ line_view shortest{nullptr, INT32_MAX};
+ for (auto& l : vs) {
+ if (l.length < shortest.length) {
+ shortest = l;
+ }
+ }
+
+ std::vector<int> ts(shortest.length, 0);
+ int i{0};
+ per_char(shortest, [&vs, &ts, &i](char c) {
+ char t[2] = {c, '\0'};
+ for (auto& l : vs) {
+ const char* p = l.contains(line_view{t, 1});
+ ts[i] += int(p != nullptr);
+ }
+ i++;
+ return true;
+ });
+
+ int total{0};
+ for (auto i : ts) {
+ total += int(!(size_t(i) < vs.size()));
+ }
+ return total;
+ }
+
+ int common() const noexcept {
+ std::vector<line_view> ls;
+ const char* p1 = people.line;
+ const char* p = p1;
+ while (p < people.line + people.length) {
+ if (*p == '\n') {
+ ls.emplace_back(p1, p);
+ p1 = p + 1;
+ }
+ p++;
+ }
+ return common(ls);
+ }
+
question_group(line_view lv) : people(lv) {
const char* p = lv.line;
while (p < lv.line + lv.length) {
@@ -26,6 +68,6 @@ struct question_group {
}
};
-int day6(line_view);
+std::pair<int, int> day6(line_view);
} // namespace aoc2020