Compare commits

...

2 Commits

Author SHA1 Message Date
d0376f21c3 2022 day 12 (C, parts 1 & 2) 2022-12-24 21:06:10 +01:00
04a856dc47 Makefile: clean keeps compile_commands.json 2022-12-24 21:05:33 +01:00
3 changed files with 188 additions and 5 deletions

View File

@@ -261,7 +261,15 @@ aoc.bash: res=408
time: 0:00.81 real, 0.80 user, 0.00 sys
context-switch: 223+1, page-faults: 0+486
aoc-c: res=408
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+170
+++++++++++++++++ part 2
aoc.bash: res=399
time: 0:00.57 real, 0.56 user, 0.00 sys
context-switch: 35+1, page-faults: 0+459
time: 0:00.55 real, 0.54 user, 0.01 sys
context-switch: 25+1, page-faults: 0+460
aoc-c: res=399
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+171

View File

@@ -28,7 +28,7 @@ export LD_LIBRARY_PATH = $(LIBDIR)
CFLAGS += -std=gnu11
CFLAGS += -O2
CFLAGS += -g
#CFLAGS += -g
# for gprof
# CFLAGS += -pg
CFLAGS += -Wall
@@ -50,7 +50,7 @@ VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all --track-origins=yes \
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 part1 part2 ccls bear org
.PHONY: clean cleanall all compile assembly memcheck memcheck1 memcheck2 part1 part2 ccls bear org
all: README.org ccls part1 part2
@@ -80,7 +80,10 @@ part2: aoc-c
ccls: $(CCLSFILE)
clean:
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html
cleanall: clean
@rm -f compile_commands.json
aoc-c: aoc-c.c common.c
@echo compiling $<

172
2022/day12/aoc-c.c Normal file
View File

@@ -0,0 +1,172 @@
/* aoc-c.c: Advent of Code 2022, day 12
*
* 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 "br.h"
#include "list.h"
#include "pool.h"
#include "aoc.h"
#define MAX_LINES 128 /* should be dynamic */
typedef struct coord {
int x, y;
} coord_t;
typedef struct square {
char height;
int dfs;
coord_t coord;
struct list_head queue;
} square_t;
typedef struct grid {
int nx, ny;
coord_t start, end;
square_t **map;
} grid_t;
pool_t *pool_lines;
LIST_HEAD(dfs);
#define CELL(g, x, y) (&(g)->map[y][x])
static inline void push(square_t *s)
{
list_add_tail(&s->queue, &dfs);
}
static inline square_t *pop()
{
square_t *s = list_first_entry_or_null(&dfs, square_t, queue);
if (s)
list_del(&s->queue);
return s;
}
static int parse(grid_t *grid)
{
size_t alloc = 0;
ssize_t buflen;
char *buf = NULL;
int line = 0;
square_t *pline;
while ((buflen = getline(&buf, &alloc, stdin)) > 0) {
buf[--buflen] = 0;
if (line == 0) {
pool_lines = pool_create("lines", 128, buflen * sizeof(square_t));
grid->nx = buflen;
}
grid->map[line] = pool_get(pool_lines);
pline = grid->map[line];
for (int i = 0; i < buflen; ++i) {
pline[i].coord.x = i;
pline[i].coord.y = line;
pline[i].dfs = 0;
if (buf[i] == 'S') {
grid->start.x = i;
grid->start.y = line;
pline[i].height = 'a';
} else if (buf[i] == 'E') {
grid->end.x = i;
grid->end.y = line;
pline[i].height = 'z';
} else {
pline[i].height = buf[i];
}
}
line++;
}
grid->ny = line;
free(buf);
return 1;
}
static square_t *dfs_add(grid_t *g, square_t *s, int n)
{
static coord_t dirs[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };
int x = s->coord.x + dirs[n].x, y = s->coord.y + dirs[n].y;
square_t *ret = NULL;
if (x >= 0 && y >= 0 && x < g->nx && y < g->ny) {
ret = CELL(g, x, y);
if (!ret->dfs)
return ret;
}
return NULL;
}
static int part1(grid_t *g)
{
square_t *s, *n;
s = CELL(g, g->start.x, g->start.y);
s->dfs = 1;
push(s);
while((s = pop())) {
//log_f(3, "");
for (int i = 0; i < 4; i++) {
if (!(n = dfs_add(g, s, i)))
continue;
if (n->height <= s->height+1) {
if (n->coord.x == g->end.x && n->coord.y == g->end.y)
return s->dfs;
n->dfs = s->dfs + 1;
push(n);
}
}
}
return -1;
}
static int part2(grid_t *g)
{
square_t *s, *n;
//int ret = 0;
s = CELL(g, g->end.x, g->end.y);
s->dfs = 1;
push(s);
while((s = pop())) {
//log_f(3, "");
for (int i = 0; i < 4; i++) {
if (!(n = dfs_add(g, s, i)))
continue;
if (n->height >= s->height - 1) {
if (n->height == 'a')
return s->dfs;
n->dfs = s->dfs + 1;
push(n);
}
}
}
return -1;
}
int main(int ac, char **av)
{
int part = parseargs(ac, av);
square_t *board[MAX_LINES];
grid_t grid = { 0, 0, { 0, 0 }, {0, 0}, board };
parse(&grid);
printf("%s: res=%d\n", *av, part == 1? part1(&grid): part2(&grid));
pool_destroy(pool_lines);
exit(0);
}