aboutsummaryrefslogtreecommitdiff
path: root/challenge-348/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-348/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-348/roger-bell-west/lua/ch-2.lua57
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-348/roger-bell-west/lua/ch-2.lua b/challenge-348/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..c8c13ba2c4
--- /dev/null
+++ b/challenge-348/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,57 @@
+#! /usr/bin/lua
+
+function hm2m(a)
+ local h, m = string.match(a, "^(%d+):(%d+)$")
+ return h * 60 + m
+end
+
+function converttime(ssrc, ttgt)
+ local src = hm2m(ssrc)
+ local tgt = hm2m(ttgt)
+ if tgt < src then
+ tgt = tgt + 24 * 60
+ end
+ local delta = tgt - src
+ local oc = 0
+ for _, op in ipairs({60, 15, 5, 1}) do
+ oc = oc + delta // op
+ delta = delta % op
+ end
+ return oc
+end
+
+if converttime("02:30", "02:45") == 1 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if converttime("11:55", "12:15") == 2 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if converttime("09:00", "13:00") == 4 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if converttime("23:45", "00:30") == 3 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if converttime("14:20", "15:25") == 2 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+