day 11
authorMart Lubbers <mart@martlubbers.net>
Sat, 11 Dec 2021 12:47:17 +0000 (13:47 +0100)
committerMart Lubbers <mart@martlubbers.net>
Sat, 11 Dec 2021 12:47:17 +0000 (13:47 +0100)
11.txt [new file with mode: 0644]
11a.c [new file with mode: 0644]
11b.c [new file with mode: 0644]
11e.txt [new file with mode: 0644]
Makefile

diff --git a/11.txt b/11.txt
new file mode 100644 (file)
index 0000000..1a0fab9
--- /dev/null
+++ b/11.txt
@@ -0,0 +1,10 @@
+4134384626
+7114585257
+1582536488
+4865715538
+5733423513
+8532144181
+1288614583
+2248711141
+6415871681
+7881531438
diff --git a/11a.c b/11a.c
new file mode 100644 (file)
index 0000000..0ed0f8d
--- /dev/null
+++ b/11a.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+void flash(char grid[10][10], int *flashes, int x, int y)
+{
+       //already flashed or not charged up enough
+       if (grid[y][x] == 0 || grid[y][x] < 10)
+               return;
+       (*flashes)++;
+       grid[y][x] = 0;
+       for (int dy = -1; dy<=1; dy++)
+               for (int dx = -1; dx<=1; dx++)
+                       if ( (dx != 0 || dy != 0) &&
+                                       x+dx >= 0 && x+dx < 10 &&
+                                       y+dy >= 0 && y+dy < 10) {
+                               if (grid[y+dy][x+dx] != 0)
+                                       grid[y+dy][x+dx]++;
+                               flash(grid, flashes, x+dx, y+dy);
+                       }
+}
+
+int main()
+{
+       int x = 0, y = 0;
+       int c;
+       char grid[10][10];
+       while( (c = getchar()) != EOF) {
+               if (c == '\n') {
+                       y++;
+                       x = 0;
+               } else {
+                       grid[y][x++] = c-'0';
+               }
+       }
+
+       int flashes = 0;
+       for (int i = 0; i<100; i++) {
+               for (int y = 0; y<10; y++)
+                       for (int x = 0; x<10; x++)
+                               grid[y][x]++;
+               for (int y = 0; y<10; y++)
+                       for (int x = 0; x<10; x++)
+                               flash(grid, &flashes, x, y);
+       }
+       
+       printf("%d\n", flashes);
+}
diff --git a/11b.c b/11b.c
new file mode 100644 (file)
index 0000000..bda7072
--- /dev/null
+++ b/11b.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+void flash(char grid[10][10], int *flashes, int x, int y)
+{
+       //already flashed or not charged up enough
+       if (grid[y][x] == 0 || grid[y][x] < 10)
+               return;
+       (*flashes)++;
+       grid[y][x] = 0;
+       for (int dy = -1; dy<=1; dy++)
+               for (int dx = -1; dx<=1; dx++)
+                       if ( (dx != 0 || dy != 0) &&
+                                       x+dx >= 0 && x+dx < 10 &&
+                                       y+dy >= 0 && y+dy < 10) {
+                               if (grid[y+dy][x+dx] != 0)
+                                       grid[y+dy][x+dx]++;
+                               flash(grid, flashes, x+dx, y+dy);
+                       }
+}
+
+int main()
+{
+       int x = 0, y = 0;
+       int c;
+       char grid[10][10];
+       while( (c = getchar()) != EOF) {
+               if (c == '\n') {
+                       y++;
+                       x = 0;
+               } else {
+                       grid[y][x++] = c-'0';
+               }
+       }
+
+       int flashes = 0;
+       int gen = 0;
+       while (flashes != 100) {
+               flashes = 0;
+               gen++;
+               for (int y = 0; y<10; y++)
+                       for (int x = 0; x<10; x++)
+                               grid[y][x]++;
+               for (int y = 0; y<10; y++)
+                       for (int x = 0; x<10; x++)
+                               flash(grid, &flashes, x, y);
+       }
+       printf("%d\n", gen);
+}
diff --git a/11e.txt b/11e.txt
new file mode 100644 (file)
index 0000000..03743f6
--- /dev/null
+++ b/11e.txt
@@ -0,0 +1,10 @@
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
index 75a0a9d..e656642 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 CFLAGS:=-Wall -Wextra -O3
 LFLAGS:=-f
 
-BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 10),$(num)a $(num)b)
+BINARIES:=$(foreach num,$(shell seq -f '%02.0f' 1 11),$(num)a $(num)b)
 
 all: $(BINARIES)