diff --git a/d02/combine.awk b/d02/combine.awk new file mode 100644 index 0000000..397bd00 --- /dev/null +++ b/d02/combine.awk @@ -0,0 +1,14 @@ +# emit a report and its variants as a bloc, separated by empty line +{ + print + for (i=1;i<=NF;i++) { + r = "" + for (j=1;j<=NF;j++) { + if (i!=j) { + r = r $j " " + } + } + print r + } + print "" +} diff --git a/d02/part1.awk b/d02/part1.awk new file mode 100644 index 0000000..6608735 --- /dev/null +++ b/d02/part1.awk @@ -0,0 +1,17 @@ +{ + p = ($2 -$1) < 0 ? -1 : 1 + safe = 1 + for (i=2;i<=NF;i++) { + d = ($i - $(i-1)) + q = d < 0 ? -1 : 1 + if (p != q || d>3 || d<-3 || d == 0) { + safe = 0 + break + } + } + #print NR, $0 , p, safe + S += safe +} +END { + print S +} diff --git a/d02/part2.awk b/d02/part2.awk new file mode 100644 index 0000000..bfcf4ea --- /dev/null +++ b/d02/part2.awk @@ -0,0 +1,25 @@ +# 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 +}