day 25: simplify code.
This commit is contained in:
@@ -294,7 +294,6 @@ aoc-c : res=31521119151421
|
|||||||
================= day25 =================
|
================= day25 =================
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
+++++++++++++++++ part 1
|
|
||||||
aoc-c : res=412
|
aoc-c : res=412
|
||||||
time: 0:00.06 real, 0.06 user, 0.00 sys
|
time: 0:00.05 real, 0.05 user, 0.00 sys
|
||||||
context-switch: 16+1, page-faults: 0+99
|
context-switch: 0+1, page-faults: 0+97
|
||||||
|
@@ -47,7 +47,7 @@ export PATH := .:$(PATH)
|
|||||||
|
|
||||||
all: ex1
|
all: ex1
|
||||||
|
|
||||||
memcheck: memcheck1 memcheck2
|
memcheck: memcheck1
|
||||||
|
|
||||||
memcheck1:
|
memcheck1:
|
||||||
@valgrind -q -s --track-origins=yes aoc-c -p 1 < $(INPUT)
|
@valgrind -q -s --track-origins=yes aoc-c -p 1 < $(INPUT)
|
||||||
|
@@ -10,129 +10,101 @@
|
|||||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Warning: Work in progress. Should not work before I spend a lot of time
|
|
||||||
* on it, the original "bitboard" approach for moves generation is pretty
|
|
||||||
* difficult to program/debug
|
|
||||||
*/
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "pool.h"
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "bits.h"
|
|
||||||
#include "list.h"
|
|
||||||
|
|
||||||
|
|
||||||
struct map {
|
struct map {
|
||||||
int ncols, nrows;
|
int ncols, nrows;
|
||||||
char *map, *next;
|
char *cur, *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline char *mapline(struct map *m, int row)
|
/**
|
||||||
|
* mapc - gives address of cuncumber at position (row, col)
|
||||||
|
* @ptr: char array
|
||||||
|
* @ncols: number of chars in one row
|
||||||
|
* @row: cuncumber row
|
||||||
|
* @col: cuncumber column
|
||||||
|
*
|
||||||
|
* @return: pointer to cuncumber position
|
||||||
|
*/
|
||||||
|
static inline char *mapc(char *p, int ncols, int row, int col)
|
||||||
{
|
{
|
||||||
return m->map + row * m->ncols + row;
|
return p + row * (ncols + 1) + col;
|
||||||
}
|
|
||||||
static inline char *nextline(struct map *m, int row)
|
|
||||||
{
|
|
||||||
return m->next + row * m->ncols + row;
|
|
||||||
}
|
|
||||||
static inline char *mapc(struct map *m, int row, int col)
|
|
||||||
{
|
|
||||||
return mapline(m, row) + col;
|
|
||||||
}
|
|
||||||
static inline char* nextc(struct map *m, int row, int col)
|
|
||||||
{
|
|
||||||
return nextline(m, row) + col;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
static void print_map(struct map *map)
|
* static void print_map(struct map *map)
|
||||||
|
* {
|
||||||
|
* printf("map: cols=%d rows=%d\ncur:\n%snext:\n%s",
|
||||||
|
* map->ncols, map->nrows, map->cur, map->next);
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
|
||||||
|
static inline int maybe_move(char *cur, char *next, int ncols,
|
||||||
|
char dir,
|
||||||
|
int r, int c, int r1, int c1)
|
||||||
{
|
{
|
||||||
int i;
|
int ret = 0;
|
||||||
printf ("map: cols=%d rows=%d\ncur:\n", map->ncols, map->nrows);
|
if (*mapc(cur, ncols, r, c) == dir && *mapc(cur, ncols, r1, c1) == '.') {
|
||||||
for (i = 0; i < map->nrows; ++i)
|
*mapc(next, ncols, r, c) = '.';
|
||||||
puts(mapline(map, i));
|
*mapc(next, ncols, r1, c1) = dir;
|
||||||
printf ("next:\n");
|
ret = 1;
|
||||||
for (i = 0; i < map->nrows; ++i)
|
} else {
|
||||||
puts(nextline(map, i));
|
*mapc(next, ncols, r, c) = *mapc(cur, ncols, r, c);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
static int step(struct map *map)
|
static int step(struct map *map)
|
||||||
{
|
{
|
||||||
int r, c, mod = map->ncols, moves=0;
|
int moves = 0, ncols = map->ncols, nrows = map->nrows;
|
||||||
char *tmp;
|
char *cur = map->cur, *next = map->next;
|
||||||
|
|
||||||
/* move right */
|
/* move east */
|
||||||
for (r = 0; r < map->nrows; r++) {
|
for (int r = 0; r < nrows; r++) {
|
||||||
for (c = 0; c < map->ncols; ++c) {
|
for (int c = 0; c < ncols; ++c) {
|
||||||
int next = (c + 1) % mod;
|
if (maybe_move(cur, next, ncols, '>', r, c, r, (c + 1) % ncols)) {
|
||||||
if (*mapc(map, r, c) == '>' && *mapc(map, r, next) == '.') {
|
|
||||||
*nextc(map, r, c) = '.';
|
|
||||||
*nextc(map, r, next) = '>';
|
|
||||||
moves++;
|
moves++;
|
||||||
c++;
|
c++;
|
||||||
} else {
|
|
||||||
*nextc(map, r, c) = *mapc(map, r, c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmp = map->map;
|
/* move south - here we move data from next to cur */
|
||||||
map->map = map->next;
|
for (int c = 0; c < ncols; ++c) {
|
||||||
map->next = tmp;
|
for (int r = 0; r < nrows; r++) {
|
||||||
|
if (maybe_move(next, cur, ncols, 'v', r, c, (r + 1) % nrows, c)) {
|
||||||
/* move down */
|
|
||||||
mod = map->nrows;
|
|
||||||
for (c = 0; c < map->ncols; ++c) {
|
|
||||||
for (r = 0; r < map->nrows; r++) {
|
|
||||||
int next = (r + 1) % mod;
|
|
||||||
if (*mapc(map, r, c) == 'v' && *mapc(map, next, c) == '.') {
|
|
||||||
*nextc(map, r, c) = '.';
|
|
||||||
*nextc(map, next, c) = 'v';
|
|
||||||
moves++;
|
moves++;
|
||||||
r++;
|
r++;
|
||||||
} else {
|
|
||||||
*nextc(map, r, c) = *mapc(map, r, c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmp = map->map;
|
|
||||||
map->map = map->next;
|
|
||||||
map->next = tmp;
|
|
||||||
//print_map(map);
|
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* minimal parsing: We just read the 3-5 lines to get
|
/**
|
||||||
* the amphipods location in side rooms
|
* read-input() - read cuncumber map into memory.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
static void read_input(struct map *map)
|
static struct map *read_input()
|
||||||
{
|
{
|
||||||
size_t alloc = 0;
|
size_t alloc = 0;
|
||||||
ssize_t buflen;
|
ssize_t buflen;
|
||||||
char *buf = NULL;
|
struct map *map=malloc(sizeof(struct map));
|
||||||
int line = 0;
|
|
||||||
|
|
||||||
buflen = getline(&buf, &alloc, stdin);
|
if (map) {
|
||||||
buf[buflen - 1] = 0;
|
map->cur = NULL;
|
||||||
map->map = malloc(buflen * buflen);
|
/* read whole input */
|
||||||
map->next = malloc(buflen * buflen);
|
buflen = getdelim(&map->cur, &alloc, '\0', stdin);
|
||||||
map->ncols = buflen - 1;
|
map->next = strdup(map->cur);
|
||||||
strcpy(map->map, buf);
|
map->ncols = strchr(map->cur, '\n') - map->cur;
|
||||||
strcpy(map->next, buf);
|
map->nrows = buflen / (map->ncols + 1);
|
||||||
line++;
|
|
||||||
free(buf);
|
|
||||||
buf = mapline(map, line);
|
|
||||||
while (fgets(buf, buflen + 1, stdin)) {
|
|
||||||
buf[buflen - 1] = 0;
|
|
||||||
strcpy(nextline(map, line), buf);
|
|
||||||
line++;
|
|
||||||
buf = mapline(map, line);
|
|
||||||
}
|
}
|
||||||
map->nrows = line;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int usage(char *prg)
|
static int usage(char *prg)
|
||||||
@@ -144,8 +116,6 @@ static int usage(char *prg)
|
|||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
int opt, part = 1, moves, cur=1;
|
int opt, part = 1, moves, cur=1;
|
||||||
struct map map = { 0, 0, NULL, NULL};
|
|
||||||
|
|
||||||
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'd':
|
case 'd':
|
||||||
@@ -163,10 +133,10 @@ int main(int ac, char **av)
|
|||||||
if (optind < ac)
|
if (optind < ac)
|
||||||
return usage(*av);
|
return usage(*av);
|
||||||
|
|
||||||
read_input(&map);
|
struct map *map = read_input();
|
||||||
while ((moves = step(&map))) {
|
if (map)
|
||||||
cur++;
|
while ((moves = step(map)))
|
||||||
}
|
cur++;
|
||||||
|
|
||||||
printf("%s : res=%d\n", *av, cur);
|
printf("%s : res=%d\n", *av, cur);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user