aboutsummaryrefslogtreecommitdiff
path: root/challenge-088/duncan-c-white/perl/ch-2.pl
blob: 9ddc7977534b3dadd1b42afd1e1fd8b6581cc14d (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/perl
#
# Task 2: "Spiral Matrix
# 
# You are given m x n matrix of positive integers.
# 
# Write a script to print a spiral path throught the matrix as list.
# 
# Example 1:
# 
# Input:
#     [ 1, 2, 3 ]
#     [ 4, 5, 6 ]
#     [ 7, 8, 9 ]
# Output:
#     [ 1, 2, 3, 6, 9, 8, 7, 4, 5 ]
# 
# Example 2:
# 
# Input:
#     [  1,  2,  3,  4 ]
#     [  5,  6,  7,  8 ]
#     [  9, 10, 11, 12 ]
#     [ 13, 14, 15, 16 ]
# Output:
#     [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]
# "
# 
# My notes: clearly defined.  Is "a spiral path" clear?  Think so.
# How to do this?  Given non-square matrix:
#  1,  2,  3,  4,  5
#  6,  7,  8,  9, 10
# 11, 12, 13, 14, 15
# 16, 17, 18, 19, 20
# 
# - we first consume whole top row: 1  2  3  4  5
# matrix now:
#  6  7  8  9 10
# 11 12 13 14 15
# 16 17 18 19 20
# - consume whole last column, top to bottom: 10 15 20
# matrix now:
#  6  7  8  9
# 11 12 13 14
# 16 17 18 19
# - consume whole bottom row, backwards: 19 18 17 16
#  matrix now:
#  6  7  8  9
# 11 12 13 14
# - consume whole first column, bottom to top: 11 6
#  matrix now:
#  7  8  9
# 12 13 14
# - repeat: consume whole top row: 7 8 9
#  matrix now:
# 12 13 14
# - consume whole last column: 14
#  matrix now:
# 12 13
# - consume whole bottom row, backwards: 13 12
# matrix empty
#

use strict;
use warnings;
use Data::Dumper;
use Function::Parameters;
use feature 'say';

# Read the matrix ignoring everything but digits and commas..
my @m;
my $rowlen = undef;
while( <> )
{
	chomp;
	tr/0-9,//cd;
	my @row = split( /,/ );
	unless( defined $rowlen )
	{
		$rowlen = @row;
	} else
	{
		die "matrix: line $. ($_) of wrong length, should be $rowlen\n"
			unless $rowlen == @row;
	}
	push @m, \@row;
}
#say Dumper(\@m);

#
# my @l = spiral( $ncols, @m );
#	Trace a spiral path through @m, each row of which has
#	$ncols columns.  Return the list of matrix elements.
#
fun spiral( $ncols, @m )
{
	my @l;
	while( @m )
	{
		# consume top row
		my $toprow = shift @m;
		push @l, @$toprow;

	last if @m==0;

		# consume last column
		foreach my $row (@m)
		{
			my $val = pop(@$row);
			push @l, $val;
		}

		$ncols--;

	last if $ncols==0;

		# consume bottom row, backwards
		my $bottomrow = pop @m;
		push @l, reverse @$bottomrow;

	last if @m==0;

		# consume first column, upwards
		my @rev;
		foreach my $row (@m)
		{
			my $val = shift(@$row);
			push @rev, $val;
		}
		push @l, reverse @rev;

		$ncols--;

	last if $ncols==0;

	}
	return @l;
}

my @l = spiral( $rowlen, @m );
say for @l;