aoc-2022/02.sh

33 lines
1.0 KiB
Bash
Executable File

#!/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
case $OPT in
1) part1 && exit 0 ;;
2) part2 && exit 0 ;;
*) echo "bad option" 1>&2; exit 1 ;;
esac
done