aboutsummaryrefslogtreecommitdiff
path: root/challenge-128/abigail/lua/ch-2.lua
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-08-30 21:20:10 +0200
committerAbigail <abigail@abigail.be>2021-08-30 21:20:10 +0200
commitb3641076d24ab77f4e12016553ea64940b06d7fb (patch)
tree31f65c8a2c4f4d0e70c97373c8f83fbe0632be31 /challenge-128/abigail/lua/ch-2.lua
parent83559ddd9bede17f2141bf98e61fd36f538a9e61 (diff)
downloadperlweeklychallenge-club-b3641076d24ab77f4e12016553ea64940b06d7fb.tar.gz
perlweeklychallenge-club-b3641076d24ab77f4e12016553ea64940b06d7fb.tar.bz2
perlweeklychallenge-club-b3641076d24ab77f4e12016553ea64940b06d7fb.zip
Lua solution for week 128, part 2
Diffstat (limited to 'challenge-128/abigail/lua/ch-2.lua')
-rw-r--r--challenge-128/abigail/lua/ch-2.lua67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-128/abigail/lua/ch-2.lua b/challenge-128/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..371e5991e4
--- /dev/null
+++ b/challenge-128/abigail/lua/ch-2.lua
@@ -0,0 +1,67 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+
+--
+-- Read the input, and convert it to minutes (from midnight)
+--
+
+local arrivals = {}
+local departures = {}
+
+for hour, minute in io . read ("*l") : gmatch ("([0-9][0-9]):([0-9][0-9])") do
+ arrivals [#arrivals + 1] = 60 * tonumber (hour) + tonumber (minute)
+end
+for hour, minute in io . read ("*l") : gmatch ("([0-9][0-9]):([0-9][0-9])") do
+ departures [#departures + 1] = 60 * tonumber (hour) + tonumber (minute)
+end
+
+--
+-- Initialize the trains array, which counts the number of trains
+-- in the station on each minute of the day.
+--
+local trains = {}
+for i = 0, 24 * 60 - 1 do
+ trains [i] = 0
+end
+
+--
+-- Process each train
+--
+for i, arrival in ipairs (arrivals) do
+ local departure = departures [i]
+ if arrival < departure then
+ for i = arrival, departure do
+ trains [i] = trains [i] + 1
+ end
+ else
+ for i = 0, arrival do
+ trains [i] = trains [i] + 1
+ end
+ for i = departures, 24 * 60 - 1 do
+ trains [i] = trains [i] + 1
+ end
+ end
+end
+
+--
+-- Find the maximum
+--
+local max = 0
+for i, count in ipairs (trains) do
+ if max < count then
+ max = count
+ end
+end
+
+--
+-- And print it
+--
+print (max)