From f456c90a9b0892c2b0f8eb9d3bd0ef3b758314fa Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 6 Dec 2020 11:56:25 +0000 Subject: [PATCH] day6: both --- day6/Makefile | 5 +++++ day6/day6.c | 26 +++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/day6/Makefile b/day6/Makefile index 9da2186..7eb034d 100644 --- a/day6/Makefile +++ b/day6/Makefile @@ -24,6 +24,11 @@ part1: day6.c | input.h part1-test: day6.c | input-test.h $(CC) $< -DTEST $(CFLAGS) -o $@ $(LDFLAGS) +part2: day6.c | input.h + $(CC) $< -DPART2 $(CFLAGS) -o $@ $(LDFLAGS) + +part2-test: day6.c | input-test.h + $(CC) $< -DTEST -DPART2 $(CFLAGS) -o $@ $(LDFLAGS) clean: rm -f part{1,2}{,-test} rm -f input.h diff --git a/day6/day6.c b/day6/day6.c index 17c7578..1173274 100644 --- a/day6/day6.c +++ b/day6/day6.c @@ -20,8 +20,15 @@ static const char* const answers[] = { #define NUM_QUESTIONS 26 -typedef struct answer { - bool table[NUM_QUESTIONS]; +typedef struct answer +{ +#ifdef PART2 + int n_in_group; + int +#else + bool +#endif + table[NUM_QUESTIONS]; } answers_t; inline static char assert_in_bound(char i) @@ -37,14 +44,24 @@ inline static char assert_in_bound(char i) static void populate(const char* from, answers_t * restrict ans) //wtf is this syntax? `bool* restrict a` -> `bool a[restrict N]`???? { while(*from) - ans->table[(int)assert_in_bound((*from++)-'a')] = true; + ans->table[(int)assert_in_bound((*from++)-'a')] +#ifdef PART2 + += +#else + = +#endif + 1; } static size_t count_ans(const answers_t* restrict ans) { register size_t j=0; for(register size_t i=0;itable[i] == ans->n_in_group ? 1 : 0; +#else j+= ans->table[i] ? 1 : 0; +#endif return j; } @@ -74,6 +91,9 @@ int main() const char* current = answers[i]; if(*current) { populate(current, &answered); +#ifdef PART2 + answered.n_in_group += 1; +#endif pop=true; } else { fullcnt += reset(&pop, &answered, &group_counts[j++]);