From 0ec96252721b621262a36f2e10d7ee76a9146dec Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sat, 11 Dec 2021 13:47:17 +0100 Subject: [PATCH] day 11 --- 11.txt | 10 ++++++++++ 11a.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 11b.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 11e.txt | 10 ++++++++++ Makefile | 2 +- 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 11.txt create mode 100644 11a.c create mode 100644 11b.c create mode 100644 11e.txt diff --git a/11.txt b/11.txt new file mode 100644 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 index 0000000..0ed0f8d --- /dev/null +++ b/11a.c @@ -0,0 +1,46 @@ +#include + +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 index 0000000..bda7072 --- /dev/null +++ b/11b.c @@ -0,0 +1,48 @@ +#include + +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 index 0000000..03743f6 --- /dev/null +++ b/11e.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/Makefile b/Makefile index 75a0a9d..e656642 100644 --- 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) -- 2.20.1