mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Add solution to day 2.
In bash, for a change.
This commit is contained in:
27
2017/day-02/solution.sh
Executable file
27
2017/day-02/solution.sh
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user