day 3, part 1 in awk and compat part 2

This commit is contained in:
setop 2022-12-04 17:57:20 +01:00
parent 3f67d3091d
commit aaece858e7
2 changed files with 19 additions and 19 deletions

12
d03/part1.awk Normal file
View File

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

View File

@ -1,24 +1,12 @@
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==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
}