diff options
| author | Abigail <abigail@abigail.be> | 2021-03-14 16:55:22 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-14 19:59:46 +0100 |
| commit | cc9c06d6417c7a55db2a2740f852c9c056f2a77d (patch) | |
| tree | b3d5b75e6bd8d58f53674ac692cd3937909c984c | |
| parent | d545f046bf04384f9b4f716a7d7acdb321f16e7d (diff) | |
| download | perlweeklychallenge-club-cc9c06d6417c7a55db2a2740f852c9c056f2a77d.tar.gz perlweeklychallenge-club-cc9c06d6417c7a55db2a2740f852c9c056f2a77d.tar.bz2 perlweeklychallenge-club-cc9c06d6417c7a55db2a2740f852c9c056f2a77d.zip | |
GNU AWK solution for week 103, part 2
| -rw-r--r-- | challenge-103/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-103/abigail/awk/ch-2.gawk | 75 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md index d35895761f..4059ddea7e 100644 --- a/challenge-103/abigail/README.md +++ b/challenge-103/abigail/README.md @@ -123,6 +123,7 @@ Output: ~~~~ ### Solutions +* [GNU AWK](awk/ch-2.awk) * [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) diff --git a/challenge-103/abigail/awk/ch-2.gawk b/challenge-103/abigail/awk/ch-2.gawk new file mode 100644 index 0000000000..301730b203 --- /dev/null +++ b/challenge-103/abigail/awk/ch-2.gawk @@ -0,0 +1,75 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: gawk -f ch-2.gawk < 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. +# +{ + start_time = $1 + current_time = $2 + file_name = $3 + + time_diff = (current_time - start_time) * 1000 + + nr_of_tracks = 1 + total_time = 0 + while ((getline track < file_name > 0)) { + # + # AWK doesn't have the ability to limit the number of + # fields a string is split into, so we use index() and + # substring() instead of split() to get the info. + # + j = index (track, ",") + run_time = substr (track, 1, j - 1) + title = substr (track, j + 1) + tracks [nr_of_tracks, 0] = run_time + tracks [nr_of_tracks, 1] = title + nr_of_tracks ++ + + total_time += run_time + } + + # + # 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. + # + for (i = 1; i <= nr_of_tracks; i ++) { + if (time_diff - tracks [i, 0] < 0) { + time_diff = int (time_diff / 1000) + hours = int (time_diff / 3600) + minutes = int ((time_diff % 3600) / 60) + seconds = time_diff % 60 + print tracks [i, 1] + if (hours > 0) { + printf ("%02d:%02d:%02d\n", hours, minutes, seconds) + } + else { + printf ( "%02d:%02d\n", minutes, seconds) + } + + break + } + else { + time_diff -= tracks [i, 0] + } + } +} |
