blob: bf416b6a92e22498e782470a34aefa1358813a9e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/bin/sh -eu
find_uniq() (
printf "$1" | fold -w 1 | sort -u | tr -d '\n'
)
find_dup() (
printf "$1" | fold -w 1 | sort | uniq -d
)
prioritize() (
ord=$1
if [ $ord -lt 97 ]
then
p=$((ord - 38))
else
p=$((ord - 96))
fi
printf "$p"
)
p1() (
sum=0
while IFS= read -r l
do
c1=$(find_uniq $(printf "$l" | cut -c-$((${#l} / 2))))
c2=$(find_uniq $(printf "$l" | cut -c$((${#l} / 2 + 1))-))
dup=$(find_dup "${c1}${c2}")
sum=$((sum + $(prioritize $(printf "%d\n" "'$dup"))))
done < input.txt
printf "%s\n" "$sum"
)
p2() (
sum=0
while IFS= read -r r1; IFS= read -r r2; IFS= read -r r3
do
r1=$(find_uniq "$r1")
r2=$(find_uniq "$r2")
r3=$(find_uniq "$r3")
dup=$(find_dup "${r1}${r2}")
dup=$(find_dup "${dup}${r3}")
sum=$((sum + $(prioritize $(printf "%d\n" "'$dup"))))
done < input.txt
printf "%s\n" "$sum"
)
p1
p2
|