aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2020-12-11 19:26:31 -0700
committernytpu <alex@nytpu.com>2020-12-11 19:26:31 -0700
commitf3bb553e40e28a22f9ba2d2c4aa80788f6ee76d8 (patch)
tree1d455a4621f6bc79d2a9234bd7cbeb5be0a77d9a
parentadded day 10 (diff)
downloadadvent_of_code_2020-f3bb553e40e28a22f9ba2d2c4aa80788f6ee76d8.tar.bz2
advent_of_code_2020-f3bb553e40e28a22f9ba2d2c4aa80788f6ee76d8.zip
added day 11
-rw-r--r--day11/input.txt91
-rw-r--r--day11/problem_1.c124
-rw-r--r--day11/problem_2.c139
-rw-r--r--day11/test_input.txt10
4 files changed, 364 insertions, 0 deletions
diff --git a/day11/input.txt b/day11/input.txt
new file mode 100644
index 0000000..6577da1
--- /dev/null
+++ b/day11/input.txt
@@ -0,0 +1,91 @@
+LLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLL.LLLLLLLL..LLLLLLLLL
+LLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLL
+LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLL.LLLL.LLLLLLLL.LLLLLLLLLL.LLLLLLLL.LLLLLLLLLL
+LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLL.LLLL.LL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
+LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLL
+LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LL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LLLLLLLL.LLLLLLLL.LLLL.LLLL.LLLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLL
+LLLLLLLLLL.LLL..LLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLL
+LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLL
+LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLL.LLLLLLLLLLLLLL.LLLL
+LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLL.LLLLLLLLLL
diff --git a/day11/problem_1.c b/day11/problem_1.c
new file mode 100644
index 0000000..c67b0f1
--- /dev/null
+++ b/day11/problem_1.c
@@ -0,0 +1,124 @@
+/* advent of code day 11, problem 1
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+
+//#define TEST_INPUT
+
+#ifdef TEST_INPUT
+#define WIDTH 10
+#define HEIGHT 10
+#else
+#define WIDTH 93
+#define HEIGHT 91
+#endif // TEST_INPUT
+
+enum point {
+ EMPTY = 'L',
+ OCCUPIED = '#',
+ FLOOR = '.',
+};
+
+void populate_grid (char g[][WIDTH], FILE *f);
+int step (char g[][WIDTH]);
+int count_adjacent (char g[][WIDTH], int row, int col);
+int count_occupied (char g[][WIDTH]);
+
+int main (int argc, char *argv[])
+{
+ char grid[HEIGHT][WIDTH];
+ FILE *infile;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ printf("Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ populate_grid(grid, infile);
+
+ if (fclose(infile) != 0) {
+ fprintf(stderr, "Could not close file %s", argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ while (step(grid) != 0) {}
+
+ printf("Occupied seats: %d\n", count_occupied(grid));
+
+ return EXIT_SUCCESS;
+}
+
+void populate_grid (char g[][WIDTH], FILE *f)
+{
+ int row = 0, col = 0;
+ char c;
+ while ((c = getc(f)) != EOF) {
+ if (c == '\n') {
+ col = 0;
+ ++row;
+ } else {
+ g[row][col++] = c;
+ }
+ }
+ return; // return end of read numbers
+}
+
+int step (char g[][WIDTH])
+{
+ char new[HEIGHT][WIDTH];
+ int row, col, delta = 0, count;
+ for (row=0; row < HEIGHT; ++row) {
+ for (col=0; col < WIDTH; ++col) {
+ count = count_adjacent(g, row, col);
+ if (g[row][col] == EMPTY && count == 0) {
+ new[row][col] = OCCUPIED;
+ ++delta;
+ } else if (g[row][col] == OCCUPIED && count >= 4) {
+ new[row][col] = EMPTY;
+ ++delta;
+ } else {
+ new[row][col] = g[row][col];
+ }
+ }
+ }
+ memcpy(g, new, sizeof(char) * WIDTH * HEIGHT);
+ return delta;
+}
+
+int count_adjacent (char g[][WIDTH], int row, int col)
+{
+ int adjacent = 0, crow, ccol;
+ for (crow=row-1; crow <= row+1; ++crow) {
+ for (ccol=col-1; ccol <= col+1; ++ccol) {
+ if (
+ !(crow == row && ccol == col)
+ && (crow >= 0 && ccol >= 0)
+ && (crow < HEIGHT && ccol < WIDTH)
+ ) {
+ adjacent += g[crow][ccol] == OCCUPIED;
+ }
+ }
+ }
+ return adjacent;
+}
+
+int count_occupied (char g[][WIDTH])
+{
+ int count = 0, row, col;
+ for (row=0; row < HEIGHT; ++row) {
+ for (col=0; col < WIDTH; ++col) {
+ count += g[row][col] == OCCUPIED;
+ }
+ }
+ return count;
+}
diff --git a/day11/problem_2.c b/day11/problem_2.c
new file mode 100644
index 0000000..1c4554d
--- /dev/null
+++ b/day11/problem_2.c
@@ -0,0 +1,139 @@
+/* advent of code day 11, problem 2
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+
+//#define TEST_INPUT
+
+#ifdef TEST_INPUT
+#define WIDTH 10
+#define HEIGHT 10
+#else
+#define WIDTH 93
+#define HEIGHT 91
+#endif // TEST_INPUT
+
+enum point {
+ EMPTY = 'L',
+ OCCUPIED = '#',
+ FLOOR = '.',
+};
+
+void populate_grid (char g[][WIDTH], FILE *f);
+int step (char g[][WIDTH]);
+int count_visible (char g[][WIDTH], int row, int col);
+int count_occupied (char g[][WIDTH]);
+
+int main (int argc, char *argv[])
+{
+ char grid[HEIGHT][WIDTH];
+ FILE *infile;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ printf("Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ populate_grid(grid, infile);
+
+ if (fclose(infile) != 0) {
+ fprintf(stderr, "Could not close file %s", argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ while (step(grid) != 0) {}
+
+ printf("Occupied seats: %d\n", count_occupied(grid));
+
+ return EXIT_SUCCESS;
+}
+
+void populate_grid (char g[][WIDTH], FILE *f)
+{
+ int row = 0, col = 0;
+ char c;
+ while ((c = getc(f)) != EOF) {
+ if (c == '\n') {
+ col = 0;
+ ++row;
+ } else {
+ g[row][col++] = c;
+ }
+ }
+ return; // return end of read numbers
+}
+
+int step (char g[][WIDTH])
+{
+ char new[HEIGHT][WIDTH];
+ int row, col, delta = 0, count;
+ for (row=0; row < HEIGHT; ++row) {
+ for (col=0; col < WIDTH; ++col) {
+ count = count_visible(g, row, col);
+ if (g[row][col] == EMPTY && count == 0) {
+ new[row][col] = OCCUPIED;
+ ++delta;
+ } else if (g[row][col] == OCCUPIED && count >= 5) {
+ new[row][col] = EMPTY;
+ ++delta;
+ } else {
+ new[row][col] = g[row][col];
+ }
+ }
+ }
+ memcpy(g, new, sizeof(char) * WIDTH * HEIGHT);
+ return delta;
+}
+
+int count_visible (char g[][WIDTH], int row, int col)
+{
+ int visible = 0, crow, ccol, row_d, col_d;
+
+ for (row_d=-1; row_d <= 1; ++row_d) {
+ for (col_d=-1; col_d <= 1; ++col_d) {
+ if (row_d == 0 && col_d == 0) continue;
+
+ crow = row;
+ ccol = col;
+ while (
+ ((crow > -1 && ccol > -1)
+ && (crow < HEIGHT && ccol < WIDTH))
+ && ((crow == row && ccol == col)
+ || g[crow][ccol] != EMPTY)
+ ) {
+ if (
+ !(crow == row && ccol == col)
+ && g[crow][ccol] == OCCUPIED
+ ) {
+ ++visible;
+ break;
+ }
+ crow += row_d;
+ ccol += col_d;
+ }
+ }
+ }
+
+ return visible;
+}
+
+int count_occupied (char g[][WIDTH])
+{
+ int count = 0, row, col;
+ for (row=0; row < HEIGHT; ++row) {
+ for (col=0; col < WIDTH; ++col) {
+ count += g[row][col] == OCCUPIED;
+ }
+ }
+ return count;
+}
diff --git a/day11/test_input.txt b/day11/test_input.txt
new file mode 100644
index 0000000..1beaede
--- /dev/null
+++ b/day11/test_input.txt
@@ -0,0 +1,10 @@
+L.LL.LL.LL
+LLLLLLL.LL
+L.L.L..L..
+LLLL.LL.LL
+L.LL.LL.LL
+L.LLLLL.LL
+..L.L.....
+LLLLLLLLLL
+L.LLLLLL.L
+L.LLLLL.LL