From b6625490c5c485e0b8e33c315dd5fd467f84cf1f Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 23 Dec 2021 21:05:29 +0000 Subject: Added day 11 --- 11.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11a.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 11.c create mode 100644 11a.c diff --git a/11.c b/11.c new file mode 100644 index 0000000..6b66cc7 --- /dev/null +++ b/11.c @@ -0,0 +1,145 @@ +#include +#include +#include + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static int Increment(char octo[10][10], int x, int y) +{ + if (x >= 0 && x < 10 && y >=0 && y < 10) + { + if (octo[y][x] != 10) + { + octo[y][x]++; + + return octo[y][x] == 10; + } + else + { + return 0; + } + } + else + { + return 0; + } +} + +static void Flash(char octo[10][10], int x, int y) +{ + int f,n; + + for(f = -1; f < 2; f++) + { + for(n = -1; n < 2; n++) + { + if (Increment(octo, x + f, y + n)) + { + Flash(octo, x + f, y + n); + } + } + } +} + +int main(void) +{ + char octo[10][10] = {0}; + char toflash[10][10] = {0}; + char buff[0x8000] = {0}; + int flashes = 0; + int f = 0; + int y = 0; + int x = 0; + + y = 0; + + while(ReadLine(buff, sizeof buff, stdin)) + { + for(f = 0; f < 10; f++) + { + octo[y][f] = buff[f] - '0'; + } + + y++; + } + + for(f = 0; f < 100; f++) + { + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + octo[y][x]++; + } + } + + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + toflash[y][x] = octo[y][x] == 10; + } + } + + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + if (toflash[y][x]) + { + Flash(octo, x, y); + } + } + } + + /* + if (f == 1) + { + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + if (octo[y][x] == 10) + { + printf("*"); + } + else + { + printf("%d", (int)octo[y][x]); + } + } + printf("\n"); + } + } + */ + + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + if (octo[y][x] == 10) + { + flashes++; + octo[y][x] = 0; + } + } + } + } + + printf("flashes = %d\n", flashes); + + return 0; +} diff --git a/11a.c b/11a.c new file mode 100644 index 0000000..e1ab19a --- /dev/null +++ b/11a.c @@ -0,0 +1,157 @@ +#include +#include +#include + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static int Increment(char octo[10][10], int x, int y) +{ + if (x >= 0 && x < 10 && y >=0 && y < 10) + { + if (octo[y][x] != 10) + { + octo[y][x]++; + + return octo[y][x] == 10; + } + else + { + return 0; + } + } + else + { + return 0; + } +} + +static void Flash(char octo[10][10], int x, int y) +{ + int f,n; + + for(f = -1; f < 2; f++) + { + for(n = -1; n < 2; n++) + { + if (Increment(octo, x + f, y + n)) + { + Flash(octo, x + f, y + n); + } + } + } +} + +int main(void) +{ + char octo[10][10] = {0}; + char toflash[10][10] = {0}; + char buff[0x8000] = {0}; + int flashes = 0; + int all_flash = 0; + int f = 0; + int y = 0; + int x = 0; + + y = 0; + + while(ReadLine(buff, sizeof buff, stdin)) + { + for(f = 0; f < 10; f++) + { + octo[y][f] = buff[f] - '0'; + } + + y++; + } + + for(f = 0; f < 500; f++) + { + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + octo[y][x]++; + } + } + + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + toflash[y][x] = octo[y][x] == 10; + } + } + + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + if (toflash[y][x]) + { + Flash(octo, x, y); + } + } + } + + /* + if (f == 1) + { + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + if (octo[y][x] == 10) + { + printf("*"); + } + else + { + printf("%d", (int)octo[y][x]); + } + } + printf("\n"); + } + } + */ + + all_flash = 1; + + for(y = 0; y < 10; y++) + { + for(x = 0; x < 10; x++) + { + if (octo[y][x] == 10) + { + flashes++; + octo[y][x] = 0; + } + else + { + all_flash = 0; + } + } + } + + if (all_flash) + { + printf("all flash on %d\n", f + 1); + } + } + + printf("flashes = %d\n", flashes); + + return 0; +} -- cgit v1.2.3