aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/abigail/lua/ch-1.lua
blob: 23262855b37065033ac5dd38924497aa89d58c79 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/opt/local/bin/lua

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

--
-- Run as: lua ch-1.lua < input-file
--

local RIGHT = 0
local UP    = 1
local LEFT  = 2
local DOWN  = 3

--
-- Read in the input: a single line, with the elemens separated by white space
--
local line = io . read ()
local elements = {}
for element in line : gmatch ("%S+") do
    elements [#elements + 1] = element
end

--
-- Calculate the optimal width and height
--
local height = math . floor (math . sqrt (#elements))
while #elements % height > 0 do
    height = height - 1
end
local width = #elements / height

--
-- Fill the matrix, starting from the bottom left
--
local min_x     = 1
local max_x     = height
local min_y     = 1
local max_y     = width
local x         = max_x
local y         = min_y
local direction = RIGHT

--
-- Initialize the matrix
--
local matrix = {}
for x = 1, height do
    matrix [x] = {}
end

for i = 1, #elements do
    matrix [x] [y] = elements [i]
    local turn = 0

    if   direction == RIGHT
    then if   y >= max_y
         then turn =  1
              x = x - 1
              max_x = max_x - 1
         else y = y + 1
         end
    end

    if   direction == UP
    then if   x <= min_x
         then turn =  1
              y = y - 1
              max_y = max_y - 1
         else x = x - 1
         end
    end

    if   direction == LEFT
    then if   y <= min_y
         then turn =  1
              x = x + 1
              min_x = min_x + 1
         else y = y - 1
         end
    end

    if   direction == DOWN
    then if   x >= max_x
         then turn =  1
              y = y + 1
              min_y = min_y + 1
         else x = x + 1
         end
    end

    if   turn > 0
    then direction = (direction + 1) % 4
    end

end

--
-- Find the maximum widths in each column
--
local widths = {}
for  y = 1, width
do   local max = 0
     for x = 1, height
     do   if   max < #matrix [x] [y]
          then max = #matrix [x] [y]
          end
     end
     widths [y] = max
end


--
-- Pretty print the matrix
--
for  x = 1, height
do   for  y = 1, width
     do   if   y > 1
          then io . write (" ")
          end
          local format = "%" .. widths [y] .. "s"
          io . write (string . format (format, matrix [x] [y]))
     end
     io . write ("\n")
end