aboutsummaryrefslogtreecommitdiff
path: root/2024/07/puzzles.awk
blob: 75564e8c3eae5e245aa4932a72ccf56aa47a91f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/gawk -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 }