diff --git a/src/2022/04.c b/src/2022/04.c index b526492..0e06cfb 100644 --- a/src/2022/04.c +++ b/src/2022/04.c @@ -1,10 +1,49 @@ #include #include +#include #include "lfinput.h" +#include "advent_utility.h" + +struct Point parse_floors(char *input) { + size_t sz = 0; + char **sp = split(input, &sz, "-"); + const char *errstr; + struct Point p; + p.x = strtonum(sp[0], INT_MIN, INT_MAX, &errstr); + p.y = strtonum(sp[1], INT_MIN, INT_MAX, &errstr); + free(sp); + return p; +} void advent2022day04(void) { - char *input = get_input("input/2022/04"); - printf("Solution for Day 04 of 2022 is not completed yet\n"); - free(input); + size_t sz = 0; + char **lines = get_lines("input/2022/04", &sz); + int p1 = 0, p2 = 0; + + for (size_t i = 0; i < sz; ++i) { + size_t sp_sz = 0; + char **sp = split(lines[i], &sp_sz, ","); + struct Point e1 = parse_floors(sp[0]); + struct Point e2 = parse_floors(sp[1]); + + if (e1.x <= e2.x && e1.y >= e2.y) { + ++p1; + } else if (e2.x <= e1.x && e2.y >= e1.y) { + ++p1; + } + + if ((e1.x >= e2.x && e1.x <= e2.y) || (e1.y >= e2.x && e1.y <= e2.y)) { + ++p2; + } else if ((e2.x >= e1.x && e2.x <= e1.y) || (e2.y >= e1.x && e2.y <= e1.y)) { + ++p2; + } + + free(sp); + } + + printf("%d\n", p1); + printf("%d\n", p2); + + del_lines(lines); }