summary refs log tree commit diff homepage
path: root/2022/day09.awk
blob: 95e4fb4605d6bc73330c327df60b72e0da869f5e (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
BEGIN {
	t[0,0] = 1;
}
function abs(x) {
	if (x < 0) return -x;
	return x;
}
function step() {
	rx = tx-hx;
	ry = ty-hy;
	if (abs(rx) <= 1 && abs(ry) <= 1) return;
	if (rx == 0) {
		ty += (ry < 0 ? +1 : -1);
	} else if (ry == 0) {
		tx += (rx < 0 ? +1 : -1);
	} else {
		tx += (rx < 0 ? +1 : -1);
		ty += (ry < 0 ? +1 : -1);
	}
	t[tx,ty] = 1;
}
{
	dx = 0;
	dy = 0;
	if ($1 == "U") dy = -1;
	if ($1 == "D") dy = +1;
	if ($1 == "L") dx = -1;
	if ($1 == "R") dx = +1;
	for (i = 1; i <= $2; ++i) {
		hx += dx;
		hy += dy;
		step();
	}
}
END {
	for (i in t) {
		sum++;
	}
	print sum;
}