Skip to content

Instantly share code, notes, and snippets.

@zid

zid/2022-day4.c Secret

Last active December 4, 2022 07:25
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/76a43308a7a5cf667186fd87e6487200 to your computer and use it in GitHub Desktop.
Save zid/76a43308a7a5cf667186fd87e6487200 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char line[1024];
FILE *f;
int count = 0, count2 = 0;
f = fopen("day4.txt", "r");
while(1)
{
if(!fgets(line, 128, f))
break;
int a = -1, b = -1, c = -1, d = -1;
sscanf(line, "%d-%d,%d-%d\n", &a, &b, &c, &d);
if((c >= a && d <= b) || (c <= a && d >= b))
{
printf("%d-%d,%d-%d matches\n", a, b, c, d);
count++;
}
if((c >= a && c <= b) || (d >= a && d <= b) || (a >= c && b <= d))
{
printf("%d-%d,%d-%d overlaps\n", a, b, c, d);
count2++;
}
}
printf("Count: %d, count2: %d\n", count, count2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment