diff options
| author | 冯昶 <seaker@qq.com> | 2021-03-15 18:18:09 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-03-15 18:18:09 +0800 |
| commit | 5ed25077fde85262036c9db3e893d70ae0907b5c (patch) | |
| tree | 8932d25b3fa6076e2d91ab2a331d4d8bfff20544 /challenge-103/abigail/lua | |
| parent | 8b6be37fe4dac8b4c6489a95e55514b76b298d15 (diff) | |
| parent | 65d54d52500028ec5359a7d39619803ade281543 (diff) | |
| download | perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.tar.gz perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.tar.bz2 perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-103/abigail/lua')
| -rw-r--r-- | challenge-103/abigail/lua/ch-1.lua | 45 | ||||
| -rw-r--r-- | challenge-103/abigail/lua/ch-2.lua | 81 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-103/abigail/lua/ch-1.lua b/challenge-103/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..352158ea06 --- /dev/null +++ b/challenge-103/abigail/lua/ch-1.lua @@ -0,0 +1,45 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +-- +-- We're reading years from standard input, one year per line, outputting +-- years from the sexagenary cycle [1]. This is slightly more than what +-- the challenge ask; the challenge asks to output the heavenly stem [2], +-- and the earthly branch [3]. But we also output its Yin/Yang. +-- +-- [1] https://en.wikipedia.org/wiki/Sexagenary_cycle +-- [2] https://en.wikipedia.org/wiki/Heavenly_Stems +-- [3] https://en.wikipedia.org/wiki/Earthly_Branches +-- + +-- +-- Each of the cycles have been rotated so the first entry corresponds to +-- the year 0 in the Proleptic Gregorian calendar. (We're using the +-- convention of having a year 0, as per ISO 8601). +-- That way, we can just mod the year with the number of entries, without +-- first having to subtract something from the year. +-- +-- The heavenly stems last for 2 years, so we just duplicate the entries. +-- + +local yin_yang = {"Yang", "Yin"}; +local heavenly_stems = {"Metal", "Metal", "Water", "Water", + "Wood", "Wood", "Fire", "Fire", + "Earth", "Earth"}; +local earthly_branches = {"Monkey", "Rooster", "Dog", "Pig", + "Rat", "Ox", "Tiger", "Rabbit", + "Dragon", "Snake", "Horse", "Goat"}; + +for line in io . lines () do + local year = tonumber (line) + io . write (yin_yang [1 + (year % #yin_yang)], " ", + heavenly_stems [1 + (year % #heavenly_stems)], " ", + earthly_branches [1 + (year % #earthly_branches)], "\n") +end 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 + + |
