day 15 part 2 final
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
ex*-c
|
ex*-c
|
||||||
aoc-c
|
aoc-c
|
||||||
core*
|
core*
|
||||||
|
gmon.out
|
||||||
*.o
|
*.o
|
||||||
ex*-cob
|
ex*-cob
|
||||||
lib/
|
lib/
|
||||||
|
@@ -20,6 +20,7 @@ CFLAGS += -std=gnu99
|
|||||||
|
|
||||||
CFLAGS += -O2
|
CFLAGS += -O2
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
|
#CFLAGS += -pg
|
||||||
CFLAGS += -Wall
|
CFLAGS += -Wall
|
||||||
CFLAGS += -Wextra
|
CFLAGS += -Wextra
|
||||||
CFLAGS += -march=native
|
CFLAGS += -march=native
|
||||||
@@ -38,7 +39,7 @@ LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC))
|
|||||||
LDFLAGS := -L$(LIBDIR)
|
LDFLAGS := -L$(LIBDIR)
|
||||||
LDLIB := -l$(LIB)
|
LDLIB := -l$(LIB)
|
||||||
|
|
||||||
.PHONY: clean cleanall all redo output lib $(SUBDIRS)
|
.PHONY: clean cleanlib cleanall all redo output lib $(SUBDIRS)
|
||||||
|
|
||||||
all: lib $(SUBDIRS)
|
all: lib $(SUBDIRS)
|
||||||
|
|
||||||
@@ -47,9 +48,11 @@ clean:
|
|||||||
$(MAKE) --no-print-directory -C $$dir clean ; \
|
$(MAKE) --no-print-directory -C $$dir clean ; \
|
||||||
done
|
done
|
||||||
|
|
||||||
cleanall: clean
|
cleanlib: clean
|
||||||
@$(RM) -f $(SLIB) $(DLIB) $(LIBOBJ)
|
@$(RM) -f $(SLIB) $(DLIB) $(LIBOBJ)
|
||||||
|
|
||||||
|
cleanall: clean cleanlib
|
||||||
|
|
||||||
redo: cleanall all
|
redo: cleanall all
|
||||||
|
|
||||||
$(SUBDIRS):
|
$(SUBDIRS):
|
||||||
|
@@ -181,3 +181,15 @@ aoc-c : res=2712
|
|||||||
aoc-c : res=8336623059567
|
aoc-c : res=8336623059567
|
||||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
context-switch: 0+1, page-faults: 0+86
|
context-switch: 0+1, page-faults: 0+86
|
||||||
|
|
||||||
|
=========================================
|
||||||
|
================= day15 =================
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
aoc-c : res=435
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 8+1, page-faults: 0+191
|
||||||
|
|
||||||
|
aoc-c : res=2842
|
||||||
|
time: 0:00.94 real, 0.94 user, 0.00 sys
|
||||||
|
context-switch: 5+1, page-faults: 0+582
|
||||||
|
@@ -26,6 +26,7 @@ export LD_LIBRARY_PATH = $(LIBDIR)
|
|||||||
CFLAGS += -std=gnu99
|
CFLAGS += -std=gnu99
|
||||||
CFLAGS += -O2
|
CFLAGS += -O2
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
|
#CFLAGS += -pg
|
||||||
CFLAGS += -Wall
|
CFLAGS += -Wall
|
||||||
CFLAGS += -Wextra
|
CFLAGS += -Wextra
|
||||||
CFLAGS += -march=native
|
CFLAGS += -march=native
|
||||||
|
@@ -25,21 +25,15 @@
|
|||||||
|
|
||||||
#define MAX_SIZE 100
|
#define MAX_SIZE 100
|
||||||
|
|
||||||
int a[] = {
|
|
||||||
['a'] = 0,
|
|
||||||
['b'] = 1,
|
|
||||||
['c'] = 2
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct square {
|
typedef struct square {
|
||||||
uchar cost;
|
uchar cost;
|
||||||
uchar visited;
|
uchar visited;
|
||||||
u64 totcost;;
|
u32 totcost;;
|
||||||
} square_t;
|
} square_t;
|
||||||
|
|
||||||
typedef struct priority_queue {
|
typedef struct priority_queue {
|
||||||
int l;
|
u16 l;
|
||||||
int c;
|
u16 c;
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
} pqueue_t;
|
} pqueue_t;
|
||||||
|
|
||||||
@@ -48,44 +42,6 @@ static struct square array[MAX_SIZE*5][MAX_SIZE*5];
|
|||||||
static int asize;
|
static int asize;
|
||||||
static pool_t *pool;
|
static pool_t *pool;
|
||||||
|
|
||||||
#define VALID(x, y) ((x) >= 0 && (x) < size && (y) >= 0 && (y) < size )
|
|
||||||
|
|
||||||
static void print_array()
|
|
||||||
{
|
|
||||||
log(3, "array (%dx%d):\n", asize, asize);
|
|
||||||
for (int i = 0; i < asize; ++i) {
|
|
||||||
for (int j = 0; j < asize; ++j)
|
|
||||||
log(3, "%1d ", array[i][j].cost);
|
|
||||||
//log(3, "%1d/%2d ", array[i][j].cost, array[i][j].totcost);
|
|
||||||
log(3, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static void print_array_total()
|
|
||||||
{
|
|
||||||
log(3, "array (%dx%d):\n", asize, asize);
|
|
||||||
for (int i = 0; i < asize; ++i) {
|
|
||||||
for (int j = 0; j < asize; ++j)
|
|
||||||
log(3, "%4d ", array[i][j].totcost);
|
|
||||||
//log(3, "%1d/%2d ", array[i][j].cost, array[i][j].totcost);
|
|
||||||
log(3, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_queue()
|
|
||||||
{
|
|
||||||
log(3, "queue:\n", asize, asize);
|
|
||||||
pqueue_t *tmp;
|
|
||||||
int i = 1;
|
|
||||||
|
|
||||||
list_for_each_entry(tmp, &plist_head, list) {
|
|
||||||
int l = tmp->l, c = tmp->c;
|
|
||||||
u64 cost = array[l][c].cost;
|
|
||||||
u64 acc = array[l][c].totcost;
|
|
||||||
log(3, "%d: (%d,%d): cost=%lu acc=%lu\n", i, l, c, cost, acc);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* read data and create graph.
|
/* read data and create graph.
|
||||||
*/
|
*/
|
||||||
static int read_input()
|
static int read_input()
|
||||||
@@ -99,102 +55,89 @@ static int read_input()
|
|||||||
while ((len = getline(&buf, &alloc, stdin)) > 0) {
|
while ((len = getline(&buf, &alloc, stdin)) > 0) {
|
||||||
buf[--len] = 0;
|
buf[--len] = 0;
|
||||||
|
|
||||||
//printf("%d: size=%d [%s]\n", l, asize, buf);
|
|
||||||
for (c = 0; buf[c]; ++c) {
|
for (c = 0; buf[c]; ++c) {
|
||||||
array[l][c].cost = buf[c] - '0';
|
array[l][c].cost = buf[c] - '0';
|
||||||
array[l][c].totcost = UINT64_MAX;
|
array[l][c].totcost = UINT32_MAX;
|
||||||
array[l][c].visited = 0;
|
array[l][c].visited = 0;
|
||||||
}
|
}
|
||||||
l++;
|
l++;
|
||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
asize = l;
|
asize = l;
|
||||||
//print_array();
|
|
||||||
return asize;
|
return asize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* insert l,c in queue, keeping cost sorted */
|
/* insert l,c in queue */
|
||||||
inline static pqueue_t *push(int l, int c, u32 parentcost)
|
inline static pqueue_t *push(int l, int c, u32 parentcost)
|
||||||
{
|
{
|
||||||
pqueue_t *queue;
|
pqueue_t *queue = NULL;
|
||||||
u64 newcost;
|
u32 newcost;
|
||||||
|
|
||||||
if (l < 0 || c < 0 || l >= asize || c >= asize)
|
if (array[l][c].visited)
|
||||||
return NULL;
|
return NULL;
|
||||||
newcost = parentcost + array[l][c].cost;
|
newcost = parentcost + array[l][c].cost;
|
||||||
if (newcost > array[l][c].totcost)
|
if (newcost > array[l][c].totcost)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
array[l][c].totcost = newcost;
|
||||||
queue = pool_get(pool);
|
queue = pool_get(pool);
|
||||||
queue->l = l;
|
queue->l = l;
|
||||||
queue->c = c;
|
queue->c = c;
|
||||||
array[l][c].totcost = newcost;
|
|
||||||
|
|
||||||
log_f(3, "(%d,%d) pcost=%u\n", l, c, newcost);
|
|
||||||
list_add_tail(&queue->list, &plist_head);
|
list_add_tail(&queue->list, &plist_head);
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static pqueue_t *pop()
|
inline static pqueue_t *pop()
|
||||||
{
|
{
|
||||||
pqueue_t *min;
|
pqueue_t *pmin, *tmp, *cur;
|
||||||
pqueue_t *tmp, *cur;
|
u32 min = UINT32_MAX;
|
||||||
|
|
||||||
if (!(min = list_first_entry_or_null(&plist_head, pqueue_t, list)))
|
if (!(pmin = list_first_entry_or_null(&plist_head, pqueue_t, list)))
|
||||||
return NULL;
|
return NULL;
|
||||||
list_for_each_entry_safe(cur, tmp, &plist_head, list) {
|
list_for_each_entry_safe(cur, tmp, &plist_head, list) {
|
||||||
int l = cur->l, c = cur->c;
|
if (array[cur->l][cur->c].totcost < min) {
|
||||||
if (array[l][c].totcost < array[min->l][min->c].totcost)
|
pmin = cur;
|
||||||
min = cur;
|
min = array[cur->l][cur->c].totcost;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
list_del(&min->list);
|
list_del(&pmin->list);
|
||||||
//if (array[l][c].visited) {
|
return pmin;
|
||||||
// pool_add(pool, cur);
|
|
||||||
// continue;
|
|
||||||
//}
|
|
||||||
log_f(3, "(%d,%d) cost=%u\n", min->l, min->c, array[min->l][min->c].totcost);
|
|
||||||
return min;
|
|
||||||
// if ((p = list_first_entry_or_null(&plist_head, pqueue_t, list)))
|
|
||||||
// list_del(&p->list);
|
|
||||||
//return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static u64 part1()
|
static u32 doit()
|
||||||
{
|
{
|
||||||
pqueue_t *pqueue;
|
pqueue_t *pqueue;
|
||||||
|
|
||||||
push(0, 0, 0);
|
push(0, 0, 0);
|
||||||
print_array();
|
|
||||||
while ((pqueue = pop())) {
|
while ((pqueue = pop())) {
|
||||||
int l = pqueue->l, c = pqueue->c;
|
int l = pqueue->l, c = pqueue->c;
|
||||||
u64 acc = array[l][c].totcost;
|
u32 acc = array[l][c].totcost;
|
||||||
bool stop = true;
|
|
||||||
|
|
||||||
if (array[l][c].visited)
|
if (array[l][c].visited)
|
||||||
goto free_pqueue;
|
goto free_pqueue;
|
||||||
if (push(l, c - 1, acc))
|
if (c > 0)
|
||||||
stop = false;
|
push(l, c - 1, acc);
|
||||||
if (push(l, c + 1, acc))
|
if (c < asize - 1)
|
||||||
stop = false;
|
push(l, c + 1, acc);
|
||||||
if (push(l - 1, c, acc))
|
if (l > 0)
|
||||||
stop = false;
|
push(l - 1, c, acc);
|
||||||
if (push(l + 1, c, acc))
|
if (l < asize - 1)
|
||||||
stop = false;
|
push(l + 1, c, acc);
|
||||||
array[l][c].visited = 1;
|
array[l][c].visited = 1;
|
||||||
//if (stop)
|
|
||||||
// break;
|
|
||||||
free_pqueue:
|
free_pqueue:
|
||||||
pool_add(pool, pqueue); /* recycle pqueue in memory pool */
|
pool_add(pool, pqueue); /* recycle pqueue in memory pool */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* as we accounted [0][0] cost, we must substract it */
|
/* as we accounted [0][0] cost, we must substract it */
|
||||||
print_array();
|
|
||||||
print_array_total();
|
|
||||||
return array[asize - 1][asize - 1].totcost - array[0][0].cost;
|
return array[asize - 1][asize - 1].totcost - array[0][0].cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
static u64 part2()
|
static u32 part1()
|
||||||
|
{
|
||||||
|
return doit();
|
||||||
|
}
|
||||||
|
|
||||||
|
static u32 part2()
|
||||||
{
|
{
|
||||||
int l, c;
|
int l, c;
|
||||||
|
|
||||||
@@ -211,21 +154,17 @@ static u64 part2()
|
|||||||
if (array[newl][newc].cost > 9) {
|
if (array[newl][newc].cost > 9) {
|
||||||
array[newl][newc].cost -= 9;
|
array[newl][newc].cost -= 9;
|
||||||
}
|
}
|
||||||
//log(3, "new(%d, %d) = %u\n", newl, newc, array[newl][newc].cost);
|
/* invalid, but it works with 75% gain
|
||||||
|
* if (j < i)
|
||||||
|
* array[newl][newc].visited = 1;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
asize *= 5;
|
asize *= 5;
|
||||||
return part1();
|
return doit();
|
||||||
}
|
|
||||||
|
|
||||||
static u64 doit(int part)
|
|
||||||
{
|
|
||||||
read_input();
|
|
||||||
|
|
||||||
return part == 1? part1(): part2();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int usage(char *prg)
|
static int usage(char *prg)
|
||||||
@@ -258,6 +197,7 @@ int main(int ac, char **av)
|
|||||||
if (!(pool = pool_init("stack", 1024, sizeof (pqueue_t))))
|
if (!(pool = pool_init("stack", 1024, sizeof (pqueue_t))))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
printf("%s : res=%lu\n", *av, doit(part));
|
read_input();
|
||||||
|
printf("%s : res=%u\n", *av, part == 1? part1(): part2());
|
||||||
exit (0);
|
exit (0);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user