day 6 reduce state size
parent
67cd627138
commit
ece6ff1139
10
day06/sol.c
10
day06/sol.c
|
@ -144,20 +144,20 @@ int solve(struct grid *g) {
|
|||
}
|
||||
|
||||
// returns whether the chosen grid will cause the guard to get stuck in a loop
|
||||
bool loops(struct grid *g, struct point start, uint16_t* state) {
|
||||
bool loops(struct grid *g, struct point start, uint8_t* state) {
|
||||
int x = start.x;
|
||||
int y = start.y;
|
||||
int dx = 0, dy = -1; // up
|
||||
int stride = g->x;
|
||||
//printf("loops(%d,%d)\n", x,y);
|
||||
while(inbounds(g, x, y)) {
|
||||
uint16_t dir = 15;
|
||||
uint8_t dir = 15;
|
||||
if (dx == 1) { dir = 1; }
|
||||
else if (dx == -1) { dir = 2; }
|
||||
else if (dy == 1) { dir = 4; }
|
||||
else if (dy == -1) { dir = 8; }
|
||||
|
||||
uint16_t *s = &state[y*stride + x];
|
||||
uint8_t *s = &state[y*stride + x];
|
||||
if ((*s & dir) != 0) {
|
||||
// revisited a state, loop!
|
||||
return true;
|
||||
|
@ -183,8 +183,8 @@ int solve2(struct grid *g) {
|
|||
int count = 0;
|
||||
int ox, oy; // obstacle coords
|
||||
struct point p = startingPoint(g);
|
||||
size_t state_size = g->x * g->y * sizeof(uint16_t);
|
||||
uint16_t *state = xrealloc(NULL, state_size);
|
||||
size_t state_size = g->x * g->y * sizeof(uint8_t);
|
||||
uint8_t *state = xrealloc(NULL, state_size);
|
||||
for (oy = 0; oy < g->y; oy++)
|
||||
for (ox = 0; ox < g->x; ox++) {
|
||||
int c = g->A[oy][ox];
|
||||
|
|
Loading…
Reference in New Issue