Codinget 1 year ago
commit 7ee787f976
  1. 1
      .gitignore
  2. 55
      day1/day1.sh

1
.gitignore vendored

@ -0,0 +1 @@
input

@ -0,0 +1,55 @@
#!/bin/bash
replacedigits() {
rep="$1"
i="0"
len="${#rep}"
ret=""
while [ $i -lt ${len} ]; do
case "${rep:${i}:1}" in
0|1|2|3|4|5|6|7|8|9)
ret="${ret}${rep:${i}:1}"
i=$((i+1))
continue
;;
esac
#echo ${i} ${rep} ${rep:${i}:1}
#echo ${rep:${i}:3} ${rep:${i}:4} ${rep:${i}:5}
if [ ${rep:${i}:3} = one ]; then
ret="${ret}1"
elif [ ${rep:${i}:3} = two ]; then
ret="${ret}2"
elif [ ${rep:${i}:5} = three ]; then
ret="${ret}3"
elif [ ${rep:${i}:4} = four ]; then
ret="${ret}4"
elif [ ${rep:${i}:4} = five ]; then
ret="${ret}5"
elif [ ${rep:${i}:3} = six ]; then
ret="${ret}6"
elif [ ${rep:${i}:5} = seven ]; then
ret="${ret}7"
elif [ ${rep:${i}:5} = eight ]; then
ret="${ret}8"
elif [ ${rep:${i}:4} = nine ]; then
ret="${ret}9"
fi
i="$((i+1))"
done
echo "$ret"
}
sum=0;
for number in $(cat "$1" | sed s/[^0-9]//g); do
sum=$((sum+${number:0:1}*10+${number:${#number}-1:${#number}}))
done
printf "part 1: %d\n" $sum
sum=0;
for line in $(cat "$1"); do
number="$(replacedigits $line)"
digits="$((10*${number:0:1}+${number:$((${#number}-1)):${#number}}))"
#printf "%s %s %s\n" $line $number $digits
sum=$((sum+${digits}))
done
printf "part 2: %d\n" $sum
Loading…
Cancel
Save