aboutsummaryrefslogtreecommitdiff
path: root/2024/07/puzzles.awk
blob: 45597e971cdfe36928602974624679c8654645fa (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
#!/usr/bin/gawk -f

@load "intdiv"

# START PART 2
function pow(x, e,    n)
{
	for (n = 1; 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 }