diff options
| author | Abigail <abigail@abigail.be> | 2021-03-14 13:32:34 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-14 19:59:46 +0100 |
| commit | d545f046bf04384f9b4f716a7d7acdb321f16e7d (patch) | |
| tree | abc51b064cd16909248854aa9e656137cfcaa838 | |
| parent | 88733249abc9cc7297417b70be218d35cf2ef064 (diff) | |
| download | perlweeklychallenge-club-d545f046bf04384f9b4f716a7d7acdb321f16e7d.tar.gz perlweeklychallenge-club-d545f046bf04384f9b4f716a7d7acdb321f16e7d.tar.bz2 perlweeklychallenge-club-d545f046bf04384f9b4f716a7d7acdb321f16e7d.zip | |
Ruby solution for week 103, part 2
| -rw-r--r-- | challenge-103/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-103/abigail/ruby/ch-2.rb | 67 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md index bbf2a50037..d35895761f 100644 --- a/challenge-103/abigail/README.md +++ b/challenge-103/abigail/README.md @@ -126,5 +126,6 @@ Output: * [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [Ruby](ruby/ch-2.rb) ### Blog diff --git a/challenge-103/abigail/ruby/ch-2.rb b/challenge-103/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..f4695983a4 --- /dev/null +++ b/challenge-103/abigail/ruby/ch-2.rb @@ -0,0 +1,67 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < 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. +# + +ARGF . each_line do |_| + start_time, end_time, file_name = _ . split + + # + # Ruby automatically creates big integers if necessary. + # + time_diff = (end_time . to_i - start_time . to_i) * 1000 + + tracks = [] + total_time = 0 + File . open (file_name) do |f| + f . each_line do |track| + run_time, title = track . split ',', 2 + run_time = run_time . to_i + total_time += run_time + tracks . push [run_time, title] + end + end + + # + # We're not interested in playing full loops. + # + 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. + # + tracks . each do |track| + if time_diff - track [0] < 0 + time_diff = (time_diff / 1000) . to_i + hours = (time_diff / 3600) . to_i + minutes = ((time_diff % 3600) / 60) . to_i + seconds = time_diff % 60 + puts (track [1]) + if hours > 0 + puts "%02d:%02d:%02d" % [hours, minutes, seconds] + else + puts "%02d:%02d" % [ minutes, seconds] + end + break + else + time_diff -= track [0] + end + end +end |
