2020 day 8 (C, both parts. No inspiration here)
This commit is contained in:
@@ -153,7 +153,7 @@ aoc-c: res=7490863
|
|||||||
context-switch: 0+1, page-faults: 0+98
|
context-switch: 0+1, page-faults: 0+98
|
||||||
|
|
||||||
=========================================
|
=========================================
|
||||||
================= day07 =================
|
================= day08 =================
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
+++++++++++++++++ part 1
|
+++++++++++++++++ part 1
|
||||||
@@ -161,7 +161,15 @@ aoc.bash: res=1698
|
|||||||
time: 0:01.10 real, 1.10 user, 0.00 sys
|
time: 0:01.10 real, 1.10 user, 0.00 sys
|
||||||
context-switch: 5+1, page-faults: 0+319
|
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
|
+++++++++++++++++ part 2
|
||||||
aoc.bash: res=672280
|
aoc.bash: res=672280
|
||||||
time: 0:03.54 real, 3.53 user, 0.00 sys
|
time: 0:03.54 real, 3.53 user, 0.00 sys
|
||||||
context-switch: 48+1, page-faults: 0+284
|
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
|
||||||
|
159
2022/day08/aoc-c.c
Normal file
159
2022/day08/aoc-c.c
Normal file
@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
@@ -45,30 +45,30 @@ check_visible() {
|
|||||||
|
|
||||||
part1() {
|
part1() {
|
||||||
declare -ig res
|
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"
|
height max 0 "$y"
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((x = 1; x < size -1; ++x)); do
|
for ((x = 1; x < size -1; ++x)); do
|
||||||
check_visible max "$x" "$y" || break
|
check_visible max "$x" "$y" || break
|
||||||
done
|
done
|
||||||
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"
|
height max $((size - 1)) "$y"
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((x = size - 2; x > 0; --x)); do
|
for ((x = size - 2; x > 0; --x)); do
|
||||||
check_visible max "$x" "$y" || break
|
check_visible max "$x" "$y" || break
|
||||||
done
|
done
|
||||||
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
|
height max "$x" 0
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((y = 1; y < size -1; ++y)); do
|
for ((y = 1; y < size -1; ++y)); do
|
||||||
check_visible max "$x" "$y" || break
|
check_visible max "$x" "$y" || break
|
||||||
done
|
done
|
||||||
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))
|
height max "$x" $((size - 1))
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((y = size - 2; y > 0; --y)); do
|
for ((y = size - 2; y > 0; --y)); do
|
||||||
|
Reference in New Issue
Block a user