summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2018/day13.c48
1 files changed, 30 insertions, 18 deletions
diff --git a/2018/day13.c b/2018/day13.c
index f367392..d6be93d 100644
--- a/2018/day13.c
+++ b/2018/day13.c
@@ -10,14 +10,11 @@ struct Cell {
 	char track;
 	char cart;
 	char turn;
-};
-
-struct Map {
-	struct Cell cells[200][200];
+	char done;
 };
 
 int main() {
-	struct Map map;
+	struct Cell map[200][200];
 	memset(&map, 0, sizeof(map));
 	uint y = 0, x = 0;
 	char ch;
@@ -26,34 +23,36 @@ int main() {
 			y++;
 			x = 0;
 		} else if (ch == '^' || ch == 'v') {
-			map.cells[y][x++] = (struct Cell) { '|', ch, '[' };
+			map[y][x++] = (struct Cell) { '|', ch, '[', 0 };
 		} else if (ch == '<' || ch == '>') {
-			map.cells[y][x++] = (struct Cell) { '-', ch, '[' };
+			map[y][x++] = (struct Cell) { '-', ch, '[', 0 };
 		} else {
-			map.cells[y][x++].track = ch;
+			map[y][x++].track = ch;
 		}
 	}
 
 	for (;;) {
-		struct Map next = map;
 		for (y = 0; y < 200; ++y) {
 			for (x = 0; x < 200; ++x) {
-				if (!map.cells[y][x].cart) continue;
-				next.cells[y][x].cart = 0;
+				if (!map[y][x].cart || map[y][x].done) continue;
 				uint ny = y, nx = x;
-				switch (map.cells[y][x].cart) {
+				switch (map[y][x].cart) {
 					break; case '^': ny--;
 					break; case 'v': ny++;
 					break; case '<': nx--;
 					break; case '>': nx++;
 				}
-				struct Cell *cell = &next.cells[ny][nx];
-				if (cell->cart) {
+				if (map[ny][nx].cart) {
 					printf("%u,%u\n", nx, ny);
-					exit(EXIT_SUCCESS);
+					map[y][x].cart = 0;
+					map[ny][nx].cart = 0;
+					continue;
 				}
-				cell->cart = map.cells[y][x].cart;
-				cell->turn = map.cells[y][x].turn;
+				map[ny][nx].cart = map[y][x].cart;
+				map[ny][nx].turn = map[y][x].turn;
+				map[ny][nx].done = 1;
+				map[y][x].cart = 0;
+				struct Cell *cell = &map[ny][nx];
 				switch (T(cell->cart, cell->track)) {
 					break; case T('^', '/'):  cell->cart = '>';
 					break; case T('^', '\\'): cell->cart = '<';
@@ -86,6 +85,19 @@ int main() {
 				}
 			}
 		}
-		map = next;
+		uint carts = 0;
+		for (y = 0; y < 200; ++y) {
+			for (x = 0; x < 200; ++x) {
+				if (map[y][x].cart) carts++;
+				map[y][x].done = 0;
+			}
+		}
+		if (carts == 1) break;
+	}
+
+	for (y = 0; y < 200; ++y) {
+		for (x = 0; x < 200; ++x) {
+			if (map[y][x].cart) printf("%u,%u\n", x, y);
+		}
 	}
 }
hi.c?id=25e13814fe04576ed949d2e8f085ac23d908bd29&follow=1'>Match whitespace between * [] {}June McEnroe 2019-02-18Fix function-like #define regexJune McEnroe A define like #define FOO (1) is not function-like. 2019-02-18Match Tag in RustJune McEnroe 2019-02-18Match sh functions as TagJune McEnroe 2019-02-18Match Sh and Ss as Tag in mdocJune McEnroe 2019-02-18Match statics and typedefs as TagJune McEnroe 2019-02-18Clean up htmlHeaderJune McEnroe 2019-02-18Remove hi line numberingJune McEnroe Tags are much better for referring to specific parts of a file and line numbering is better done by a post-processing tool such as cat -n or producing a two-column HTML <table>. 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe Running hi twice to insert stuff between the head and the content is a bit of a hack but oh well. 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. 2019-02-17Always split spans after newlinesJune McEnroe Simplifies ANSI and IRC output code, and prepares for line numbered output. 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe