From 31a255a9acb72c85450c2a2f7d7bf62787648a15 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Mon, 12 Dec 2022 20:46:09 +0100 Subject: [PATCH] 2020 day 8 (C, both parts. No inspiration here) --- 2022/RESULTS.txt | 10 ++- 2022/day08/aoc-c.c | 159 ++++++++++++++++++++++++++++++++++++++++++++ 2022/day08/aoc.bash | 10 +-- 3 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 2022/day08/aoc-c.c diff --git a/2022/RESULTS.txt b/2022/RESULTS.txt index b543896..87d3ff2 100644 --- a/2022/RESULTS.txt +++ b/2022/RESULTS.txt @@ -153,7 +153,7 @@ aoc-c: res=7490863 context-switch: 0+1, page-faults: 0+98 ========================================= -================= day07 ================= +================= day08 ================= ========================================= +++++++++++++++++ part 1 @@ -161,7 +161,15 @@ aoc.bash: res=1698 time: 0:01.10 real, 1.10 user, 0.00 sys context-switch: 5+1, page-faults: 0+319 +aoc-c: res=1698 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+92 + +++++++++++++++++ part 2 aoc.bash: res=672280 time: 0:03.54 real, 3.53 user, 0.00 sys context-switch: 48+1, page-faults: 0+284 + +aoc-c: res=672280 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+91 diff --git a/2022/day08/aoc-c.c b/2022/day08/aoc-c.c new file mode 100644 index 0000000..efe2211 --- /dev/null +++ b/2022/day08/aoc-c.c @@ -0,0 +1,159 @@ +/* aoc-c.c: Advent of Code 2022, day 8 + * + * 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 "bits.h" +#include "aoc.h" + +typedef struct tree { + u16 size; + char *h; /* heigts */ + char *v; /* visible (part 1) */ +} tree_t; + +#define HEIGHT(t, x, y) (((t)->h[((t)->size * (y)) + (x)])) +#define VISIBLE(t, x, y) (((t)->v[((t)->size * (y)) + (x)])) + +static tree_t *parse(tree_t *trees) +{ + size_t alloc = 0; + char *buf = NULL; + ssize_t buflen; + + buflen = getline(&buf, &alloc, stdin); + buf[--buflen] = 0; + trees->size = buflen; + trees->h = malloc((buflen * buflen) * sizeof(char) + 1); + memcpy(trees->h, buf, buflen); /* store first line */ + free(buf); + + for (buf = trees->h + buflen; ; buf += buflen) { + if (scanf("%s", buf) <= 0) + break; + } + trees->v = calloc(buflen * buflen, sizeof (char)); + return trees; +} + +static int visible(tree_t *t, int *max, int x, int y) +{ + int h = HEIGHT(t, x, y); + if (h > *max) { + VISIBLE(t, x, y) = 1; + if ((*max = h) == '9') { + return 1; + } + } + return 0; +} + +static int check_tree(tree_t *t, int x, int y) +{ + int i, h = HEIGHT(t, x, y), res = 1, size = t->size, tmp; + + for (tmp = 0, i = x + 1; i < size ; ++i) { /* east */ + tmp++; + if (HEIGHT(t, i, y) >= h) + break; + } + res *= tmp; + for (tmp = 0, i = x - 1; i >= 0; --i) { /* west */ + tmp++; + if (HEIGHT(t, i, y) >= h) + break; + } + res *= tmp; + for (tmp = 0, i = y + 1; i < size; ++i) { /* south */ + tmp++; + if (HEIGHT(t, x, i) >= h) + break; + } + res *= tmp; + for (tmp = 0, i = y - 1; i >= 0; --i) { /* north */ + tmp++; + if (HEIGHT(t, x, i) >= h) + break; + } + res *= tmp; + return res; +} + +static int part1(tree_t *t) +{ + int x, y, max, res = 0, size = t->size; + + for (y = 1; y < size -1; ++y) { /* to east */ + if ((max = HEIGHT(t, 0, y)) < '9') { + for (x = 1; x < size -1; ++x) { + if (visible (t, &max, x, y)) + break; + } + } + } + for (y = 1; y < size -1; ++y) { /* to west */ + if ((max = HEIGHT(t, size - 1, y)) < '9') { + for (x = size - 2; x > 0; --x) { + if (visible (t, &max, x, y)) { + break; + } + } + } + } + for (x = 1; x < size -1; ++x) { /* to south */ + if ((max = HEIGHT(t, x, 0)) < '9') { + for (y = 1; y < size -1; ++y) { + if (visible (t, &max, x, y)) + break; + } + } + } + for (x = 1; x < size -1; ++x) { /* to north */ + if ((max = HEIGHT(t, x, size - 1)) < '9') { + for (y = size - 2; y > 0; --y) { + if (visible (t, &max, x, y)) + break; + } + } + } + for (y = 0; y < size; ++y) + for (x = 0; x < size; ++x) + if (VISIBLE(t, x, y)) + res++; + return res + size * 4 - 4; +} + +static int part2(tree_t *t) +{ + int res = 0, tmp, size = t->size; + + for (int y = 1; y < size - 1; ++y) { + for (int x = 1; x < size - 1; ++x) { + tmp = check_tree(t, x, y); + if (tmp > res) + res = tmp; + } + } + return res; +} + +int main(int ac, char **av) +{ + int part = parseargs(ac, av); + tree_t trees; + + parse(&trees); + printf("%s: res=%d\n", *av, part == 1? part1(&trees): part2(&trees)); + exit(0); +} diff --git a/2022/day08/aoc.bash b/2022/day08/aoc.bash index c16ce24..7fb4034 100755 --- a/2022/day08/aoc.bash +++ b/2022/day08/aoc.bash @@ -45,30 +45,30 @@ check_visible() { part1() { declare -ig res - local -i x y max c + local -i x y max - for ((y = 1; y < size -1; ++y)); do # left to right + for ((y = 1; y < size -1; ++y)); do # to east height max 0 "$y" (( max == 9 )) && continue for ((x = 1; x < size -1; ++x)); do check_visible max "$x" "$y" || break done done - for ((y = 1; y < size -1; ++y)); do # right to left + for ((y = 1; y < size -1; ++y)); do # to west height max $((size - 1)) "$y" (( max == 9 )) && continue for ((x = size - 2; x > 0; --x)); do check_visible max "$x" "$y" || break done done - for ((x = 1; x < size -1; ++x)); do # top to bottom + for ((x = 1; x < size -1; ++x)); do # to south height max "$x" 0 (( max == 9 )) && continue for ((y = 1; y < size -1; ++y)); do check_visible max "$x" "$y" || break done done - for ((x = 1; x < size -1; ++x)); do # bottom to top + for ((x = 1; x < size -1; ++x)); do # to north height max "$x" $((size - 1)) (( max == 9 )) && continue for ((y = size - 2; y > 0; --y)); do