Files
adventofcode/2017/day-02/solution.sh
Bert Peters 12f76678dc Add solution to day 2.
In bash, for a change.
2017-12-02 11:01:35 +01:00

28 lines
439 B
Bash
Executable File

#!/bin/bash
sum1=0
sum2=0
while IFS="\n" read line; do
sorted=$(echo $line | xargs -n 1 echo | sort -n)
checksum=$(($(echo "$sorted" | tail -n 1) - $(echo "$sorted" | head -n 1)))
for a in $sorted; do
for b in $sorted; do
if [[ $a -le $b ]]; then
continue
fi
if [[ $((a % b)) -eq 0 ]]; then
sum2=$((sum2 + a / b))
break
fi
done
done
sum1=$((sum1 + checksum))
done
echo Sum 1: $sum1
echo Sum 2: $sum2