aboutsummaryrefslogtreecommitdiff
path: root/src/2020/day7/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2020/day7/aoc.cpp')
-rw-r--r--src/2020/day7/aoc.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/2020/day7/aoc.cpp b/src/2020/day7/aoc.cpp
index 2519be1..9ed0f99 100644
--- a/src/2020/day7/aoc.cpp
+++ b/src/2020/day7/aoc.cpp
@@ -1,4 +1,35 @@
#include "aoc.h"
+#include <algorithm>
namespace aoc2020 {
+
+bool find_color(color_bag* bag, line_view color) {
+ if (bag->color == color) {
+ return true;
+ } else {
+ for (auto& p : bag->bags) {
+ if (find_color(p.first, color)) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
+
+int day7(line_view file, const char* color) {
+ bag_regulations rs;
+ per_line(file, [&rs](line_view lv) {
+ rs.parse(lv);
+ return true;
+ });
+
+ int total{0};
+ for (auto& b : rs.regulations) {
+ if (!(b.second->color == color)) {
+ total += int(find_color(b.second, color));
+ }
+ }
+
+ return total;
}
+} // namespace aoc2020