advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs

commit 53faf8ede208ca33cafabf4346559b2eac484744
parent cf69ab1ab4a79faea843cc69927e55a00953f11e
Author: bsandro <brian.drosan@gmail.com>
Date:   Wed,  1 Dec 2021 22:00:15 +0200

Day 01, puzzle 2

Diffstat:
Mday01/main.c | 39++++++++++++---------------------------
Aday01/puzzle1.c | 33+++++++++++++++++++++++++++++++++
Aday01/puzzle1.h | 1+
Aday01/puzzle2.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday01/puzzle2.h | 1+
5 files changed, 103 insertions(+), 27 deletions(-)

diff --git a/day01/main.c b/day01/main.c @@ -1,39 +1,24 @@ #include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> +#include "puzzle1.h" +#include "puzzle2.h" int main(int argc, char *argv[]) { - printf("advent of code day 01\n"); - if (argc <= 1) { - printf("Usage: %s inputfile.txt\n", argv[0]); - return 0; - } + printf("Advent of Code: day 01\n"); - FILE *infile = fopen(argv[1], "r"); - if (infile == NULL) { - printf("fopen() error: %s\n", strerror(errno)); + if (argc <= 0) { return -1; } - - char *buf = NULL; - size_t len = 0; - int prev_depth = -1; - int counter = 0; - while ((buf = fgetln(infile, &len)) != NULL) { - if (len == 0) { - break; - } - int depth = atoi(buf); - if (prev_depth > -1 && depth > prev_depth) { - ++counter; - } - prev_depth = depth; + if (argc <= 1) { + printf("Usage: %s inputfile.txt\n", argv[0]); + return -1; } - printf("1: %d\n", counter); + const char *filename = argv[1]; - fclose(infile); + int counter1 = puzzle1(filename); + printf("Puzzle #1: %d\n", counter1); + int counter2 = puzzle2(filename); + printf("Puzzle #2: %d\n", counter2); return 0; } diff --git a/day01/puzzle1.c b/day01/puzzle1.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> + +int puzzle1(const char *filename) { + FILE *infile = fopen(filename, "r"); + if (infile == NULL) { + fprintf(stderr, "fopen() error: %s\n", strerror(errno)); + return -1; + } + + char *buf = NULL; + size_t len = 0; + int prev_depth = -1; + int counter = 0; + + while ((buf = fgetln(infile, &len)) != NULL) { + if (len == 0) { + fprintf(stderr, "Error: invalid input line\n"); + counter = -1; + break; + } + int depth = atoi(buf); + if (prev_depth > -1 && depth > prev_depth) { + ++counter; + } + prev_depth = depth; + } + + fclose(infile); + return counter; +} diff --git a/day01/puzzle1.h b/day01/puzzle1.h @@ -0,0 +1 @@ +int puzzle1(const char *filename); diff --git a/day01/puzzle2.c b/day01/puzzle2.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <stdbool.h> + +void rotate_window(int window[3]) { + int tmp = window[0]; + window[0] = window[1]; + window[1] = window[2]; + window[2] = tmp; +} + +int sum_window(int window[3]) { + return window[0] + window[1] + window[2]; +} + +int sum_new_window(int window[3], int value) { + return window[1] + window[2] + value; +} + +bool is_window_full(int window[3]) { + return window[0] > 0 && window[1] > 0 && window[2] > 0; +} + +int puzzle2(const char *filename) { + FILE *infile = fopen(filename, "r"); + if (infile == NULL) { + fprintf(stderr, "fopen() error: %s\n", strerror(errno)); + return -1; + } + + char *buf = NULL; + size_t len = 0; + int window[3] = {0, 0, 0}; + int counter = 0; + + while ((buf = fgetln(infile, &len)) != NULL) { + if (len == 0) { + fprintf(stderr, "Error: invalid input line\n"); + counter = -1; + break; + } + int depth = atoi(buf); + int sum_win = sum_window(window); + int sum_new = sum_new_window(window, depth); + if (is_window_full(window) && sum_win > 0 && sum_new > sum_win) { + ++counter; + } + rotate_window(window); + window[2] = depth; + } + + fclose(infile); + return counter; +} diff --git a/day01/puzzle2.h b/day01/puzzle2.h @@ -0,0 +1 @@ +int puzzle2(const char *filename);