Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active October 17, 2025 08:02
Show Gist options
  • Select an option

  • Save oguz-ismail/959f99f3801b5faa596adbc9c6a1267b to your computer and use it in GitHub Desktop.

Select an option

Save oguz-ismail/959f99f3801b5faa596adbc9c6a1267b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#define HASH(h, c) do { \
h *= 5; \
switch (c) { \
case 'w': h++; case 'b': h++; \
case 'u': h++; case 'r': h++; \
case 'g': h++; \
} \
} while (0)
static char design[0x40];
static long long cache[sizeof design];
static unsigned *
pattern(unsigned h) {
#define N 0x800
static unsigned a[N];
unsigned i;
for (i = h; i %= N, a[i] && a[i] != h; i++);
return &a[i];
}
static long long
count(int k) {
long long n;
unsigned h;
int i;
if (cache[k])
return cache[k]-1;
if (!design[k]) {
n = 1;
}
else {
n = h = 0;
for (i = k; i < k+8 && design[i]; i++) {
HASH(h, design[i]);
if (*pattern(h))
n += count(i+1);
}
}
cache[k] = n+1;
return n;
}
int
main(void) {
char *gets(char *);
unsigned h;
int c;
long long silver, gold, n;
h = 0;
do
switch (c = getchar()) {
case '\n':
case ' ': *pattern(h) = h; h = 0;
case ',': continue;
default: HASH(h, c);
}
while (c != '\n');
getchar();
silver = gold = 0;
while (gets(design)) {
memset(cache, 0, sizeof cache);
n = count(0);
silver += n > 0;
gold += n;
}
printf("%lld\n%lld\n", silver, gold);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment