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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
#!perl
###############################################################################
=comment
Perl Weekly Challenge 101
=========================
Task #1
-------
*Pack a Spiral*
Submitted by: Stuart Little
You are given an array @A of items (integers say, but they can be anything).
Your task is to pack that array into an MxN matrix spirally counterclockwise,
as tightly as possible.
'Tightly' means the absolute value |M-N| of the difference has to be as
small as possible.
Example 1:
Input: @A = (1,2,3,4)
Output:
4 3
1 2
Since the given array is already a 1x4 matrix on its own, but that's not as
tight as possible. Instead, you'd spiral it counterclockwise into
4 3
1 2
Example 2:
Input: @A = (1..6)
Output:
6 5 4
1 2 3
or
5 4
6 3
1 2
Either will do as an answer, because they're equally tight.
Example 3:
Input: @A = (1..12)
Output:
9 8 7 6
10 11 12 5
1 2 3 4
or
8 7 6
9 12 5
10 11 4
1 2 3
=cut
###############################################################################
#--------------------------------------#
# Copyright © 2021 PerlMonk Athanasius #
#--------------------------------------#
use strict;
use warnings;
use Const::Fast;
use enum qw( RIGHT UP LEFT DOWN );
use Regexp::Common qw( number );
const my $USAGE =>
"Usage:
perl $0 [<A> ...]
[<A> ...] A non-empty array of integers and/or integer ranges: I..J\n";
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 101, Task #1: Pack a Spiral (Perl)\n\n";
}
#==============================================================================
MAIN:
#==============================================================================
{
# (1) Populate and validate the array
my @A = get_array();
printf "Input: \@A = (%s)\n", join ',', @A;
# (2) Find M <= N such that M * N == @A.elems and |M - N| is a minimum
my ($M, $N) = find_dimensions( \@A );
# (3) Create and populate the M x N matrix
my $matrix = pack_matrix( \@A, $M, $N );
# (4) Print the matrix
print "\nOutput:\n\n";
print_matrix( $matrix );
}
#------------------------------------------------------------------------------
sub get_array
#------------------------------------------------------------------------------
{
scalar @ARGV > 0 or error( 'Empty array' );
my @A;
for my $item (@ARGV)
{
if (my ($lhs, $rhs) = $item =~ / ^ (.+) \.\. (.+) $ /x)
{
for ($lhs, $rhs)
{
/ ^ $RE{num}{int} $ /x
or error( qq[Item "$_" is not an integer] );
}
push @A, $lhs .. $rhs;
}
else
{
$item =~ / ^ $RE{num}{int} $ /x
or error( qq[Item "$item" is not an integer] );
push @A, $item;
}
}
return @A;
}
#------------------------------------------------------------------------------
sub find_dimensions
#------------------------------------------------------------------------------
{
my ($A) = @_;
my ($M, $N) = (1, scalar @$A);
my $root = int sqrt $N;
if ($root * $root == $N)
{
($M, $N) = ($root, $root);
}
else
{
for my $div1 (reverse 2 .. $root)
{
my $div2 = int($N / $div1);
if ($div1 * $div2 == $N)
{
($M, $N) = ($div1, $div2);
last;
}
}
}
return $M, $N;
}
#------------------------------------------------------------------------------
sub pack_matrix
#------------------------------------------------------------------------------
{
my ($A, $M, $N) = @_;
my $max_row = $M - 1;
my $min_row = 0;
my $max_col = $N - 1;
my $min_col = 0;
my $row = $max_row;
my $col = -1;
my $dir = RIGHT;
my @matrix;
for my $item (@$A)
{
if ($dir == RIGHT)
{
if (++$col > $max_col)
{
$col = $max_col;
$dir = UP;
--$row;
--$max_row;
}
}
elsif ($dir == UP)
{
if (--$row < $min_row)
{
$row = $min_row;
$dir = LEFT;
--$col;
--$max_col;
}
}
elsif ($dir == LEFT)
{
if (--$col < $min_col)
{
$col = $min_col;
$dir = DOWN;
++$row;
++$min_row;
}
}
else # DOWN
{
if (++$row > $max_row)
{
$row = $max_row;
$dir = RIGHT;
++$col;
++$min_col;
}
}
$matrix[ $row ][ $col ] = $item;
}
return \@matrix;
}
#------------------------------------------------------------------------------
sub print_matrix
#------------------------------------------------------------------------------
{
my ($matrix) = @_;
my $max_row = $#$matrix;
my $max_col = $#{ $matrix->[ 0 ] };
my @widths;
# (1) Calculate maximum column widths
for my $col (0 .. $max_col)
{
my $max = length $matrix->[ 0 ][ $col ];
for my $row (1 .. $max_row)
{
my $len = length $matrix->[ $row ][ $col ];
$max = $len if $len > $max;
}
push @widths, $max;
}
# (2) Print the matrix
for my $row (0 .. $max_row)
{
print ' ' x 5;
for my $col (0 .. $max_col)
{
printf " %*d", $widths[ $col ], $matrix->[ $row ][ $col ];
}
print "\n";
}
}
#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
###############################################################################
|