aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-14 22:07:42 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-14 22:07:42 +0800
commit9dcf63c0a3c8cfd0b1c1d6b335d38524775cd933 (patch)
treec629313107262f906a938139340f615b4ee2a479 /src
parentd94968612147691923e6b1dc5488414702f5ad8e (diff)
downloadadvent-of-code-9dcf63c0a3c8cfd0b1c1d6b335d38524775cd933.tar.gz
advent-of-code-9dcf63c0a3c8cfd0b1c1d6b335d38524775cd933.zip
2016 2017 day6 part1
Diffstat (limited to 'src')
-rw-r--r--src/2016/day6/README.md7
-rw-r--r--src/2017/day6/README.md5
-rw-r--r--src/2017/day6/aoc.cpp21
-rw-r--r--src/2017/day6/aoc.h63
-rw-r--r--src/2017/day6/input2
5 files changed, 96 insertions, 2 deletions
diff --git a/src/2016/day6/README.md b/src/2016/day6/README.md
index 79c6660..7279496 100644
--- a/src/2016/day6/README.md
+++ b/src/2016/day6/README.md
@@ -25,4 +25,11 @@ The most common character in the first column is e; in the second, a; in the thi
Given the recording in your puzzle input, what is the error-corrected version of the message being sent?
+--- Part Two ---
+Of course, that would be the message - if you hadn't agreed to use a modified repetition code instead.
+In this modified code, the sender instead transmits what looks like random data, but for each character, the character they actually want to send is slightly less likely than the others. Even after signal-jamming noise, you can look at the letter distributions in each column and choose the least common letter to reconstruct the original message.
+
+In the above example, the least common character in the first column is a; in the second, d, and so on. Repeating this process for the remaining characters produces the original message, advent.
+
+Given the recording in your puzzle input and this new decoding methodology, what is the original message that Santa is trying to send?
diff --git a/src/2017/day6/README.md b/src/2017/day6/README.md
index 4f9bdfb..468e626 100644
--- a/src/2017/day6/README.md
+++ b/src/2017/day6/README.md
@@ -19,4 +19,9 @@ At this point, we've reached a state we've seen before: 2 4 1 2 was already seen
Given the initial block counts in your puzzle input, how many redistribution cycles must be completed before a configuration is produced that has been seen before?
+--- Part Two ---
+Out of curiosity, the debugger would also like to know the size of the loop: starting from a state that has already been seen, how many block redistribution cycles must be performed before that same state is seen again?
+In the example above, 2 4 1 2 is seen again after four cycles, and so the answer in that example would be 4.
+
+How many cycles are in the infinite loop that arises from the configuration in your puzzle input?
diff --git a/src/2017/day6/aoc.cpp b/src/2017/day6/aoc.cpp
index ed2d9d3..66511fd 100644
--- a/src/2017/day6/aoc.cpp
+++ b/src/2017/day6/aoc.cpp
@@ -1,5 +1,26 @@
#include "aoc.h"
+#include <set>
namespace aoc2017 {
+void next(int* step, memory_bank& m, std::set<memory_bank>& ms) {
+ if (ms.find(m) == ms.end()) {
+ ms.insert(m);
+ *step += 1;
+ int d{0};
+ int i = m.highest(&d);
+ // printf("%d %d %d\n", *step, i, d);
+ m.distribute(i, d);
+ next(step, m, ms);
+ }
}
+
+int day6(line_view file) {
+ std::set<memory_bank> ms;
+ memory_bank m{file};
+ int steps{0};
+ next(&steps, m, ms);
+ return steps;
+}
+
+} // namespace aoc2017
diff --git a/src/2017/day6/aoc.h b/src/2017/day6/aoc.h
index 7aacc4c..8e8f043 100644
--- a/src/2017/day6/aoc.h
+++ b/src/2017/day6/aoc.h
@@ -3,4 +3,65 @@
namespace aoc2017 {
-}
+struct memory_bank {
+ int banks[16];
+
+ void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ *d = 0;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+ }
+
+ memory_bank(line_view lv) {
+ const char* p = lv.line;
+ int i{0};
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, &banks[i++]);
+ }
+ p++;
+ }
+ }
+
+ bool operator<(const memory_bank& m) const noexcept {
+ for (size_t i = 0; i < ARRAY_SIZE(banks); i++) {
+ if (banks[i] < m.banks[i]) {
+ return true;
+ }
+ if (banks[i] > m.banks[i]) {
+ return false;
+ }
+ }
+ return false;
+ }
+
+ int highest(int* d) const noexcept {
+ int max{INT32_MIN};
+ int index{0};
+ for (size_t i = 0; i < ARRAY_SIZE(banks); i++) {
+ if (banks[i] > max) {
+ max = banks[i];
+ index = i;
+ }
+ }
+ *d = max;
+ return index;
+ }
+
+ void distribute(size_t i, int x) {
+ banks[i] = 0;
+ while (x > 0) {
+ i = (i == ARRAY_SIZE(banks) - 1) ? 0 : i + 1;
+ banks[i] += 1;
+ x -= 1;
+ }
+ }
+};
+
+int day6(line_view);
+
+} // namespace aoc2017
diff --git a/src/2017/day6/input b/src/2017/day6/input
index 04f1425..16cd5fa 100644
--- a/src/2017/day6/input
+++ b/src/2017/day6/input
@@ -1 +1 @@
-4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3
+4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3