summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2018/day18.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/2018/day18.c b/2018/day18.c
new file mode 100644
index 0000000..686ae70
--- /dev/null
+++ b/2018/day18.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef unsigned uint;
+
+struct Map {
+	char acres[50][50];
+};
+
+static uint adjacent(struct Map map, uint y, uint x, char type) {
+	uint count = 0;
+	if (y > 0) {
+		count += (type == map.acres[y - 1][x]);
+		if (x > 0)  count += (type == map.acres[y - 1][x - 1]);
+		if (x < 49) count += (type == map.acres[y - 1][x + 1]);
+	}
+	if (x > 0)  count += (type == map.acres[y][x - 1]);
+	if (x < 49) count += (type == map.acres[y][x + 1]);
+	if (y < 49) {
+		count += (type == map.acres[y + 1][x]);
+		if (x > 0)  count += (type == map.acres[y + 1][x - 1]);
+		if (x < 49) count += (type == map.acres[y + 1][x + 1]);
+	}
+	return count;
+}
+
+static struct Map step(struct Map prev) {
+	struct Map next;
+	for (uint y = 0; y < 50; ++y) {
+		for (uint x = 0; x < 50; ++x) {
+			if (prev.acres[y][x] == '.') {
+				if (adjacent(prev, y, x, '|') > 2) {
+					next.acres[y][x] = '|';
+				} else {
+					next.acres[y][x] = '.';
+				}
+			} else if (prev.acres[y][x] == '|') {
+				if (adjacent(prev, y, x, '#') > 2) {
+					next.acres[y][x] = '#';
+				} else {
+					next.acres[y][x] = '|';
+				}
+			} else {
+				if (adjacent(prev, y, x, '#') && adjacent(prev, y, x, '|')) {
+					next.acres[y][x] = '#';
+				} else {
+					next.acres[y][x] = '.';
+				}
+			}
+		}
+	}
+	return next;
+}
+
+int main(void) {
+	struct Map map;
+	for (uint y = 0; y < 50; ++y) {
+		scanf("%50c\n", map.acres[y]);
+	}
+
+	for (uint i = 0; i < 10; ++i) {
+		map = step(map);
+	}
+	uint wood = 0, lumber = 0;
+	for (uint y = 0; y < 50; ++y) {
+		for (uint x = 0; x < 50; ++x) {
+			if (map.acres[y][x] == '|') wood++;
+			if (map.acres[y][x] == '#') lumber++;
+		}
+	}
+	printf("%u\n", wood * lumber);
+}
10Request all supported caps from serverJune McEnroe 2019-11-10Filter ACCOUNT, AWAY, CHGHOST for incapable clientsJune McEnroe 2019-11-10Rename listen to localJune McEnroe 2019-11-09Remove extended-join and invite-notifyJune McEnroe 2019-11-09Maintain stateCaps and offer them to clientsJune McEnroe 2019-11-09Parse capabilitiesJune McEnroe 2019-11-09Avoid the reserved _A names with BIT macroJune McEnroe 2019-11-09Define macro for bit flag enumsJune McEnroe 2019-11-08Check that password is hashedJune McEnroe 2019-11-08Avoid calling getopt_long again after it returns -1June McEnroe 2019-11-08Only change AWAY status for registered clientsJune McEnroe 2019-11-07Just write the example normallyJune McEnroe 2019-11-07Include path in readlinkat errorJune McEnroe 2019-11-07Call clientConsume before clientRecvJune McEnroe 2019-11-06Use -l:filename in Linux.mkJune McEnroe 2019-11-06Fix compat.h for #defined strlcpyJune McEnroe 2019-11-06Allow unsetting LIBRESSL_PREFIXJune McEnroe 2019-11-06Document calico service configurationJune McEnroe 2019-11-06Document SASL EXTERNAL configuration in more detailJune McEnroe 2019-11-06Document pounce service configurationJune McEnroe 2019-11-06Mention Darwin and GNU/Linux in READMEJune McEnroe 2019-11-06Assume LibreSSL from brew on DarwinJune McEnroe 2019-11-06Remove -DNO_EXPLICIT_BZERO from Darwin.mkJune McEnroe 2019-11-06Don't install rc scripts or dirs on LinuxJune McEnroe