c 2022 4
This commit is contained in:
		
							parent
							
								
									332ece0d97
								
							
						
					
					
						commit
						c0bcbd6877
					
				
							
								
								
									
										39
									
								
								c/2022/4/camp_cleanup.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								c/2022/4/camp_cleanup.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
#include "../../lib/aoc.h"
 | 
			
		||||
 | 
			
		||||
bool fully_contain(char *line) {
 | 
			
		||||
  int a, b, c, d;
 | 
			
		||||
  int y = sscanf(line, "%d-%d,%d-%d", &a, &b, &c, &d);
 | 
			
		||||
  return ((a <= c && b >= d) ||
 | 
			
		||||
	  (c <= a && d >= b));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool overlap(char *line) {
 | 
			
		||||
  int a, b, c, d;
 | 
			
		||||
  int y = sscanf(line, "%d-%d,%d-%d", &a, &b, &c, &d);
 | 
			
		||||
  return ((a <= c && b >= c) ||
 | 
			
		||||
	  (a <= d && b >= d) ||
 | 
			
		||||
	  (c <= a && d >= a) ||
 | 
			
		||||
	  (c <= b && d >= b));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
  char *line;
 | 
			
		||||
 | 
			
		||||
  int contain_sum = 0;
 | 
			
		||||
  int overlap_sum = 0;
 | 
			
		||||
  while ((line = aoc_read_line()) != NULL) {
 | 
			
		||||
    if (fully_contain(line)) contain_sum++;
 | 
			
		||||
    if (overlap(line)) overlap_sum++;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  printf("Part 1: %d\n", contain_sum);
 | 
			
		||||
  printf("Part 2: %d\n", overlap_sum);
 | 
			
		||||
 | 
			
		||||
  aoc_free();
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user