logan_2023/day1/day1p2.sh

28 lines
1.2 KiB
Bash
Raw Normal View History

2023-12-03 01:09:26 -05:00
#!/bin/bash
# This is known as a 4AM abomination
# You should not imitate this
2023-12-03 01:09:26 -05:00
SUM=0
while read f; do
TEMP1=($(echo $f | awk '{
while (match($0, /([0-9]|one|two|three|four|five|six|seven|eight|nine)/)) {
print substr($0, RSTART, RLENGTH)
$0 = substr($0, RSTART + RLENGTH)
}
}' | sed -e 's/[0-9]/\0 /g;s/one/\0 /g;s/two/\0 /g;s/three/\0 /g;s/four/\0 /g;s/five/\0 /g;s/six/\0 /;s/seven/\0 /;s/eight/\0 /;s/nine/\0 /g'))
TEMP2=($(echo $f | rev | awk '{
while (match($0, /([0-9]|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin)/)) {
print substr($0, RSTART, RLENGTH)
$0 = substr($0, RSTART + RLENGTH)
}
}' | sed -e 's/[0-9]/\0 /g;s/eno/\0 /g;s/owt/\0 /g;s/eerht/\0 /g;s/ruof/\0 /g;s/evif/\0 /g;s/xis/\0 /;s/neves/\0 /;s/thgie/\0 /;s/enin/\0 /g'))
FIRST=$( echo ${TEMP1[0]} | sed -e 's/one/1/g;s/two/2/g;s/three/3/g;s/four/4/g;s/five/5/g;s/six/6/g;s/seven/7/g;s/eight/8/g;s/nine/9/g')
LAST=$( echo ${TEMP2[0]} | rev | sed -e 's/one/1/g;s/two/2/g;s/three/3/g;s/four/4/g;s/five/5/g;s/six/6/g;s/seven/7/g;s/eight/8/g;s/nine/9/g')
echo $f: $FIRST$LAST
SUM=$(( $SUM + $FIRST$LAST ))
#sleep 1
done < "$1"
2023-12-03 01:09:26 -05:00
echo $SUM