aboutsummaryrefslogtreecommitdiff
path: root/2019/06/puzzle-2.awk
blob: 3d0e42ced1c2b6e900aa4883c3ae8c0865aafada (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
#!/usr/bin/awk -f

function trace_to_com(xs, o)
{
	if (o == "COM")
		return
	o = orbits[o]
	xs[length(xs) + 1] = o
	trace_to_com(xs, o)
}

BEGIN { FS = ")" }

{ orbits[$2] = $1 }

END {
	# Declare ‘xs’ and ‘ys’ as arrays
	xs[0]; delete xs
	ys[0]; delete ys

	trace_to_com(xs, "YOU")
	trace_to_com(ys, "SAN")

	for (i = 1; i <= length(xs); i++) {
		for (j = 1; j <= length(ys); j++) {
			if (xs[i] == ys[j]) {
				print i + j - 2
				exit
			}
		}
	}
}