aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-05-14 11:11:00 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-05-14 11:11:00 +0800
commitcd8458f37abae6b5d1f7f289a22e506da8070357 (patch)
treead77724654c4d4c6e821dd7be9e4dfbc1f736c09 /src
parentdfe25822f031cebea626eca29ea304af5598f657 (diff)
downloadadvent-of-code-cd8458f37abae6b5d1f7f289a22e506da8070357.tar.gz
advent-of-code-cd8458f37abae6b5d1f7f289a22e506da8070357.zip
2019 day8 part1
Diffstat (limited to 'src')
-rw-r--r--src/2019/day8/README.md32
-rw-r--r--src/2019/day8/aoc.cpp38
-rw-r--r--src/2019/day8/aoc.h1
3 files changed, 71 insertions, 0 deletions
diff --git a/src/2019/day8/README.md b/src/2019/day8/README.md
index 3b3f73a..f740a28 100644
--- a/src/2019/day8/README.md
+++ b/src/2019/day8/README.md
@@ -22,4 +22,36 @@ The image you received is 25 pixels wide and 6 pixels tall.
To make sure the image wasn't corrupted during transmission, the Elves would like you to find the layer that contains the fewest 0 digits. On that layer, what is the number of 1 digits multiplied by the number of 2 digits?
+--- Part Two ---
+Now you're ready to decode the image. The image is rendered by stacking the layers and aligning the pixels with the same positions in each layer. The digits indicate the color of the corresponding pixel: 0 is black, 1 is white, and 2 is transparent.
+
+The layers are rendered with the first layer in front and the last layer in back. So, if a given position has a transparent pixel in the first and second layers, a black pixel in the third layer, and a white pixel in the fourth layer, the final image would have a black pixel at that position.
+
+For example, given an image 2 pixels wide and 2 pixels tall, the image data 0222112222120000 corresponds to the following image layers:
+
+Layer 1: 02
+ 22
+
+Layer 2: 11
+ 22
+
+Layer 3: 22
+ 12
+
+Layer 4: 00
+ 00
+
+Then, the full image can be found by determining the top visible pixel in each position:
+
+ The top-left pixel is black because the top layer is 0.
+ The top-right pixel is white because the top layer is 2 (transparent), but the second layer is 1.
+ The bottom-left pixel is white because the top two layers are 2, but the third layer is 1.
+ The bottom-right pixel is black because the only visible pixel in that position is 0 (from layer 4).
+
+So, the final image looks like this:
+
+01
+10
+
+What message is produced after decoding your image?
diff --git a/src/2019/day8/aoc.cpp b/src/2019/day8/aoc.cpp
index f0cbc1b..c92a054 100644
--- a/src/2019/day8/aoc.cpp
+++ b/src/2019/day8/aoc.cpp
@@ -1,5 +1,43 @@
#include "aoc.h"
+#include <algorithm>
namespace aoc2019 {
+struct digits {
+ int chars[10] = {0};
+};
+
+static void count_digit(const char* p1, const char* p2, digits* p) {
+ while (p1 < p2) {
+ p->chars[*p1 - '0'] += 1;
+ p1++;
+ }
+}
+
+std::pair<int, int> day8(line_view file) {
+ digits ds[100];
+ int x = 6 * 25;
+ for (int i = 0; i < 100; i++) {
+ const char* p1 = file.line + i * x;
+ const char* p2 = file.line + (i + 1) * x;
+ count_digit(p1, p2, &ds[i]);
+ }
+
+ struct ct {
+ int zeros = 0;
+ int times = 0;
+ } cs[100];
+
+ for (int i = 0; i < 100; i++) {
+ cs[i].zeros = ds[i].chars[0];
+ cs[i].times = ds[i].chars[1] * ds[i].chars[2];
+ // for (int j : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) {
+ // printf("%d ", ds[i].chars[j]);
+ // }
+ // printf("\n");
+ }
+
+ std::sort(cs, cs + 100, [](ct c1, ct c2) { return c1.zeros < c2.zeros; });
+ return {cs[0].times, 0};
}
+} // namespace aoc2019
diff --git a/src/2019/day8/aoc.h b/src/2019/day8/aoc.h
index 959d6d8..caac71d 100644
--- a/src/2019/day8/aoc.h
+++ b/src/2019/day8/aoc.h
@@ -3,4 +3,5 @@
namespace aoc2019 {
+std::pair<int, int> day8(line_view);
}