aboutsummaryrefslogtreecommitdiff
path: root/challenge-337/bob-lied/perl/ch-2.pl
blob: 40c6ed9100705f1c5bba0165d6aef1cbe5f2c846 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2025, Bob Lied
#=============================================================================
# ch-2.pl Perl Weekly Challenge 337 Task 2  Odd Matrix
#=============================================================================
# You are given row and col, also a list of positions in the matrix.
# Write a script to perform action on each location (0-indexed) as provided
# in the list and find out the total odd valued cells.
# For each location (r, c), do both of the following:
#   a) Increment by 1 all the cells on row r.
#   b) Increment by 1 all the cells on column c.
#
# Example 1 Input: $row = 2, $col = 3, @locations = ([0,1],[1,1])
#           Output: 6
#                                   Before     After
#   Apply [0,1]: Increment row 0:
#                                   [ 0 0 0 ]  [ 1 1 1 ]
#                                   [ 0 0 0 ]  [ 0 0 0 ]
#                 Increment col 1:
#                                   [ 1 1 1 ]  [ 1 2 1 ]
#                                   [ 0 0 0 ]  [ 0 1 0 ]
#   Apply [1,1]: Increment row 1 and col 1
#                                   [ 1 2 1 ]  [ 1 3 1 ]
#                                   [ 1 2 1 ]  [ 1 3 1 ]
#
# Example 2 Input: $row = 2, $col = 2, @locations = ([1,1],[0,0])
#           Output: 0
#                                   Before     After
#   Apply [1,1]: Increment row 1:
#                                   [ 0 0 ]   [ 0 0 ]
#                                   [ 0 0 ]   [ 1 1 ]
#               Increment col 1:
#                                   [ 0 0 ]   [ 0 1 ]
#                                   [ 1 1 ]   [ 1 2 ]
#   Apply [0,0]: Increment row 0 and col 0:
#                                   [ 1 2 ]   [ 2 2 ]
#                                   [ 1 2 ]   [ 2 2 ]
#
# Example 3 Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1])
#           Output: 0
#                                   Before     After
#   Apply [0,0]: Increment row 0 and col 0:
#                                   [ 1 1 1 ]  [ 2 1 1 ]
#                                   [ 0 0 0 ]  [ 1 0 0 ]
#                                   [ 0 0 0 ]  [ 1 0 0 ]
#   Apply [1,2]: Increment row 1 and col 2:
#                                   [ 2 1 1 ]  [ 2 1 2 ]
#                                   [ 2 1 1 ]  [ 2 1 2 ]
#                                   [ 1 0 0 ]  [ 1 0 1 ]
#   Apply [2,1]: Increment row 2 and col 1:
#                                   [ 2 1 2 ]  [ 2 2 2 ]
#                                   [ 2 1 2 ]  [ 2 2 2 ]
#                                   [ 2 1 2 ]  [ 2 2 2 ]
#
# Example 4 Input: $row = 1, $col = 5, @locations = ([0,2],[0,4])
#           Output: 2
#                                   Before         After
#   Apply [0,2]: Increment row 0 and  col 2:
#                                   [ 1 1 1 1 1 ]  [ 1 1 2 1 1 ]
#   Apply [0,4]: Increment row 0 and col 4:
#                                   [ 2 2 3 2 2 ]  [ 2 2 3 2 3 ]
#
# Example 5 Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1])
#           Output: 8
#                                   Before     After
#   Apply [1,0]: Increment row 1 and col 0:
#                                   [ 0 0 ]    [ 1 0 ]
#                                   [ 1 1 ]    [ 2 1 ]
#                                   [ 0 0 ]    [ 1 0 ]
#                                   [ 0 0 ]    [ 1 0 ]
#   Apply [3,1]: Increment row 3 and col 1:
#                                   [ 1 0 ]    [ 1 1 ]
#                                   [ 2 1 ]    [ 2 2 ]
#                                   [ 1 0 ]    [ 1 1 ]
#                                   [ 2 1 ]    [ 2 2 ]
#   Apply [2,0]: Increment row 2 and col 0:
#                                   [ 1 1 ]    [ 2 1 ]
#                                   [ 2 2 ]    [ 3 2 ]
#                                   [ 2 2 ]    [ 3 2 ]
#                                   [ 2 2 ]    [ 3 2 ]
#   Apply [0,1]: Increment row 0 and col 1:
#                                   [ 3 2 ]    [ 3 3 ]
#                                   [ 3 2 ]    [ 3 3 ]
#                                   [ 3 2 ]    [ 3 3 ]
#                                   [ 3 2 ]    [ 3 3 ]
#=============================================================================

use v5.42;


use Getopt::Long;
my $Verbose = false;
my $DoTest  = false;

GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
my $logger;
{
    use Log::Log4perl qw(:easy);
    Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ),
            layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" });
    $logger = Log::Log4perl->get_logger();
}
#=============================================================================

exit(!runTest()) if $DoTest;

my $ROW = shift;
my $COL = shift;
my @LOC = map { [ split ',' ] } @ARGV;
try { say oddMatrix($ROW, $COL, @LOC); }
catch ( $e ) { say "Error: $e", "\n\t", "Usage: row col  r,c r,c" }

#=============================================================================
# Logging/debugging aid
#=============================================================================
sub showMatrix($m)
{
    return join("\n",  map { '[ ' . join(" ", $_->@*) . ' ]' } $m->@*);
}

sub oddMatrix($row, $col, @location)
{
    use List::Util qw/sum0/;
    my @matrix;
    push @matrix, [ (0) x $col ] for 1 .. $row;
    $row--; $col--; # Switch to 0-based indexing

    for (@location)
    {
        my ($r, $c) = $_->@*;
        die "Location ($r,$c) outside of ($row,$col)" if ( $r > $row || $c > $col );
        $matrix[$r][$_]++ for 0 .. $col;
        $matrix[$_][$c]++ for 0 .. $row;
        $logger->debug("After [$_->@*]:\n", showMatrix(\@matrix) ) if $Verbose;
    }

    my $oddCount = sum0 map { scalar grep { $_ % 2 } $_->@* } @matrix;
    return $oddCount;
}

sub runTest
{
    use Test2::V0;
    use Test::Exception;

    is( oddMatrix(2,3, [0,1],[1,1]             ), 6, "Example 1");
    is( oddMatrix(2,2, [1,1],[0,0]             ), 0, "Example 2");
    is( oddMatrix(3,3, [0,0],[1,2],[2,1]       ), 0, "Example 3");
    is( oddMatrix(1,5, [0,2],[0,4]             ), 2, "Example 4");
    is( oddMatrix(4,2, [1,0],[3,1],[2,0],[0,1] ), 8, "Example 5");

    is( oddMatrix(1,1, [0,0]                   ), 0, "1x1");
    like( dies { oddMatrix(2,2, [1,1],[2,2]), }, qr/Location/,  "Location out of range dies");

    done_testing;
}