summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2018/day22.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/2018/day22.c b/2018/day22.c
new file mode 100644
index 0000000..459f8cb
--- /dev/null
+++ b/2018/day22.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef unsigned uint;
+
+enum Type {
+	Rocky,
+	Wet,
+	Narrow,
+};
+
+static uint depth;
+static uint targetX, targetY;
+
+static uint erosionLevel(uint x, uint y);
+
+static uint geologicIndex(uint x, uint y) {
+	if (x == 0 && y == 0) return 0;
+	if (x == targetX && y == targetY) return 0;
+	if (y == 0) return x * 16807;
+	if (x == 0) return y * 48271;
+	return erosionLevel(x - 1, y) * erosionLevel(x, y - 1);
+}
+
+static uint erosionLevels[1000][1000];
+
+static uint erosionLevel(uint x, uint y) {
+	if (!erosionLevels[x][y]) {
+		erosionLevels[x][y] = (geologicIndex(x, y) + depth) % 20183;
+	}
+	return erosionLevels[x][y];
+}
+
+static enum Type type(uint x, uint y) {
+	return erosionLevel(x, y) % 3;
+}
+
+int main(void) {
+	scanf("depth: %u\n", &depth);
+	scanf("target: %u,%u\n", &targetX, &targetY);
+
+	uint riskLevel = 0;
+	for (uint x = 0; x <= targetX; ++x) {
+		for (uint y = 0; y <= targetY; ++y) {
+			riskLevel += type(x, y);
+		}
+	}
+	printf("%u\n", riskLevel);
+}
oe 2019-10-02Add The Red Threads of FortuneJune McEnroe 2019-09-28Add The Black Tides of HeavenJune McEnroe 2019-09-27Fail on HTTP failure status in titleJune McEnroe 2019-09-23Add Trail of LightningJune McEnroe 2019-09-22Revert "Enable cookies in title"June McEnroe 2019-09-20Enable cookies in titleJune McEnroe 2019-09-16Use sensitivity aliases in TF2June McEnroe 2019-09-16Add The Just CityJune McEnroe 2019-09-12Only GET the final redirect locationJune McEnroe 2019-09-12Consume entire bodyJune McEnroe 2019-09-10Add title -v flagJune McEnroe 2019-09-10Use curl error bufferJune McEnroe 2019-09-10Set Accept-Encoding in titleJune McEnroe 2019-09-08Set title User-AgentJune McEnroe 2019-09-07Add -x flag to titleJune McEnroe 2019-09-07Ignore SIGPIPE in relayJune McEnroe 2019-09-07Add A Memory Called EmpireJune McEnroe 2019-09-05Handle lack of Content-TypeJune McEnroe 2019-09-05Use CURLINFO_CONTENT_TYPEJune McEnroe 2019-09-05Decode entities in titlesJune McEnroe 2019-09-05Print title as soon as it's availableJune McEnroe 2019-09-05Use CURL_PREFIX to set flagsJune McEnroe 2019-09-05Add titleJune McEnroe 2019-09-04Add Avorter n'est pas tuerJune McEnroe 2019-08-29Unset executable on shell scriptsJune McEnroe 2019-08-29Add long-missing setopt to bin.7June McEnroe 2019-08-29Add editJune McEnroe