rename repo, subdir for yearly challenges
This commit is contained in:
32
2020/day13/ex2.bash
Executable file
32
2020/day13/ex2.bash
Executable file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex2.bash: Advent2020 game, day 13/game 2.
|
||||
|
||||
CMD=${0##*/}
|
||||
shopt -s extglob
|
||||
|
||||
declare -a buses btimes
|
||||
declare -i lcm=1 thetime=0 c=0 t=0
|
||||
|
||||
read -r line
|
||||
read -r line
|
||||
IFS=,
|
||||
for i in ${line}; do
|
||||
if [[ $i != x ]]; then
|
||||
buses[$c]=$i
|
||||
btimes[$c]=$t
|
||||
((c++))
|
||||
fi
|
||||
((t++))
|
||||
done
|
||||
for ((i=0; i<(${#buses[@]}-1); ++i)); do
|
||||
bus=${buses[i+1]}
|
||||
idx=${btimes[i+1]}
|
||||
((lcm*=buses[i]))
|
||||
while ((((thetime + idx) % bus) != 0)); do
|
||||
((thetime+=lcm))
|
||||
done
|
||||
|
||||
done
|
||||
printf "%s : res=%d\n" "$CMD" "$thetime"
|
||||
exit 0
|
Reference in New Issue
Block a user