commit 51a244f055d4f0177ba944588faeb746acf8c0f9
parent 54fd25077b19ed9ce6d4c5a42b54bf8ce0bc0ff7
Author: bsandro <email@bsandro.tech>
Date: Wed, 6 Dec 2023 22:19:57 +0200
day 06 p1+p2 so lazy I didn't even bother to parse the data
Diffstat:
5 files changed, 145 insertions(+), 0 deletions(-)
diff --git a/day06/Makefile b/day06/Makefile
@@ -0,0 +1,25 @@
+NAME=$(shell basename ${PWD})
+SRC=$(wildcard *.c)
+DEPS:=$(wildcard *.h)
+OBJ:=$(SRC:.c=.o)
+CFLAGS=-O2 -std=c11 -Werror -Wall -Wextra -I. -I../common
+LDFLAGS=-lc
+
+all: $(NAME)
+
+.PHONY: clean run
+
+clean:
+ rm -f $(OBJ) $(NAME)
+
+%.o : %.c $(DEPS)
+ @$(CC) $(CFLAGS) -c $< -o $@
+
+$(NAME): $(OBJ)
+ @$(CC) $(OBJ) -o $@ $(LDFLAGS)
+
+run: $(NAME)
+ @./$(NAME) input.txt
+
+test: $(NAME)
+ @./$(NAME) test.txt
diff --git a/day06/input.txt b/day06/input.txt
@@ -0,0 +1,2 @@
+Time: 45 97 72 95
+Distance: 305 1062 1110 1695
diff --git a/day06/main.c b/day06/main.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+
+void puzzle(const char *filename, long long *res1, long long *res2);
+//void puzzle_test(const char *filename, long long *res1, long long *res2);
+
+int main(int argc, char *argv[]) {
+ printf("Advent of Code: day 06\n");
+ double time_start = clock();
+
+ if (argc <= 1) {
+ printf("Usage: %s inputfile.txt\n", argv[0]);
+ return -1;
+ }
+
+ const char *filename = argv[1];
+ long long counter1 = -1;
+ long long counter2 = -1;
+
+ puzzle(filename, &counter1, &counter2);
+
+ printf("Puzzle #1: %lld\n", counter1);
+ printf("Puzzle #2: %lld\n", counter2);
+
+ double elapsed = clock() - time_start;
+ printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC);
+
+ return 0;
+}
diff --git a/day06/puzzle.c b/day06/puzzle.c
@@ -0,0 +1,86 @@
+#define _DEFAULT_SOURCE
+
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <strings.h>
+#include <assert.h>
+#include <stdbool.h>
+
+#define STR_LEN 2048
+
+bool is_digit(char b) {
+ return b>='0' && b<='9';
+}
+
+// I'll skip the parse part for now
+
+typedef struct Race {
+ int time;
+ int record;
+} Race;
+
+void puzzle(const char *filename, long long *result1, long long *result2) {
+ FILE *infile = fopen(filename, "r");
+ if (infile == NULL) {
+ fprintf(stderr, "fopen() error: %s\n", strerror(errno));
+ return;
+ }
+
+ char buf[STR_LEN] = {0};
+ unsigned int line_num = 0;
+
+ *result1 = 0;
+ *result2 = 0;
+
+ while (fgets(buf, STR_LEN, infile) != NULL) {
+
+ ++line_num;
+ bzero(buf, STR_LEN);
+ }
+
+ // test data
+ /*Race races[3] = {
+ { .time = 7, .record = 9 },
+ { .time = 15, .record = 40 },
+ { .time = 30, .record = 200 }
+ };*/
+
+ // real data
+ Race races[4] = {
+ { .time = 45, .record = 305 },
+ { .time = 97, .record = 1062 },
+ { .time = 72, .record = 1110 },
+ { .time = 95, .record = 1695 },
+ };
+
+ *result1 = 1;
+ for (int i=0; i<4; ++i) {
+ int wins = 0;
+ for (int t=1; t<races[i].time; ++t) { // holding the button
+ int speed = t;
+ int time = races[i].time-t;
+ int dist = speed*time;
+ if (dist>races[i].record) wins++;
+ }
+ //printf("wins: %d\n", wins);
+ *result1 *= wins;
+ }
+
+ {
+ int wins = 0;
+ for (uint64_t t=1; t<45977295; ++t) { // holding the button
+ uint64_t speed = t;
+ uint64_t time = 45977295-t;
+ uint64_t dist = speed*time;
+ if (dist>305106211101695) wins++;
+ }
+ *result2 = wins;
+ }
+
+ // mutiny! ignoring feof/ferror.
+ fclose(infile);
+}
diff --git a/day06/test.txt b/day06/test.txt
@@ -0,0 +1,2 @@
+Time: 7 15 30
+Distance: 9 40 200