aboutsummaryrefslogtreecommitdiff
path: root/challenge-068/colin-crain/perl/ch-1.pl
blob: 421508f45e465c4fe242cb1c7e126348fc6bdf47 (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
#! /opt/local/bin/perl
#
#       zip-zilch-zero.pl
#
#         TASK #1 › Zero Matrix
#         Submitted by: Mohammad S Anwar
#         You are given a matrix of size M x N having only 0s and 1s.
#
#         Write a script to set the entire row and column to 0 if an
#         element is 0.
#
#         Example 1
#             Input:  [1, 0, 1]
#                     [1, 1, 1]
#                     [1, 1, 1]
#
#             Output: [0, 0, 0]
#                     [1, 0, 1]
#                     [1, 0, 1]
#         Example 2
#             Input:  [1, 0, 1]
#                     [1, 1, 1]
#                     [1, 0, 1]
#
#             Output: [0, 0, 0]
#                     [1, 0, 1]
#                     [0, 0, 0]
#
#
#       2020 colin crain
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##



use warnings;
use strict;
use feature ":5.26";

## ## ## ## ## MAIN:

## as by challenge definition we only use 1s and 0s,
## concatenate individual rows into strings
## example: 101 111 111 001
my @matrix;
push @matrix, map { [ split //, $_ ] } @ARGV;
my $rows = @matrix;
my $cols = $matrix[0]->@*;

print_matrix(\@matrix, "Input:");

## 0s are considered 'opaque' -- a single 0 occludes the entire row or column
## We pass once through the matrix, row by row, recording the 0 occurence data to
## two arrays, one for rows, the other columns
my @row_zeros = (0) x $rows;
my @col_zeros = (0) x $cols;

for my $row_idx (0..$rows-1) {
    my $sum = 0;
    for my $col_idx ( 0..$cols-1) {
        $sum += $matrix[$row_idx]->[$col_idx];
        $col_zeros[$col_idx] |= ! $matrix[$row_idx]->[$col_idx];
    }
    $row_zeros[$row_idx] = 1 if $sum != $cols;
}

say<<__END__;

Zero Occlusions:
    cols: @col_zeros
    rows: @row_zeros
__END__

## now we can pass through the matrix again, transferring the occurence
## data back to the rows and columns, zeroing them out as specified
for my $row_idx (0..$rows-1) {
    if ($row_zeros[$row_idx] == 1) {
        $matrix[$row_idx] = [ (0) x $cols ];
        next;
    }
    for my $col_idx ( 0..$cols-1) {
        $matrix[$row_idx]->[$col_idx] = 0 if $col_zeros[$col_idx] == 1;
    }
}

print_matrix(\@matrix, "Output:");

## ## ## ## ## SUBS:

sub print_matrix {
    my ($matrix, $heading ) = @_;
    say "$heading";
    for ($matrix->@*) {
        say "\t[ ", (join ', ', $_->@*), " ]";
    }
}