37 lines
872 B
Awk
37 lines
872 B
Awk
BEGIN { FS=""
|
|
# make a barebones "value" array for any character
|
|
for (n=1;n<=26;n++) {
|
|
v[sprintf("%c", n+96)] = n
|
|
v[sprintf("%c", n+64)] = n + 26
|
|
}
|
|
}
|
|
{
|
|
# part 1
|
|
h = length($0)/2
|
|
split(substr($0, h+1), a)
|
|
for (c in a)
|
|
if (index(substr($0,1,h), a[c])) { # find the index of each "2nd half" character in the 1st half, if it exists (!= 0) then add value to t
|
|
t += v[a[c]]
|
|
break
|
|
}
|
|
# part 2
|
|
mem = (mem + 1) % 3 # count member in group
|
|
split($0, ma)
|
|
for (i in ma)
|
|
if (s[ma[i]] != 1) { # this is a really basic and naive (?) way to check if you've already seen a character but I wrote this at 03:00 AM cut me some slack
|
|
ga[ma[i]] += 1
|
|
s[ma[i]] = 1
|
|
}
|
|
split("", s) # this clears array s
|
|
if (mem == 0) {
|
|
for (c in ga)
|
|
if (ga[c] == 3)
|
|
gt += v[c]
|
|
split("", ga)
|
|
}
|
|
}
|
|
END {
|
|
print "compartment total: " t
|
|
print "three-group total: " gt
|
|
}
|