aboutsummaryrefslogtreecommitdiff
path: root/2024/07/puzzles.awk
blob: 491a1b5432ee38dbe8969c1b8de31b4f78394b6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/awk -f

function eval(i, acc)
{
	if (acc > $1)
		return 0
	if (i == NF + 1)
		return acc == $1
# START PART 1
	return eval(i + 1, acc * $i) || eval(i + 1, acc + $i)
# END PART 1 START PART 2
	return eval(i + 1, (acc $i) + 0) || eval(i + 1, acc * $i) ||
		eval(i + 1, acc + $i)
# END PART 2
}

BEGIN       { FS = ":? " }
eval(3, $2) { n += $1 }
END         { print n }