Skip to content

Instantly share code, notes, and snippets.

@zid
Last active December 6, 2022 10:57
Show Gist options
  • Save zid/274b3be966ce1ce3fb10101febdfa06a to your computer and use it in GitHub Desktop.
Save zid/274b3be966ce1ce3fb10101febdfa06a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define R 14
static int fullcheck(const char *roll)
{
int i, j;
for(i = R-2; i >= 0; i--)
for(j = R-1; j >= i+1; j--)
if(roll[i] == roll[j])
return i+1;
return 0;
}
static int fastcheck(const char *roll)
{
int i;
for(i = R-2; i >= 0; i--)
if(roll[R-1] == roll[i])
return i+1;
return 0;
}
int main(void)
{
char line[4100];
FILE *f;
int pos;
f = fopen("day6.txt", "r");
while(1)
{
char roll[R];
int i, prevsize;
if(!fgets(line, 4100, f))
break;
memcpy(roll, line, R);
pos = R;
prevsize = 0;
while(1)
{
int r;
if(prevsize != R-1)
r = fullcheck(roll);
else
r = fastcheck(roll);
if(!r)
break;
prevsize = R-r;
memmove(roll, roll+r, R-r);
for(i = 0; i < r; i++)
{
if(!line[pos] || line[pos] == '\n')
{
pos++;
goto out;
}
pos++;
roll[R-r+i] = line[pos];
}
}
out:
for(i = 0; i < R; i++)
putchar(roll[i]);
putchar('\n');
printf("pos: %d\n", pos+1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment