summary refs log tree commit diff homepage
path: root/2018/day09.c
diff options
context:
space:
mode:
Diffstat (limited to '2018/day09.c')
-rw-r--r--2018/day09.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/2018/day09.c b/2018/day09.c
new file mode 100644
index 0000000..804392b
--- /dev/null
+++ b/2018/day09.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef unsigned uint;
+
+static struct Marble {
+	uint num;
+	struct Marble *prev;
+	struct Marble *next;
+} *current;
+
+static struct Marble *ins(struct Marble *prev, uint num) {
+	struct Marble *next = malloc(sizeof(*next));
+	next->num = num;
+	next->prev = prev;
+	next->next = prev->next;
+	next->prev->next = next;
+	next->next->prev = next;
+	return next;
+}
+
+static struct Marble *rem(struct Marble *marb) {
+	struct Marble *next = marb->next;
+	marb->prev->next = marb->next;
+	marb->next->prev = marb->prev;
+	free(marb);
+	return next;
+}
+
+int main() {
+	uint players, marbles;
+	scanf("%u players; last marble is worth %u points", &players, &marbles);
+	marbles += 1;
+
+	uint scores[players];
+	for (uint i = 0; i < players; ++i) {
+		scores[i] = 0;
+	}
+
+	current = malloc(sizeof(*current));
+	current->num = 0;
+	current->prev = current;
+	current->next = current;
+
+	uint marble = 1;
+	while (marble < marbles) {
+		for (uint i = 0; i < players; ++i) {
+			if (marble % 23) {
+				current = ins(current->next, marble);
+			} else {
+				scores[i] += marble;
+				struct Marble *marb = current;
+				for (uint j = 0; j < 7; ++j) {
+					marb = marb->prev;
+				}
+				scores[i] += marb->num;
+				current = rem(marb);
+			}
+			if (++marble == marbles) break;
+		}
+	}
+
+	uint max = 0;
+	for (uint i = 0; i < players; ++i) {
+		if (scores[i] > max) max = scores[i];
+	}
+	printf("%u\n", max);
+}
2020-03-03Remove setoptJune McEnroe 2020-03-03Use getopts in shell scriptsJune McEnroe 2020-02-27Style %T outside of Rs in italicJune McEnroe 2020-02-26Add Fierce Femmes and Notorious LiarsJune McEnroe 2020-02-23Add This Is How You Lose the Time WarJune McEnroe 2020-02-22Add See Ya LaterJune McEnroe 2020-02-20Remove wiki scriptJune McEnroe 2020-02-19Add The Obelisk GateJune McEnroe 2020-02-17Add Four Tet — HandsJune McEnroe 2020-02-12Simplify macOS notify-sendJune McEnroe 2020-02-12Add imbox and notemap to pageJune McEnroe 2020-02-12Collapse simple linksJune McEnroe 2020-02-12Move catgirl up the pageJune McEnroe 2020-02-12Update catgirl pty grabJune McEnroe 2020-02-12Link to cgit /about pages where appropriateJune McEnroe 2020-02-11Separate LINKS from BINS for html to workJune McEnroe 2020-02-11Add margin to Bl-bullet itemsJune McEnroe 2020-02-10Match URLs inside parens or with paired parens insideJune McEnroe 2020-02-10Duplicate effective URL before passing it back to curlJune McEnroe 2020-02-09Add To Be Taught, If FortunateJune McEnroe 2020-02-04Add The Future of Another TimelineJune McEnroe 2020-01-31Reorganize the Makefile for the umpteenth timeJune McEnroe 2020-01-28Change scout sensitivity to 1.4June McEnroe 2020-01-28Import shows.txtJune McEnroe