From 625966f5b8f260b994bd97cb39a85f95eb4c8d63 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Mon, 3 Oct 2022 10:54:54 +0200 Subject: [PATCH] 2019 day 8: parts 1 & 2 --- 2019/RESULTS.txt | 20 +++++++ 2019/day08/EXAMPLE.txt | 2 +- 2019/day08/README.org | 57 ++++++++++++++++++-- 2019/day08/aoc-c.c | 120 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 2019/day08/aoc-c.c diff --git a/2019/RESULTS.txt b/2019/RESULTS.txt index 9b9ad50..b88f369 100644 --- a/2019/RESULTS.txt +++ b/2019/RESULTS.txt @@ -96,3 +96,23 @@ aoc-c : res=65464 aoc-c : res=1518124 time: 0:00.00 real, 0.00 user, 0.00 sys context-switch: 0+1, page-faults: 0+94 + +========================================= +================= day08 ================= +========================================= + ++++++++++++++++++ part 1 +aoc-c : res=2250 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+93 + ++++++++++++++++++ part 2 +#### # # ## # # # +# # # # # # # +### #### # # # # +# # # # # # # +# # # # # # # # +# # # ## ## #### +aoc-c : res=0 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+90 diff --git a/2019/day08/EXAMPLE.txt b/2019/day08/EXAMPLE.txt index 8db426e..2bca7976 100644 --- a/2019/day08/EXAMPLE.txt +++ b/2019/day08/EXAMPLE.txt @@ -1 +1 @@ -123456789012 +0222112222120000 diff --git a/2019/day08/README.org b/2019/day08/README.org index 2e977ba..891e4c2 100644 --- a/2019/day08/README.org +++ b/2019/day08/README.org @@ -43,10 +43,57 @@ 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?/ -To begin, [[file:8/input][get your puzzle input]]. +Your puzzle answer was =2250=. -Answer: +** --- 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. -You can also [Shareon -[[https://twitter.com/intent/tweet?text=%22Space+Image+Format%22+%2D+Day+8+%2D+Advent+of+Code+2019&url=https%3A%2F%2Fadventofcode%2Ecom%2F2019%2Fday%2F8&related=ericwastl&hashtags=AdventOfCode][Twitter]] -[[javascript:void(0);][Mastodon]]] this puzzle. +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: + +#+BEGIN_EXAMPLE + Layer 1: 02 + 22 + + Layer 2: 11 + 22 + + Layer 3: 22 + 12 + + Layer 4: 00 + 00 +#+END_EXAMPLE + +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: + +#+BEGIN_EXAMPLE + 01 + 10 +#+END_EXAMPLE + +/What message is produced after decoding your image?/ + +Your puzzle answer was =FHJUL=. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2019/day08/aoc-c.c b/2019/day08/aoc-c.c new file mode 100644 index 0000000..39e8cc9 --- /dev/null +++ b/2019/day08/aoc-c.c @@ -0,0 +1,120 @@ +/* aoc-c.c: Advent of Code 2019, day 8 parts 1 & 2 + * + * Copyright (C) 2022 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include +#include + +#include "br.h" +#include "bits.h" +#include "debug.h" +#include "list.h" +#include "pool.h" + +struct input { + int len; + char *buf; + +}; + +static int part1(struct input *input, int width, int height) +{ + int depth = input->len / width / height; + int minzero = input->len, n1n2; + + for (int i = 0; i < depth; ++i) { + char *layer = input->buf + i * (width * height); + int tmp[10] = {0}; + for (int j = 0; j < width*height; ++j) { + tmp[layer[j] - '0'] ++; + } + if (tmp[0] < minzero) { + minzero = tmp[0]; + n1n2 = tmp[1] * tmp[2]; + } + } + return n1n2; +} + +static int part2(struct input *input, int width, int height) +{ + for (int line = 0; line < height; line++) { + for (int pixel = 0; pixel < width; ++pixel) { + char *pos = input->buf + line * width + pixel; + while (pos < input->buf + input->len && *pos == '2') + pos += width * height; + putchar(*pos == '0'? ' ': '#'); + } + putchar('\n'); + } + return 0; +} + +static int parse(struct input *input) +{ + size_t alloc = 0; + ssize_t buflen; + char *buf = NULL; + + if ((buflen = getline(&buf, &alloc, stdin)) <= 0) { + fprintf(stderr, "error reading file.\n"); + return 0; + } + buf[buflen--] = 0; + input->buf = buf; + input->len = buflen; + return buflen; +} + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part] [-i input]\n", prg); + return 1; +} + +int main(int ac, char **av) +{ + int opt, part = 1, width = 25, height = 6; + struct input input = { 0 }; + + while ((opt = getopt(ac, av, "d:p:w:h:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'w': + width = atoi(optarg); + break; + case 'h': + height = atoi(optarg); + break; + case 'p': /* 1 or 2 */ + part = atoi(optarg); + if (part < 1 || part > 2) + return usage(*av); + break; + default: + return usage(*av); + } + } + if (optind < ac) + return usage(*av); + + parse(&input); + printf("%s : res=%d\n", *av, + part == 1? + part1(&input, width, height) : + part2(&input, width, height)); + free(input.buf); + exit(0); +}