# 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 }