aboutsummaryrefslogtreecommitdiff
path: root/2024/02/puzzles.awk
blob: da9ac93be3f463038c9b53e60a5aab0379ccbb5b (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
38
39
40
41
42
43
#!/usr/bin/gawk -f

function sign(x)
{
	return x < 0 ? -1 : +1
}

function abs(x)
{
	return x < 0 ? -x : x
}

function check(skip,    xs, s, i, j, d, D)
{
	for (i = 1; i <= NF; i++) {
		if (i != skip)
			xs[++j] = $i
	}
	s = sign(xs[1] - xs[2])
	for (i = 2; i <= length(xs); i++) {
		d = xs[i - 1] - xs[i]
		D = abs(d)
		if (sign(d) != s || D < 1 || D > 3)
			return 0
	}
	return 1
}

# START PART 1
{ n += check() }
# END PART 1 START PART 2
!check() {
	for (i = 1; i <= NF; i++) {
		if (check(i))
			break
	}
	if (i > NF)
		next
}
{ n++ }
# END PART 2

END { print n }