From aaece858e772a505f4c92c067f087ff892eb9000 Mon Sep 17 00:00:00 2001 From: setop Date: Sun, 4 Dec 2022 17:57:20 +0100 Subject: [PATCH] day 3, part 1 in awk and compat part 2 --- d03/part1.awk | 12 ++++++++++++ d03/part2.awk | 26 +++++++------------------- 2 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 d03/part1.awk diff --git a/d03/part1.awk b/d03/part1.awk new file mode 100644 index 0000000..2627988 --- /dev/null +++ b/d03/part1.awk @@ -0,0 +1,12 @@ +{ + split(substr($0,1,length($0)/2),arr,"") + r = substr($0,1+length($0)/2) + for (a in arr) { + c = arr[a] + if (index(r,c)>0) { + comm = c + } + } + S += index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", comm) +} +END {print S} diff --git a/d03/part2.awk b/d03/part2.awk index 7e003a7..bcaae31 100644 --- a/d03/part2.awk +++ b/d03/part2.awk @@ -1,25 +1,13 @@ -NR %3 == 1 { split($0, arr, "") - for (a in arr) { - A[arr[a]]=1 - } -} -NR %3 == 2 { split($0, arr, "") - for (a in arr) { - B[arr[a]]=1 - } -} -NR %3 == 0 { - split($0, arr, "") +NR%3==1 {split($0,arr,"");for(a in arr){A[arr[a]]=1}} +NR%3==2 {split($0,arr,"");for(a in arr){B[arr[a]]=1}} +NR%3==0 { + split($0,arr,"") for (a in arr) { c = arr[a] - if ((c in A) && (c in B)) { - comm = c - } + if ((c in A)&&(c in B)) {comm = c} } - i = index("abcdefghijklmnopqrstuvwxyz", comm) - j = index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", comm)+26 - S += i > 0 ? i : j + S += index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", comm) delete A delete B } -END { print S} +END {print S}