aboutsummaryrefslogtreecommitdiff
path: root/challenge-103
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-11 19:19:02 +0100
committerAbigail <abigail@abigail.be>2021-03-14 19:59:45 +0100
commit790b1c8fe5584b1c6e5754744d7c7d0922a1fd65 (patch)
treea9d44610e5639c4d8a26f515c86d2e13ba5f20c3 /challenge-103
parent52efe6c989305d458ec6b6424bb477246a5bad7a (diff)
downloadperlweeklychallenge-club-790b1c8fe5584b1c6e5754744d7c7d0922a1fd65.tar.gz
perlweeklychallenge-club-790b1c8fe5584b1c6e5754744d7c7d0922a1fd65.tar.bz2
perlweeklychallenge-club-790b1c8fe5584b1c6e5754744d7c7d0922a1fd65.zip
Lua solution for week 103, part 2
Diffstat (limited to 'challenge-103')
-rw-r--r--challenge-103/abigail/README.md1
-rw-r--r--challenge-103/abigail/lua/ch-2.lua81
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md
index 0f05539ef8..03b5dc3a3c 100644
--- a/challenge-103/abigail/README.md
+++ b/challenge-103/abigail/README.md
@@ -123,6 +123,7 @@ Output:
~~~~
### Solutions
+* [Lua](lua/ch-1.lua)
* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-103/abigail/lua/ch-2.lua b/challenge-103/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..dc39dc3e8a
--- /dev/null
+++ b/challenge-103/abigail/lua/ch-2.lua
@@ -0,0 +1,81 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+--
+-- We're reading a start time, the current time, and a file name
+-- from STDIN, separated by white space. The file contains the
+-- tracks played.
+--
+
+for line in io . lines () do
+ local _, _, start_time, current_time, file_name =
+ line : find ("(%d+)%s+(%d+)%s+(%S+)")
+
+ --
+ -- Time difference in milliseconds
+ --
+ local time_diff = (current_time - start_time) * 1000
+
+ local tracks = {}
+ local total_time = 0
+
+ --
+ -- Read in the media file; store the results in the
+ -- table tracks.
+ --
+ for track in io . lines (file_name) do
+ local _, _, play_time, title = track : find ("(%d+),(.*)")
+ local record = {}
+ record [0] = play_time
+ record [1] = title
+ total_time = total_time + play_time
+ table . insert (tracks, record)
+ end
+
+ --
+ -- We're not interested in playing full loops.
+ --
+ time_diff = time_diff % total_time
+
+ --
+ -- Find the current track playing. If time_diff is less than the
+ -- play time of the track, this is the track currently playing. If
+ -- so, print the track name (with quotes, as given in the example --
+ -- although they don't belong there, as they're from the CSV encoding;
+ -- for that reason, we won't deescape anything else either). Otherwise
+ -- (track length is shorter than time_diff), we subtrack the length
+ -- of the track from time_diff, and continue checking the next track.
+ --
+ for _, track in ipairs (tracks) do
+ if time_diff - track [0] < 0
+ then time_diff = math . floor (time_diff / 1000)
+ local hours = math . floor (time_diff / 3600)
+ local minutes = math . floor ((time_diff % 3600) / 60)
+ local seconds = time_diff % 60
+
+ --
+ -- Format a time string
+ --
+ local time
+ if hours > 0
+ then time = string .
+ format ("%02d:%02d:%02d", hours, minutes, seconds)
+ else time = string .
+ format ( "%02d:%02d", minutes, seconds)
+ end
+ print (track [1])
+ print (time)
+ break
+ else time_diff = time_diff - track [0]
+ end
+ end
+end
+
+