diff options
Diffstat (limited to '3')
-rwxr-xr-x | 3 | 48 |
1 files changed, 48 insertions, 0 deletions
@@ -0,0 +1,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 |