day 2 - original solution

This commit is contained in:
yosh 2022-12-02 12:58:14 -05:00
parent 35e42e74ac
commit 3c74beb814
3 changed files with 2533 additions and 0 deletions

32
02.sh Executable file
View File

@ -0,0 +1,32 @@
#!/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

2500
02_input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,3 +4,4 @@ mainly going to be using shell + posix-defined utilities, probably
## Days
1. awk (uses features exclusive to GNU's implementation, gawk)
2. shell (completely POSIX-conforming implementation)