Skip to content

Instantly share code, notes, and snippets.

@zid

zid/2022-day3b.c Secret

Created December 3, 2022 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zid/f25c10d7d02626ea38aef8dac502ec71 to your computer and use it in GitHub Desktop.
Save zid/f25c10d7d02626ea38aef8dac502ec71 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *f;
char line[1024];
unsigned long sum = 0;
f = fopen("day3.txt", "r");
char sack1a[256];
char sack1b[256];
char sack1c[256];
while(1)
{
memset(sack1a, 0, 256);
memset(sack1b, 0, 256);
memset(sack1c, 0, 256);
if(!fgets(line, 128, f))
break;
if(line[0] == '\n')
break;
int i, len = strlen(line) - 1;
for(i = 0; i < len; i++)
sack1a[line[i]]=1;
if(!fgets(line, 128, f))
break;
if(line[0] == '\n')
break;
len = strlen(line) - 1;
for(i = 0; i < len; i++)
sack1b[line[i]]=1;
if(!fgets(line, 128, f))
break;
if(line[0] == '\n')
break;
len = strlen(line) - 1;
for(i = 0; i < len; i++)
sack1c[line[i]]=1;
for(i = 0; i < 255; i++)
{
if(sack1a[i] && sack1b[i] && sack1c[i])
{
if(i >= 'a' && i <= 'z')
sum += 1 + i - 'a';
else
sum += 27 + i - 'A';
printf("sum: %lu, %c is in all 3: %s", sum, i, line);
}
}
}
printf("sum: %lu\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment