simplify 02.sh a bit lol

This commit is contained in:
yosh 2022-12-02 14:02:09 -05:00
parent c600e6198c
commit 4d9fbe730e
1 changed files with 12 additions and 25 deletions

37
02.sh
View File

@ -1,32 +1,19 @@
#!/bin/sh
set -eu
# "oneline" versions follow each function
part1() {
# 3,1 6,2 0,3 0,1 3,2 6,3 6,1 0,2 3,3
set -- 4 8 3 1 5 9 7 2 6
sort 02_input | uniq -c | awk '{print $1}' | while read l; do
printf "$l" | sed s/$/*$1+/ # if you go for the less magic-numbery version you can do sed "s/$/*(${1%,*}+${1#*,})/"
shift
done | sed s/+$/\\n/ | bc
}
# set -- 4 8 3 1 5 9 7 2 6; sort 02_input | uniq -c | awk '{print $1}' | while read l; do printf "$l" | sed s/$/*$1+/; shift; done | sed s/+$/\\n/ | bc
part2() {
# 0,3 3,1 6,2 0,1 3,2 6,3 0,2 3,3 6,1
set -- 3 4 8 1 5 9 2 6 7
sort 02_input | uniq -c | awk '{print $1}' | while read l; do
printf "$l" | sed s/$/*$1+/ # same comment as before
shift
done | sed s/+$/\\n/ | bc
}
# set -- 3 4 8 1 5 9 2 6 7; sort 02_input | uniq -c | awk '{print $1}' | while read l; do printf "$l" | sed s/$/*$1+/; shift; done | sed s/+$/\\n/ | bc
while getopts :12 OPT; do
# part 1 values
# 3,1 6,2 0,3 0,1 3,2 6,3 6,1 0,2 3,3
set -- 4 8 3 1 5 9 7 2 6
while getopts :2 OPT; do
case $OPT in
1) part1 && exit 0 ;;
2) part2 && exit 0 ;;
# 0,3 3,1 6,2 0,1 3,2 6,3 0,2 3,3 6,1
2) set -- 3 4 8 1 5 9 2 6 7 ;;
*) echo "bad option" 1>&2; exit 1 ;;
esac
done
# sort 02_input | uniq -c | awk '{print $1}' | while read l; do printf "$l" | sed s/$/*$1+/; shift; done | sed s/+$/\\n/ | bc
sort 02_input | uniq -c | awk '{print $1}' | while read l; do
printf "$l" | sed s/$/*$1+/ # if you go for the less magic-numbery version you can do sed "s/$/*(${1%,*}+${1#*,})/"
shift
done | sed s/+$/\\n/ | bc