diff --git a/include/advent_math.h b/include/advent_math.h new file mode 100644 index 0000000..27be6da --- /dev/null +++ b/include/advent_math.h @@ -0,0 +1,7 @@ +#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/01.c b/src/2015/01.c index 73eea21..05d7e89 100644 --- a/src/2015/01.c +++ b/src/2015/01.c @@ -1,10 +1,29 @@ #include #include +#include #include "input.h" void advent2015day01(void) { char *input = get_input("input/2015/01"); - printf("Solution for Day 01 of 2015 is not completed yet\n"); + char *c = input; + int f = 0, b = 0, s = 1; + + while (*c != '\0') { + if (*c == '(') { + ++f; + } else { + --f; + } + + if (f == -1 && b == 0) { + b = s; + } + + ++c; + ++s; + } + + printf("%d\n%d\n", f, b); free(input); } diff --git a/src/2015/02.c b/src/2015/02.c index 3ea3836..aa7bad2 100644 --- a/src/2015/02.c +++ b/src/2015/02.c @@ -1,10 +1,27 @@ #include #include +#include #include "input.h" +#include "advent_math.h" void advent2015day02(void) { - char *input = get_input("input/2015/02"); - printf("Solution for Day 02 of 2015 is not completed yet\n"); - free(input); + size_t sz = 0; + char **lines = get_lines("input/2015/02", &sz); + int paper = 0, ribbon = 0; + + for (size_t i = 0; i < sz; i++) { + char *t = strtok(lines[i], "x"); + int w = atoi(t); + t = strtok(NULL, "x"); + int l = atoi(t); + 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)); + } + + printf("%d\n%d\n", paper, ribbon); + del_lines(lines); } diff --git a/src/advent_math.c b/src/advent_math.c new file mode 100644 index 0000000..30bcc3f --- /dev/null +++ b/src/advent_math.c @@ -0,0 +1,9 @@ +#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; +}