aboutsummaryrefslogtreecommitdiff
path: root/2024/07/puzzles.awk
blob: 53b23d3b38f2237699d73fff8529beb9e61819ae (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
34
35
36
37
#!/usr/bin/gawk -f

@load "intdiv"

# START PART 2
function pow(x, e,    n)
{
	n = 1
	while (e--)
		n *= x
	return n
}
# END PART 2

function eval(i, acc,    a)
{
	if (acc <= 0)
		return 0
	if (i == 2)
		return acc == $2

# START PART 2
	intdiv(acc, pow(10, length($i)), a)
	if (a["remainder"] == $i && eval(i - 1, a["quotient"]))
		return 1
# END PART 2

	intdiv(acc, $i, a)
	if (a["remainder"] == 0 && eval(i - 1, a["quotient"]))
		return 1

	return eval(i - 1, acc - $i)
}

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