Compare commits

..

2 Commits

5 changed files with 264 additions and 665 deletions

View File

@@ -278,6 +278,18 @@ aoc-c : res=1162571910364852
time: 0:00.19 real, 0.18 user, 0.00 sys time: 0:00.19 real, 0.18 user, 0.00 sys
context-switch: 17+1, page-faults: 0+973 context-switch: 17+1, page-faults: 0+973
=========================================
================= day23 =================
=========================================
aoc-c : res=11120
time: 0:00.16 real, 0.14 user, 0.01 sys
context-switch: 34+1, page-faults: 0+4593
aoc-c : res=49232
time: 0:00.09 real, 0.08 user, 0.00 sys
context-switch: 3+1, page-faults: 0+3454
========================================= =========================================
================= day24 ================= ================= day24 =================
========================================= =========================================

View File

@@ -325,3 +325,7 @@ In this updated example, the least energy required to organize these amphipods i
######### #########
Using the initial configuration from the full diagram, what is the least energy required to organize the amphipods? Using the initial configuration from the full diagram, what is the least energy required to organize the amphipods?
Your puzzle answer was 49232.
Both parts of this puzzle are complete! They provide two gold stars: **

File diff suppressed because it is too large Load Diff

View File

@@ -24,23 +24,23 @@ LDLIB := -l$(LIB)
export LD_LIBRARY_PATH = $(LIBDIR) export LD_LIBRARY_PATH = $(LIBDIR)
CFLAGS += -std=gnu99 CFLAGS += -std=gnu99
CFLAGS += -O2 CFLAGS += -O2
CFLAGS += -g CFLAGS += -g
# for gprof # for gprof
#CFLAGS += -pg #CFLAGS += -pg
CFLAGS += -Wall CFLAGS += -Wall
CFLAGS += -Wextra CFLAGS += -Wextra
CFLAGS += -march=native CFLAGS += -march=native
# Next one may be useful for valgrind (some invalid instructions) # Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm # CFLAGS += -mno-tbm
CFLAGS += -Wmissing-declarations CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result CFLAGS += -Wno-unused-result
CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c) CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c)
CFLAGS += -DDEBUG_POOL # memory pools management CFLAGS += -DDEBUG_POOL # memory pools management
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH) export PATH := .:$(PATH)
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2 .PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2

View File

@@ -112,29 +112,57 @@ static int step(struct map *map)
} }
/** /**
* read-input() - read cuncumber map into memory. * map_release() - release a map structure memory.
* @map: A pointer on a map structure.
* *
*/ */
static void map_release(struct map *map)
{
if (map) {
if (map->cur)
free(map->cur);
if (map->next)
free(map->next);
free(map);
}
}
/**
* read_input() - read cuncumber map into memory.
*
* In case of success, the map structure and its cur and array components
* should be released for example by calling map_release().
*
* Return: a pointer to a map structure or NULL if failure.
*/
static struct map *read_input() static struct map *read_input()
{ {
size_t alloc = 0; size_t alloc = 0;
ssize_t buflen; ssize_t buflen;
struct map *map=malloc(sizeof(struct map)); struct map *map;
if (map) { /* use calloc() to ensure cur & next are set to NULL */
map->cur = NULL; if (!(map = calloc(1, sizeof(struct map))))
/* read whole input, we will keep '\n' and avoit useless splitting */ goto end;
buflen = getdelim(&map->cur, &alloc, '\0', stdin);
map->next = strdup(map->cur);
map->ncols = strchr(map->cur, '\n') - map->cur;
/* we suppose there is nothing after the last input data last line
* Therefore last char of input is '\n', at position (bufflen - 1)
*/
map->nrows = (buflen - 1) / map->ncols;
log(2, "buflen=%ld ncols=%d nrows=%d lastnl=%ld\n", buflen, map->ncols, /* read whole input, we will keep '\n' and avoid useless '\0' splitting */
map->nrows, strrchr(map->cur, '\n') - map->cur); if ((buflen = getdelim(&map->cur, &alloc, '\0', stdin)) < 0)
} goto freemem;
if (!(map->next = strdup(map->cur)))
goto freemem;
map->ncols = strchr(map->cur, '\n') - map->cur;
/* next line works if there is nothing after the last input data last line,
* i.e. if last char of input (at position bufflen - 1) is the '\n' on the
* last valid puzzle line.
*/
map->nrows = (buflen - 1) / map->ncols;
log(2, "buflen=%ld ncols=%d nrows=%d lastnl=%ld\n", buflen, map->ncols,
map->nrows, strrchr(map->cur, '\n') - map->cur);
goto end;
freemem:
map_release(map);
end:
return map; return map;
} }
@@ -173,9 +201,10 @@ int main(int ac, char **av)
log(2, "+++ after step %d\n", cur); log(2, "+++ after step %d\n", cur);
print_map(map, 0); print_map(map, 0);
} }
printf("%s : res=%d\n", *av, cur);
map_release(map);
} }
printf("%s : res=%d\n", *av, cur);
exit(0); exit(0);
} }