summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-04 21:06:59 -0500
committerJune McEnroe <june@causal.agency>2018-12-04 21:06:59 -0500
commitf6d1527ef4283024f33181b1aa614b94e7c743e9 (patch)
tree48e22ac63bf4780167ecf8480affea594a0e2d24 /2018
parentSolve day 3 part 2 (diff)
downloadaoc-f6d1527ef4283024f33181b1aa614b94e7c743e9.tar.gz
aoc-f6d1527ef4283024f33181b1aa614b94e7c743e9.zip
Solve day 4 part 1
Diffstat (limited to '2018')
-rw-r--r--2018/day04.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/2018/day04.c b/2018/day04.c
new file mode 100644
index 0000000..5d2939f
--- /dev/null
+++ b/2018/day04.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned uint;
+
+enum { Len = 4096 };
+uint guards[Len][60];
+
+int main() {
+	uint id, sleep;
+	while (!feof(stdin)) {
+		uint year, month, day, hour, minute;
+		char word[256];
+		scanf(
+			"[%u-%u-%u %u:%u] %255s #%u",
+			&year, &month, &day, &hour, &minute, word, &id
+		);
+		scanf("%*[^\n]\n");
+
+		if (!strcmp(word, "falls")) sleep = minute;
+		if (!strcmp(word, "wakes")) {
+			for (uint m = sleep; m < minute; ++m) {
+				guards[id][m]++;
+			}
+		}
+	}
+	uint maxID = 0, maxSum = 0;
+	for (uint i = 0; i < Len; ++i) {
+		uint sum = 0;
+		for (uint m = 0; m < 60; ++m) {
+			sum += guards[i][m];
+		}
+		if (sum < maxSum) continue;
+		maxSum = sum;
+		maxID = i;
+	}
+	uint maxMin = 0, maxSleep = 0;
+	for (uint m = 0; m < 60; ++m) {
+		if (guards[maxID][m] < maxSleep) continue;
+		maxSleep = guards[maxID][m];
+		maxMin = m;
+	}
+	printf("%u\n", maxID * maxMin);
+}