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
|
#!/usr/bin/awk
#
# See ../README.md
#
#
# Run as: awk -f ch-1.awk < input-file
#
#
# Map 2-D coordinates to 1-D coordinates, as AWK doesn't
# have multidimensional arrays.
#
function idx (x, y, h, w) {
return x * w + y + 1
}
BEGIN {
RIGHT = 0
UP = 1
LEFT = 2
DOWN = 3
}
{
#
# Get the data; we're assuming it's one a single line,
# seperated by white space.
#
split ($0, elements)
#
# Find optimal sizes: width w and height h
# We start at the square root, and count downwards till we
# have a divider.
#
n = length (elements)
h = int (sqrt (n))
for (;n % h;) {
h --
}
w = n / h
#
# Fill the matrix, starting from the bottom left
#
min_x = 0
max_x = h - 1
min_y = 0
max_y = w - 1
x = max_x
y = 0
direction = RIGHT
for (i = 1; i <= length (elements); i ++) {
matrix [idx(x, y, h, w)] = elements [i]
turn = 0
if (direction == RIGHT) {
if (y >= max_y) {turn = 1; x --; max_x --}
else {y ++}
}
if (direction == UP) {
if (x <= min_x) {turn = 1; y --; max_y --}
else {x --}
}
if (direction == LEFT) {
if (y <= min_y) {turn = 1; x ++; min_x ++}
else {y --}
}
if (direction == DOWN) {
if (x >= max_x) {turn = 1; y ++; min_y ++}
else {x ++}
}
if (turn) {
direction ++
direction %= 4
}
}
#
# Find the max widths for each columns.
#
for (y = 0; y < w; y ++) {
max = 0;
for (x = 0; x < h; x ++) {
l = length (matrix [idx(x, y, h, w)])
if (l > max) {
max = l
}
}
widths [y + 1] = max
}
#
# Print the matrix, with each column outlined
#
for (x = 0; x < h; x ++) {
for (y = 0; y < w; y ++) {
width = widths [y + 1]
format = "%s%" width "s"
printf (format, y == 0 ? "" : " ", matrix [idx(x, y, h, w)])
}
printf ("\n")
}
}
|