01.sh - aoc22 - 2022 Advent of Code
HTML git clone https://git.parazyd.org/aoc22
DIR Log
DIR Files
DIR Refs
---
01.sh (579B)
---
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 line; do
13 first="$(echo "$line" | awk '{print substr($0,1,length/2);}')"
14 secnd="$(echo "$line" | awk '{print substr($0,length/2+1);}')"
15
16 ffd="$(mktemp)"
17 sfd="$(mktemp)"
18
19 printf "%s" "$first" | fold -w1 | sort -u > "$ffd"
20 printf "%s" "$secnd" | fold -w1 | sort -u > "$sfd"
21
22 common="$(comm -12 "$ffd" "$sfd")"
23 prio="$(priority "$common")"
24 answer="$(( answer + prio ))"
25
26 rm -f "$ffd" "$sfd"
27 done
28
29 echo "$answer"