summary refs log tree commit diff homepage
path: root/2022/day02.awk
blob: d01b15df36f4d1c683fc8c925a87b281673ba29b (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
function outcome(a, b) {
	if (a == 1 && b == 3) return 0;
	if (a == 2 && b == 1) return 0;
	if (a == 3 && b == 2) return 0;
	if (b == 1 && a == 3) return 6;
	if (b == 2 && a == 1) return 6;
	if (b == 3 && a == 2) return 6;
	return 3;
}
function score(a, b) {
	return b + outcome(a, b);
}
{
	a = index("ABC", $1);
	b = index("XYZ", $2);
	total1 += score(a, b);
	if (b == 1) {
		if (a == 1) b = 3;
		if (a == 2) b = 1;
		if (a == 3) b = 2;
	} else if (b == 2) {
		b = a;
	} else if (b == 3) {
		if (a == 1) b = 2;
		if (a == 2) b = 3;
		if (a == 3) b = 1;
	}
	total2 += score(a, b);
}
END {
	print total1;
	print total2;
}