aboutsummaryrefslogtreecommitdiff
path: root/src/2020/day7/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-05-06 15:43:30 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-05-06 15:43:30 +0800
commit591f81258727537ce8a7a74df5e1de377d85519a (patch)
tree19c36fb13797d8d8408352be2ae492e174fd537e /src/2020/day7/aoc.cpp
parentb2af7ad35cc3610c2617ab288843de8d543fc7ed (diff)
downloadadvent-of-code-591f81258727537ce8a7a74df5e1de377d85519a.tar.gz
advent-of-code-591f81258727537ce8a7a74df5e1de377d85519a.zip
2020 day7 part1
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