commit c2f92267749224cda5d4854ba1a4820306753202
parent 548678ce56cc166d3495816095aed4049fbf7103
Author: bsandro <[email protected]>
Date: Sat, 11 Dec 2021 11:42:23 +0200
Day 11, puzzle 1
Diffstat:
5 files changed, 189 insertions(+), 0 deletions(-)
diff --git a/day11/Makefile b/day11/Makefile
@@ -0,0 +1,25 @@
+NAME=$(shell basename ${PWD})
+SRC=$(wildcard *.c ../common/*.c)
+DEPS:=$(wildcard *.h ../common/*.h)
+OBJ:=$(SRC:.c=.o)
+CFLAGS=-O2 -std=c99 -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/day11/input.txt b/day11/input.txt
@@ -0,0 +1,10 @@
+4438624262
+6263251864
+2618812434
+2134264565
+1815131247
+2612457325
+8585767584
+7217134556
+2825456563
+8248473584
diff --git a/day11/main.c b/day11/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 11\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/day11/puzzle.c b/day11/puzzle.c
@@ -0,0 +1,114 @@
+#define _DEFAULT_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <strings.h>
+#include <assert.h>
+#include <ctype.h>
+
+#include "util.h"
+
+#define STR_LEN 1024
+#define GRID_WIDTH 10
+#define GRID_HEIGHT 10
+#define STEPS 100
+
+typedef int octopuses_t[GRID_HEIGHT][GRID_WIDTH];
+
+void step(octopuses_t *octopuses);
+void print(octopuses_t *octopuses);
+void light(octopuses_t *octopuses, int x, int y);
+void process(octopuses_t *octopuses, int x, int y);
+int normalize(octopuses_t *octopuses);
+
+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;
+ octopuses_t octopuses = {0};
+
+ *result1 = 0;
+ *result2 = 0;
+
+ while (fgets(buf, STR_LEN, infile) != NULL) {
+ size_t len = strlen(buf);
+ assert(len >= GRID_WIDTH);
+ for (int i = 0; i < GRID_WIDTH; ++i) {
+ assert(isdigit(buf[i]));
+ octopuses[line_num][i] = buf[i] - '0';
+ }
+ ++line_num;
+ bzero(buf, STR_LEN);
+ }
+
+ int flashes = 0;
+
+ for (int s = 1; s <= STEPS; ++s) {
+ step(&octopuses);
+ flashes += normalize(&octopuses);
+ }
+
+ *result1 = flashes;
+
+ // mutiny! ignoring feof/ferror.
+ fclose(infile);
+}
+
+void step(octopuses_t *octopuses) {
+ for (int y = 0; y < GRID_HEIGHT; ++y) {
+ for (int x = 0; x < GRID_WIDTH; ++x) {
+ process(octopuses, x, y);
+ }
+ }
+}
+
+void process(octopuses_t *octopuses, int x, int y) {
+ int o = ++(*octopuses)[y][x];
+ if (o == 10) {
+ light(octopuses, x, y);
+ }
+}
+
+void light(octopuses_t *octopuses, int x, int y) {
+ if (y > 0) process(octopuses, x, y-1);
+ if (y < GRID_HEIGHT-1) process(octopuses, x, y+1);
+ if (x > 0) {
+ process(octopuses, x-1, y);
+ if (y > 0) process(octopuses, x-1, y-1);
+ if (y < GRID_HEIGHT-1) process(octopuses, x-1, y+1);
+ }
+ if (x < GRID_WIDTH-1) {
+ process(octopuses, x+1, y);
+ if (y > 0) process(octopuses, x+1, y-1);
+ if (y < GRID_HEIGHT-1) process(octopuses, x+1, y+1);
+ }
+}
+
+int normalize(octopuses_t *octopuses) {
+ int flashes = 0;
+ for (int y = 0; y < GRID_HEIGHT; ++y) {
+ for (int x = 0; x < GRID_WIDTH; ++x) {
+ if ((*octopuses)[y][x] > 9) {
+ (*octopuses)[y][x] = 0;
+ ++flashes;
+ }
+ }
+ }
+ return flashes;
+}
+
+void print(octopuses_t *octopuses) {
+ for (int y = 0; y < GRID_HEIGHT; ++y) {
+ for (int x = 0; x < GRID_WIDTH; ++x) {
+ printf("%2d", (*octopuses)[y][x]);
+ }
+ printf("\n");
+ }
+}
diff --git a/day11/test.txt b/day11/test.txt
@@ -0,0 +1,10 @@
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526