master
Avril 4 years ago
parent 5e49fe73da
commit f456c90a9b
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -24,6 +24,11 @@ part1: day6.c | input.h
part1-test: day6.c | input-test.h part1-test: day6.c | input-test.h
$(CC) $< -DTEST $(CFLAGS) -o $@ $(LDFLAGS) $(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: clean:
rm -f part{1,2}{,-test} rm -f part{1,2}{,-test}
rm -f input.h rm -f input.h

@ -20,8 +20,15 @@ static const char* const answers[] = {
#define NUM_QUESTIONS 26 #define NUM_QUESTIONS 26
typedef struct answer { typedef struct answer
bool table[NUM_QUESTIONS]; {
#ifdef PART2
int n_in_group;
int
#else
bool
#endif
table[NUM_QUESTIONS];
} answers_t; } answers_t;
inline static char assert_in_bound(char i) 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]`???? static void populate(const char* from, answers_t * restrict ans) //wtf is this syntax? `bool* restrict a` -> `bool a[restrict N]`????
{ {
while(*from) 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) static size_t count_ans(const answers_t* restrict ans)
{ {
register size_t j=0; register size_t j=0;
for(register size_t i=0;i<NUM_QUESTIONS;i++) for(register size_t i=0;i<NUM_QUESTIONS;i++)
#ifdef PART2
j+= ans->table[i] == ans->n_in_group ? 1 : 0;
#else
j+= ans->table[i] ? 1 : 0; j+= ans->table[i] ? 1 : 0;
#endif
return j; return j;
} }
@ -74,6 +91,9 @@ int main()
const char* current = answers[i]; const char* current = answers[i];
if(*current) { if(*current) {
populate(current, &answered); populate(current, &answered);
#ifdef PART2
answered.n_in_group += 1;
#endif
pop=true; pop=true;
} else { } else {
fullcnt += reset(&pop, &answered, &group_counts[j++]); fullcnt += reset(&pop, &answered, &group_counts[j++]);

Loading…
Cancel
Save