aoc2024/d02/part2.awk

26 lines
473 B
Awk
Raw Normal View History

2024-12-02 08:15:41 +00:00
# run with awk -f combine.awk < input | awk -f part2.awk
length == 0 {
#print "onesafe", onesafe
S+= onesafe>0 # if at least one variant is safe, then report is safe
onesafe = 0
next
}
{
p = ($2 -$1) < 0 ? -1 : 1 # uphill or downhill
safe = 1
for (i=2;i<=NF;i++) {
d = ($i - $(i-1))
q = d < 0 ? -1 : 1
if (q != p || d>3 || d<-3 || d == 0) {
safe = 0
break
}
}
#print NR, $0 , p, safe
onesafe += safe
}
END {
print S
}