From c0bcbd68771a6258c12ace3bc32ef7644d5a8361 Mon Sep 17 00:00:00 2001 From: Bill Rossi <bassguitarbill@gmail.com> Date: Sat, 30 Nov 2024 16:07:43 -0500 Subject: [PATCH] c 2022 4 --- c/2022/4/camp_cleanup.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 c/2022/4/camp_cleanup.c diff --git a/c/2022/4/camp_cleanup.c b/c/2022/4/camp_cleanup.c new file mode 100644 index 0000000..ebc35bb --- /dev/null +++ b/c/2022/4/camp_cleanup.c @@ -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; +}