aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-20 22:11:10 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-20 22:11:10 +0800
commit3bd1fa60246ecc9b2431350fb154b20547619da7 (patch)
tree9ff78fc6878c00a6c032ff6dd1beda642e7f543c /src
parent8d14dac6c384fcccfdf884f5c89d363ec00e97e2 (diff)
downloadadvent-of-code-3bd1fa60246ecc9b2431350fb154b20547619da7.tar.gz
advent-of-code-3bd1fa60246ecc9b2431350fb154b20547619da7.zip
day16 done
Diffstat (limited to 'src')
-rw-r--r--src/2015/day16/README.md8
-rw-r--r--src/2015/day16/aoc.cpp14
-rw-r--r--src/2015/day16/aoc.h119
3 files changed, 140 insertions, 1 deletions
diff --git a/src/2015/day16/README.md b/src/2015/day16/README.md
index 0a067a8..dd14383 100644
--- a/src/2015/day16/README.md
+++ b/src/2015/day16/README.md
@@ -33,3 +33,11 @@ You make a list of the things you can remember about each Aunt Sue. Things missi
What is the number of the Sue that got you the gift?
+--- Part Two ---
+
+As you're about to send the thank you note, something in the MFCSAM's instructions catches your eye. Apparently, it has an outdated retroencabulator, and so the output from the machine isn't exact values - some of them indicate ranges.
+
+In particular, the cats and trees readings indicates that there are greater than that many (due to the unpredictable nuclear decay of cat dander and tree pollen), while the pomeranians and goldfish readings indicate that there are fewer than that many (due to the modial interaction of magnetoreluctance).
+
+What is the number of the real Aunt Sue?
+
diff --git a/src/2015/day16/aoc.cpp b/src/2015/day16/aoc.cpp
index 2adfe7c..9d1ac4a 100644
--- a/src/2015/day16/aoc.cpp
+++ b/src/2015/day16/aoc.cpp
@@ -1,3 +1,17 @@
#include "aoc.h"
namespace aoc2015 {
+
+bool like(const sue& s1, const sue& s2) { return s1.like(s2); }
+bool like_range(const sue& s1, const sue& s2) { return s1.like_range(s2); }
+
+std::pair<int, int> day16(line_view file) {
+ aunts as;
+ per_line(file, [&as](line_view lv) {
+ as.parse(lv);
+ return true;
+ });
+
+ sue x{3, 7, 2, 3, 0, 0, 5, 3, 2, 1};
+ return {as.filter(x, like), as.filter(x, like_range)};
}
+} // namespace aoc2015
diff --git a/src/2015/day16/aoc.h b/src/2015/day16/aoc.h
index 6ec68d7..b51ad61 100644
--- a/src/2015/day16/aoc.h
+++ b/src/2015/day16/aoc.h
@@ -2,4 +2,121 @@
#include "common.h"
namespace aoc2015 {
-}
+struct sue {
+ int children = -1;
+ int cats = -1;
+ int samoyeds = -1;
+ int pomeranians = -1;
+ int akitas = -1;
+ int vizslas = -1;
+ int goldfish = -1;
+ int trees = -1;
+ int cars = -1;
+ int perfumes = -1;
+
+ sue() {}
+ sue(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9) {
+ children = i0;
+ cats = i1;
+ samoyeds = i2;
+ pomeranians = i3;
+ akitas = i4;
+ vizslas = i5;
+ goldfish = i6;
+ trees = i7;
+ cars = i8;
+ perfumes = i9;
+ }
+
+ bool like(const sue& x) const noexcept {
+ const int* is0[] = {
+ &children, &cats, &samoyeds, &pomeranians, &akitas, &vizslas, &goldfish, &trees, &cars, &perfumes,
+ };
+ const int* is1[] = {
+ &x.children, &x.cats, &x.samoyeds, &x.pomeranians, &x.akitas,
+ &x.vizslas, &x.goldfish, &x.trees, &x.cars, &x.perfumes,
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(is0); i++) {
+ if (*is0[i] != -1 && *is0[i] != *is1[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ bool like_range(const sue& x) const noexcept {
+ const int* is0[] = {
+ &children, &cats, &samoyeds, &pomeranians, &akitas, &vizslas, &goldfish, &trees, &cars, &perfumes,
+ };
+ const int* is1[] = {
+ &x.children, &x.cats, &x.samoyeds, &x.pomeranians, &x.akitas,
+ &x.vizslas, &x.goldfish, &x.trees, &x.cars, &x.perfumes,
+ };
+ auto p1 = [](const int* i0, const int* i1) -> bool { return *i0 == *i1; };
+ auto p2 = [](const int* i0, const int* i1) -> bool { return *i0 > *i1; };
+ auto p3 = [](const int* i0, const int* i1) -> bool { return *i0 < *i1; };
+
+ std::function<bool(const int*, const int*)> f[] = {
+ p1, p2, p1, p3, p1, p1, p3, p2, p1, p1,
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(is0); i++) {
+ if (*is0[i] != -1 && !(f[i])(is0[i], is1[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+};
+
+bool like(const sue& s1, const sue& s2);
+bool like_range(const sue& s1, const sue& s2);
+
+struct aunts {
+ sue as[500];
+
+ int get_number(const char* p) {
+ int d{0};
+ while ((*p) >= '0' && (*p) <= '9') {
+ d = d * 10 + *p - '0';
+ p++;
+ }
+ return d;
+ }
+
+ typedef bool(*likef)(const sue&, const sue&);
+ int filter(sue s, likef f) {
+ for (size_t i = 0; i < ARRAY_SIZE(as); i++) {
+ if (f(as[i],s)) {
+ return i + 1;
+ }
+ }
+ return -1;
+ }
+
+ void parse(line_view lv) {
+ static const char* keys[] = {
+ "children", "cats", "samoyeds", "pomeranians", "akitas", "vizslas", "goldfish", "trees", "cars", "perfumes",
+ };
+ sue x;
+ int i = get_number(lv.line + 4);
+ int* is[] = {
+ &x.children, &x.cats, &x.samoyeds, &x.pomeranians, &x.akitas,
+ &x.vizslas, &x.goldfish, &x.trees, &x.cars, &x.perfumes,
+ };
+ for (size_t k = 0; k < ARRAY_SIZE(keys); k++) {
+ const char* p = lv.contains(keys[k]);
+ if (p != nullptr) {
+ *is[k] = get_number(p + strlen(keys[k]) + 2);
+ // printf("%d %s %d\n", i, keys[k], *is[k]);
+ }
+ }
+
+ as[i - 1] = x;
+ }
+};
+
+std::pair<int,int> day16(line_view);
+
+} // namespace aoc2015