aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-103/abigail/README.md1
-rw-r--r--challenge-103/abigail/bash/ch-2.sh62
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md
index 9d90314025..69c67fa04e 100644
--- a/challenge-103/abigail/README.md
+++ b/challenge-103/abigail/README.md
@@ -124,6 +124,7 @@ Output:
### Solutions
* [GNU AWK](awk/ch-2.awk)
+* [Bash](bash/ch-2.sh)
* [C](c/ch-2.c)
* [Lua](lua/ch-2.lua)
* [Perl](perl/ch-2.pl)
diff --git a/challenge-103/abigail/bash/ch-2.sh b/challenge-103/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..e35d48c034
--- /dev/null
+++ b/challenge-103/abigail/bash/ch-2.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+set -f
+
+#
+# Parse input
+#
+while read start_time current_time file_name
+do time_diff=$(((current_time - start_time) * 1000))
+
+ #
+ # Read the media file, storing run times and titles
+ # into two arrays.
+ #
+ declare -a run_times
+ declare -a titles
+ total_time=0
+
+ nr_of_tracks=0
+ while read track
+ do
+ run_time=${track/,*/}
+ title=${track/[^,]*,/}
+ run_times[$nr_of_tracks]=$run_time
+ titles[$nr_of_tracks]=$title
+ ((nr_of_tracks ++))
+ total_time=$((total_time + run_time))
+ done < $file_name
+
+ #
+ # Don't care about full loops played.
+ #
+ time_diff=$((time_diff % total_time))
+
+ #
+ # Find the right track.
+ #
+ for ((i = 0; i < nr_of_tracks; i ++))
+ do
+ if ((time_diff - run_times[i] < 0))
+ then echo ${titles[$i]}
+ time_diff=$((time_diff / 1000))
+ hours=$(( time_diff / 3600))
+ minutes=$(( (time_diff % 3600) / 60))
+ seconds=$(( time_diff % 60))
+ if ((hours > 0))
+ then printf "%02d:%02d:%02d\n" $hours $minutes $seconds
+ else printf "%02d:%02d\n" $minutes $seconds
+ fi
+ break
+ else time_diff=$((time_diff - run_times[i]))
+ fi
+ done
+done