From 28c6c83641191868f4880c606b83bf8c7798e91c Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 6 Dec 2020 11:43:43 +0000 Subject: [PATCH] day6: part1 --- day6/Makefile | 30 +++++++++++++++++++ day6/day6.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 day6/Makefile create mode 100644 day6/day6.c diff --git a/day6/Makefile b/day6/Makefile new file mode 100644 index 0000000..9da2186 --- /dev/null +++ b/day6/Makefile @@ -0,0 +1,30 @@ + +OPT_FLAGS?= -O3 -march=native -pipe -flto \ + -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ + -fno-stack-check -fno-strict-aliasing + +CFLAGS?=$(OPT_FLAGS) +LDFLAGS?=-O3 -flto + +CFLAGS+= -Wall -pedantic --std=gnu11 + +.PHONY: all +all: part1 + +inpu%.h: inpu% + @rm -f $@ + while read line; do \ + echo "\"$$line\"," >> $@; \ + done < $< + +part1: day6.c | input.h + $(CC) $< $(CFLAGS) -o $@ $(LDFLAGS) + +part1-test: day6.c | input-test.h + $(CC) $< -DTEST $(CFLAGS) -o $@ $(LDFLAGS) + +clean: + rm -f part{1,2}{,-test} + rm -f input.h + diff --git a/day6/day6.c b/day6/day6.c new file mode 100644 index 0000000..bec1bf3 --- /dev/null +++ b/day6/day6.c @@ -0,0 +1,83 @@ + +#include +#include +#include +#include + +#if defined(TEST) && !defined(DEBUG) +#define DEBUG +#endif + +static const char* const answers[] = { +#ifdef TEST +#include "input-test.h" +#else +#include "input.h" +#endif +}; + +#define answers_sz (sizeof(answers) / sizeof(char*)) + +#define NUM_QUESTIONS 26 + +inline static char assert_in_bound(char i) +{ + register int x=(int)i; + if(x<0 || x>=NUM_QUESTIONS) { + fprintf(stderr, "Fatal error: char '%c' (%d) is not in range 0..%d\n", i, x, NUM_QUESTIONS); + abort(); + } + return i; +} + +static void populate(const char* from, bool answered [restrict NUM_QUESTIONS]) //wtf is this syntax? `bool* restrict a` -> `bool a[restrict N]`???? +{ + while(*from) + answered[(int)assert_in_bound((*from++)-'a')] = true; +} + +static size_t count_ans(const bool answered[restrict NUM_QUESTIONS]) +{ + register size_t j=0; + for(register size_t i=0;i