aoc22

2022 Advent of Code
git clone https://git.parazyd.org/aoc22
Log | Files | Refs

02.sh (587B)


      1 #!/bin/sh
      2 
      3 priority() {
      4 	case "$1" in
      5 	[a-z]) python -c "print(ord('$1') - 96)" ;;
      6 	[A-Z]) python -c "print(ord('$1') - 38)" ;;
      7 	esac
      8 }
      9 
     10 answer=0
     11 
     12 while read -r line0; read -r line1; read -r line2; do
     13 	ffd="$(mktemp)"
     14 	sfd="$(mktemp)"
     15 	tfd="$(mktemp)"
     16 	tmp="$(mktemp)"
     17 
     18 	echo "$line0" | fold -w1 | sort -u > "$ffd"
     19 	echo "$line1" | fold -w1 | sort -u > "$sfd"
     20 	echo "$line2" | fold -w1 | sort -u > "$tfd"
     21 
     22 	comm -12 "$ffd" "$sfd" | sort -u > "$tmp"
     23 
     24 	prio="$(priority "$(comm -12 "$tmp" "$tfd")")"
     25 
     26 	answer="$(( answer + prio ))"
     27 
     28 	rm -f "$ffd" "$sfd" "$tfd" "$tmp"
     29 done
     30 
     31 echo "$answer"