aboutsummaryrefslogtreecommitdiff
path: root/challenge-128/abigail/lua/ch-2.lua
blob: 522cf769601a3597091761c31ad3230ecbe57812 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/opt/local/bin/lua

--
-- See ../README.md
--

--
-- Run as: lua ch-2.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, departure do
            trains [i] = trains [i] + 1
        end
        for i = arrival, 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)