Day4
This commit is contained in:
1000
day4/input.txt
Normal file
1000
day4/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
BIN
day4/overlap
Executable file
BIN
day4/overlap
Executable file
Binary file not shown.
46
day4/overlap.c
Normal file
46
day4/overlap.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 64
|
||||
#define max(a,b) (a >= b ? a : b)
|
||||
#define min(a,b) (a <= b ? a : b)
|
||||
|
||||
int overlap(int, int, int, int);
|
||||
|
||||
int main() {
|
||||
char buf[BUFFER_SIZE], *p, c;
|
||||
int min1, max1, min2, max2, overlap_count, sum = 0, sum2 = 0;
|
||||
memset(buf, 0, BUFFER_SIZE);
|
||||
p = buf;
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
sscanf(buf, "%i-%i,%i-%i", &min1, &max1, &min2, &max2);
|
||||
|
||||
overlap_count = overlap(min1, max1, min2, max2);
|
||||
|
||||
if (overlap_count == max1 - min1 + 1 || overlap_count == max2 - min2 + 1) {
|
||||
sum += 1;
|
||||
}
|
||||
|
||||
if (overlap_count > 0) {
|
||||
sum2 += 1;
|
||||
}
|
||||
|
||||
memset(buf, 0, BUFFER_SIZE);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%i\n", sum);
|
||||
printf("%i\n", sum2);
|
||||
}
|
||||
|
||||
int overlap(int min1, int max1, int min2, int max2) {
|
||||
if (max1 < min2 || max2 < min1)
|
||||
return 0;
|
||||
|
||||
return min(max1, max2) - max(min1, min2) + 1;
|
||||
}
|
||||
Reference in New Issue
Block a user