You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
1.9 KiB

3 years ago
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if defined(TEST) && !defined(DEBUG)
#define DEBUG
#endif
#include <attrs.h>
#include <panic.h>
3 years ago
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
3 years ago
typedef struct answer
{
#ifdef PART2
int n_in_group;
int
#else
bool
#endif
table[NUM_QUESTIONS];
3 years ago
} answers_t;
noglobal inline static char assert_in_bound(char i)
3 years ago
{
register int x=(int)i;
if(x<0 || x>=NUM_QUESTIONS) {
panic("char '%c' (%d) is not in range 0..%d", i, x, NUM_QUESTIONS);
3 years ago
}
return i;
}
static void populate(const char* from, answers_t * restrict ans)
3 years ago
{
while(*from)
3 years ago
ans->table[(int)assert_in_bound((*from++)-'a')]
#ifdef PART2
+=
#else
=
#endif
1;
3 years ago
}
pure static size_t count_ans(const answers_t* restrict ans)
3 years ago
{
register size_t j=0;
for(register size_t i=0;i<NUM_QUESTIONS;i++)
3 years ago
#ifdef PART2
j+= ans->table[i] == ans->n_in_group ? 1 : 0;
#else
3 years ago
j+= ans->table[i] ? 1 : 0;
3 years ago
#endif
3 years ago
return j;
}
3 years ago
inline static size_t reset(bool* restrict pop, answers_t * restrict ans, size_t* restrict group_count)
3 years ago
{
register size_t fullcnt=0;
3 years ago
if(*pop) {
fullcnt = (*group_count = count_ans(ans));
3 years ago
#ifdef DEBUG
fprintf(stderr, "Last group: %lu (fcnt %lu)\n" , *group_count, fullcnt);
#endif
}
3 years ago
*pop = false;
memset(ans,0, sizeof(answers_t));
3 years ago
return fullcnt;
}
int main()
{
3 years ago
answers_t answered = {0};
3 years ago
size_t group_counts[answers_sz+1] = {0};
size_t fullcnt=0;
bool pop=false;
for(size_t i=0,j=0;i<answers_sz;i++)
{
const char* current = answers[i];
if(*current) {
3 years ago
populate(current, &answered);
3 years ago
#ifdef PART2
answered.n_in_group += 1;
#endif
3 years ago
pop=true;
} else {
3 years ago
fullcnt += reset(&pop, &answered, &group_counts[j++]);
3 years ago
}
}
3 years ago
fullcnt+= reset(&pop, &answered, group_counts +answers_sz);
3 years ago
printf("%lu\n", fullcnt);
return 0;
}