From b5caa4dfa4ba1996fe2a50c75c7a54889595fd7c Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 10 Dec 2021 14:37:41 -0800 Subject: [PATCH] update libflint, 2021-02 --- include/advent_math.h | 7 ------ src/2015/02.c | 6 ++--- src/2021/02.c | 53 +++++++++++++++++++++++++++++++++++++++---- src/2021/03.c | 9 +++++--- src/advent_math.c | 9 -------- 5 files changed, 58 insertions(+), 26 deletions(-) delete mode 100644 include/advent_math.h delete mode 100644 src/advent_math.c diff --git a/include/advent_math.h b/include/advent_math.h deleted file mode 100644 index 27be6da..0000000 --- a/include/advent_math.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef ADVENT_MATH_H_ -#define ADVENT_MATH_H_ - -int int_max(int, int); -int int_min(int, int); - -#endif // ADVENT_MATH_H_ diff --git a/src/2015/02.c b/src/2015/02.c index 376a4f5..2a1bcfb 100644 --- a/src/2015/02.c +++ b/src/2015/02.c @@ -3,7 +3,7 @@ #include #include "input.h" -#include "advent_math.h" +#include "math.h" void advent2015day02(void) { size_t sz = 0; @@ -18,8 +18,8 @@ void advent2015day02(void) { t = strtok(NULL, "x"); int h = atoi(t); - paper += 2 * l * w + 2 * w * h + 2 * h * l + int_min(w * l, int_min(l * h, h * w)); - ribbon += l * w * h + int_min(w + w + h + h, int_min(h + h + l + l, l + l + w + w)); + paper += 2 * l * w + 2 * w * h + 2 * h * l + min_int(w * l, min_int(l * h, h * w)); + ribbon += l * w * h + min_int(w + w + h + h, min_int(h + h + l + l, l + l + w + w)); } printf("%d\n%d\n", paper, ribbon); diff --git a/src/2021/02.c b/src/2021/02.c index 1e29e7c..ec5cab6 100644 --- a/src/2021/02.c +++ b/src/2021/02.c @@ -1,10 +1,55 @@ #include #include +#include +#include #include "input.h" -void advent2021day02(void) { - char *input = get_input("input/2021/02"); - printf("Solution for Day 02 of 2021 is not completed yet\n"); - free(input); +static void solution(long p2) { + size_t sz = 0; + char **input = get_lines("input/2021/02", &sz); + long h = 0, d = 0, a = 0; + + for (size_t i = 0; i < sz; ++i) { + size_t sp_sz = 0; + char **sp = split(input[i], &sp_sz, " "); + if (sp == NULL) { + printf("Failed to split\n"); + exit(1); + } + const char *errstr; + long n = 0; + n = (long)strtonum(sp[1], LONG_MIN, LONG_MAX, &errstr); + if (errstr) { + printf("Failed to convert %s to long\n", sp[i]); + exit(1); + } + + if (!p2) { + if (strcmp("forward", sp[0]) == 0) { + h += n; + } else if (strcmp("down", sp[0]) == 0) { + d += n; + } else { + d -= n; + } + } else { + if (strcmp("forward", sp[0]) == 0) { + h += n; + d += a * n; + } else if (strcmp("down", sp[0]) == 0) { + a += n; + } else { + a -= n; + } + } + free(sp); + } + printf("%ld\n", h * d); + del_lines(input); +} + +void advent2021day02(void) { + solution(0); + solution(1); } diff --git a/src/2021/03.c b/src/2021/03.c index d7b287d..05e7a20 100644 --- a/src/2021/03.c +++ b/src/2021/03.c @@ -1,10 +1,13 @@ #include #include +#include #include "input.h" void advent2021day03(void) { - char *input = get_input("input/2021/03"); - printf("Solution for Day 03 of 2021 is not completed yet\n"); - free(input); + size_t sz; + char **input = get_lines("input/2021/03", &sz); + + + del_lines(input); } diff --git a/src/advent_math.c b/src/advent_math.c deleted file mode 100644 index 30bcc3f..0000000 --- a/src/advent_math.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "advent_math.h" - -int int_max(int a, int b) { - return a > b ? a : b; -} - -int int_min(int a, int b) { - return a < b ? a : b; -}