summary refs log tree commit diff homepage
path: root/2022/day03.awk
blob: 8a48d691eaca3b2b16e5de8d67df18366dfd64d1 (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
function common(a, b) {
	split(a, as, "");
	for (i in as) {
		if (index(b, as[i])) return as[i];
	}
}
function common3(a, b, c) {
	split(a, as, "");
	for (i in as) {
		if (index(b, as[i]) && index(c, as[i])) return as[i];
	}
}
function priority(x) {
	return index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", x);
}
{
	a = substr($1, 1, length($1)/2);
	b = substr($1, length($1)/2 + 1);
	sum1 += priority(common(a, b));
	g1 = g2;
	g2 = g3;
	g3 = $1;
	if (NR % 3 == 0) {
		sum2 += priority(common3(g1, g2, g3));
	}
}
END {
	print sum1;
	print sum2;
}