summary refs log tree commit diff homepage
path: root/2018/day22.c
blob: 459f8cba2ba7147bafee7a95aa530ab2618171e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
}