summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2021-12-23 21:05:29 +0000
committerIan C <ianc@noddybox.co.uk>2021-12-23 21:05:29 +0000
commitb6625490c5c485e0b8e33c315dd5fd467f84cf1f (patch)
tree4021209f0a68bd170a65cb28ce726d28ca6e15bb
parentb35eb09038fc1f581aa63321b312fde72df346cd (diff)
Added day 11
-rw-r--r--11.c145
-rw-r--r--11a.c157
2 files changed, 302 insertions, 0 deletions
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 <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+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 <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+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;
+}