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}