diff --git a/day11/Makefile b/day11/Makefile index dc3fdbf..ea10e0a 100644 --- a/day11/Makefile +++ b/day11/Makefile @@ -17,6 +17,9 @@ STRIP=strip .PHONY: all all: part1 part2 +.PHONY: test +test: part1-test part2-test + inpu%-sz.h: inpu% echo "#define input_sz (`head -n 1 $< | wc -m`)" > $@ @@ -33,7 +36,13 @@ part1: day11.c | input.h part1-test: day11.c | input-test.h $(CC) $< -DTEST $(CFLAGS) -o $@ $(LDFLAGS) + +part2: day11.c | input.h + $(CC) $< -DPART2 $(CFLAGS) -o $@ $(LDFLAGS) $(STRIP) $@ + +part2-test: day11.c | input-test.h + $(CC) $< -DTEST -DPART2 $(CFLAGS) -o $@ $(LDFLAGS) clean: rm -f input{,-sz}{,-test}.h rm -f part{1,2}{,-test} diff --git a/day11/day11.c b/day11/day11.c index 8e71b17..44323ec 100644 --- a/day11/day11.c +++ b/day11/day11.c @@ -55,15 +55,20 @@ pure gol_t generate_arena() return gol; } +noglobal static inline int gol_in_bounds(int x, int y) +{ + return !(x<0 || x >= (int)INPUT_WIDTH || y<0 || y >= (int)INPUT_HEIGHT); +} + static inline pure enum gol_state gol_index(const gol_t* gol, int x, int y) { - if (x<0 || x >= (int)INPUT_WIDTH || y<0 || y >= (int)INPUT_HEIGHT) return STATE_FLOOR; + if (!gol_in_bounds(x,y)) return STATE_FLOOR; return gol->arena[GOL_INDEX(x,y)]; } static inline void gol_set(gol_t* restrict gol, int x,int y, enum gol_state st) { - if (x<0 || x >= (int)INPUT_WIDTH || y<0 || y >= (int)INPUT_HEIGHT) return; + if (!gol_in_bounds(x,y)) return; gol->arena[GOL_INDEX(x,y)] = st; } @@ -81,19 +86,92 @@ static inline int gol_count(const gol_t* gol, enum gol_state s) return count_states(gol->arena, s, INPUT_WIDTH*INPUT_HEIGHT); } -static inline void neighbours(const gol_t* gol, int x, int y, enum gol_state out[restrict 8]) +static inline int nneighbours(const gol_t* gol, int n, int x, int y, enum gol_state out[restrict 8]) { - out[0] = gol_index(gol, x-1, y-1); - out[1] = gol_index(gol, x, y-1); - out[2] = gol_index(gol, x+1, y-1); + out[0] = gol_index(gol, x-n, y-n); + out[1] = gol_index(gol, x, y-n); + out[2] = gol_index(gol, x+n, y-n); - out[3] = gol_index(gol, x-1, y); + out[3] = gol_index(gol, x-n, y); //out[4] = gol_index(gol, x, y); - out[4] = gol_index(gol, x+1, y); + out[4] = gol_index(gol, x+n, y); + + out[5] = gol_index(gol, x-n, y+n); + out[6] = gol_index(gol, x, y+n); + out[7] = gol_index(gol, x+n, y+n); + + return gol_in_bounds(x-n, y-n) || + gol_in_bounds(x+n, y-n) || + gol_in_bounds(x-n, y+n) || + gol_in_bounds(x+n, y+n); +} +inline static int neighbours(const gol_t* gol, int x, int y, enum gol_state out[restrict 8]) +{ + return nneighbours(gol, 1, x, y, out); +} + +#define CEAR_WITH(clr, num, with) do { for(size_t __i=0;__i=4) next = STATE_EMPTY; +#ifdef PART2 + if(count_states(n, STATE_OCCU, 8)>=5) +#else + if(count_states(n, STATE_OCCU, 8)>=4) +#endif + next = STATE_EMPTY; default: break; } gol_set(dest, w, h, next); @@ -149,7 +236,7 @@ int main() gol_t tmp; while(1) { -#ifndef _COPY_ALLOC_ARENA +#if !defined(_COPY_ALLOC_ARENA) simulate_ip(arena, &tmp); #else tmp = simulate_once(arena);