From 949dfdcea0a2df5d5e01dc7453d6e56afce40324 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Thu, 18 Jul 2024 21:06:39 -0400 Subject: [PATCH] C 2017 day 1 --- bin/run | 2 +- c/2017/1/problem.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 c/2017/1/problem.c diff --git a/bin/run b/bin/run index 02de37a..4c45a47 100755 --- a/bin/run +++ b/bin/run @@ -16,7 +16,7 @@ fi year=$(($2)) -if [[ $year -gt 2015 ]] +if [[ $year -lt 2015 ]] then echo "invalid year: $2" echo "must be 2015 or later" diff --git a/c/2017/1/problem.c b/c/2017/1/problem.c new file mode 100644 index 0000000..2b60143 --- /dev/null +++ b/c/2017/1/problem.c @@ -0,0 +1,43 @@ +#include +#include + +int main() { + FILE* input = fopen("../data/2017/1/input.txt", "r"); + char c1, c2, first_char; + long total; + + fread(&first_char, 1, 1, input); + c2 = first_char; + + while (fread(&c1, 1, 1, input) > 0 && c1 != '\n') { + if (c1 == c2) { + total += c1 - '0'; + } + c2 = c1; + } + + if (c2 == first_char) { + total += c2 - '0'; + } + + printf("Part 1: %d\n", total); + + int num_chars = ftell(input); + rewind(input); + total = 0; + + char *buffer = malloc(num_chars); + fread(buffer, 1, num_chars, input); + + for(int i = 0; i < num_chars / 2; i++) { + c1 = buffer[i]; + c2 = buffer[i + (num_chars / 2)]; + if (c1 == c2) { + total += c1 - '0'; + } + } + + total <<= 1; + + printf("Part 2: %d\n", total); +}