The weekly challenge 337 - Task 2: Odd Matrix
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-337/#TASK2 3 # 4 # Task 2: Odd Matrix 5 # ================== 6 # 7 # You are given row and col, also a list of positions in the matrix. 8 # 9 # Write a script to perform action on each location (0-indexed) as provided in 10 # the list and find out the total odd valued cells. 11 # 12 # For each location (r, c), do both of the following: 13 # 14 # a) Increment by 1 all the cells on row r. 15 # b) Increment by 1 all the cells on column c. 16 # 17 # 18 ## Example 1 19 ## 20 ## Input: $row = 2, $col = 3, @locations = ([0,1],[1,1]) 21 ## Output: 6 22 ## 23 ## Initial: 24 ## [ 0 0 0 ] 25 ## [ 0 0 0 ] 26 ## 27 ## Apply [0,1]: 28 ## Increment row 0: 29 ## Before After 30 ## [ 0 0 0 ] [ 1 1 1 ] 31 ## [ 0 0 0 ] [ 0 0 0 ] 32 ## Increment col 1: 33 ## Before After 34 ## [ 1 1 1 ] [ 1 2 1 ] 35 ## [ 0 0 0 ] [ 0 1 0 ] 36 ## 37 ## Apply [1,1]: 38 ## Increment row 1: 39 ## Before After 40 ## [ 1 2 1 ] [ 1 2 1 ] 41 ## [ 0 1 0 ] [ 1 2 1 ] 42 ## Increment col 1: 43 ## Before After 44 ## [ 1 2 1 ] [ 1 3 1 ] 45 ## [ 1 2 1 ] [ 1 3 1 ] 46 ## 47 ## Final: 48 ## [ 1 3 1 ] 49 ## [ 1 3 1 ] 50 # 51 # 52 ## Example 2 53 ## 54 ## Input: $row = 2, $col = 2, @locations = ([1,1],[0,0]) 55 ## Output: 0 56 ## 57 ## Initial: 58 ## [ 0 0 ] 59 ## [ 0 0 ] 60 ## 61 ## Apply [1,1]: 62 ## Increment row 1: 63 ## Before After 64 ## [ 0 0 ] [ 0 0 ] 65 ## [ 0 0 ] [ 1 1 ] 66 ## Increment col 1: 67 ## Before After 68 ## [ 0 0 ] [ 0 1 ] 69 ## [ 1 1 ] [ 1 2 ] 70 ## 71 ## Apply [0,0]: 72 ## Increment row 0: 73 ## Before After 74 ## [ 0 1 ] [ 1 2 ] 75 ## [ 1 2 ] [ 1 2 ] 76 ## Increment col 0: 77 ## Before After 78 ## [ 1 2 ] [ 2 2 ] 79 ## [ 1 2 ] [ 2 2 ] 80 ## 81 ## Final: 82 ## [ 2 2 ] 83 ## [ 2 2 ] 84 # 85 # 86 ## Example 3 87 ## 88 ## Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1]) 89 ## Output: 0 90 ## 91 ## Initial: 92 ## [ 0 0 0 ] 93 ## [ 0 0 0 ] 94 ## [ 0 0 0 ] 95 ## 96 ## Apply [0,0]: 97 ## Increment row 0: 98 ## Before After 99 ## [ 0 0 0 ] [ 1 1 1 ] 100 ## [ 0 0 0 ] [ 0 0 0 ] 101 ## [ 0 0 0 ] [ 0 0 0 ] 102 ## Increment col 0: 103 ## Before After 104 ## [ 1 1 1 ] [ 2 1 1 ] 105 ## [ 0 0 0 ] [ 1 0 0 ] 106 ## [ 0 0 0 ] [ 1 0 0 ] 107 ## 108 ## Apply [1,2]: 109 ## Increment row 1: 110 ## Before After 111 ## [ 2 1 1 ] [ 2 1 1 ] 112 ## [ 1 0 0 ] [ 2 1 1 ] 113 ## [ 1 0 0 ] [ 1 0 0 ] 114 ## Increment col 2: 115 ## Before After 116 ## [ 2 1 1 ] [ 2 1 2 ] 117 ## [ 2 1 1 ] [ 2 1 2 ] 118 ## [ 1 0 0 ] [ 1 0 1 ] 119 ## 120 ## Apply [2,1]: 121 ## Increment row 2: 122 ## Before After 123 ## [ 2 1 2 ] [ 2 1 2 ] 124 ## [ 2 1 2 ] [ 2 1 2 ] 125 ## [ 1 0 1 ] [ 2 1 2 ] 126 ## Increment col 1: 127 ## Before After 128 ## [ 2 1 2 ] [ 2 2 2 ] 129 ## [ 2 1 2 ] [ 2 2 2 ] 130 ## [ 2 1 2 ] [ 2 2 2 ] 131 ## 132 ## Final: 133 ## [ 2 2 2 ] 134 ## [ 2 2 2 ] 135 ## [ 2 2 2 ] 136 # 137 # 138 ## Example 4 139 ## 140 ## Input: $row = 1, $col = 5, @locations = ([0,2],[0,4]) 141 ## Output: 2 142 ## 143 ## Initial: 144 ## [ 0 0 0 0 0 ] 145 ## 146 ## Apply [0,2]: 147 ## Increment row 0: 148 ## Before After 149 ## [ 0 0 0 0 0 ] [ 1 1 1 1 1 ] 150 ## Increment col 2: 151 ## Before After 152 ## [ 1 1 1 1 1 ] [ 1 1 2 1 1 ] 153 ## 154 ## Apply [0,4]: 155 ## Increment row 0: 156 ## Before After 157 ## [ 1 1 2 1 1 ] [ 2 2 3 2 2 ] 158 ## Increment col 4: 159 ## Before After 160 ## [ 2 2 3 2 2 ] [ 2 2 3 2 3 ] 161 ## 162 ## Final: 163 ## [ 2 2 3 2 3 ] 164 # 165 # 166 ## Example 5 167 ## 168 ## Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1]) 169 ## Output: 8 170 ## 171 ## Initial: 172 ## [ 0 0 ] 173 ## [ 0 0 ] 174 ## [ 0 0 ] 175 ## [ 0 0 ] 176 ## 177 ## Apply [1,0]: 178 ## Increment row 1: 179 ## Before After 180 ## [ 0 0 ] [ 0 0 ] 181 ## [ 0 0 ] [ 1 1 ] 182 ## [ 0 0 ] [ 0 0 ] 183 ## [ 0 0 ] [ 0 0 ] 184 ## Increment col 0: 185 ## Before After 186 ## [ 0 0 ] [ 1 0 ] 187 ## [ 1 1 ] [ 2 1 ] 188 ## [ 0 0 ] [ 1 0 ] 189 ## [ 0 0 ] [ 1 0 ] 190 ## 191 ## Apply [3,1]: 192 ## Increment row 3: 193 ## Before After 194 ## [ 1 0 ] [ 1 0 ] 195 ## [ 2 1 ] [ 2 1 ] 196 ## [ 1 0 ] [ 1 0 ] 197 ## [ 1 0 ] [ 2 1 ] 198 ## Increment col 1: 199 ## Before After 200 ## [ 1 0 ] [ 1 1 ] 201 ## [ 2 1 ] [ 2 2 ] 202 ## [ 1 0 ] [ 1 1 ] 203 ## [ 2 1 ] [ 2 2 ] 204 ## 205 ## Apply [2,0]: 206 ## Increment row 2: 207 ## Before After 208 ## [ 1 1 ] [ 1 1 ] 209 ## [ 2 2 ] [ 2 2 ] 210 ## [ 1 1 ] [ 2 2 ] 211 ## [ 2 2 ] [ 2 2 ] 212 ## Increment col 0: 213 ## Before After 214 ## [ 1 1 ] [ 2 1 ] 215 ## [ 2 2 ] [ 3 2 ] 216 ## [ 2 2 ] [ 3 2 ] 217 ## [ 2 2 ] [ 3 2 ] 218 ## 219 ## Apply [0,1]: 220 ## Increment row 0: 221 ## Before After 222 ## [ 2 1 ] [ 3 2 ] 223 ## [ 3 2 ] [ 3 2 ] 224 ## [ 3 2 ] [ 3 2 ] 225 ## [ 3 2 ] [ 3 2 ] 226 ## Increment col 1: 227 ## Before After 228 ## [ 3 2 ] [ 3 3 ] 229 ## [ 3 2 ] [ 3 3 ] 230 ## [ 3 2 ] [ 3 3 ] 231 ## [ 3 2 ] [ 3 3 ] 232 ## 233 ## Final: 234 ## [ 3 3 ] 235 ## [ 3 3 ] 236 ## [ 3 3 ] 237 ## [ 3 3 ] 238 # 239 ############################################################ 240 ## 241 ## discussion 242 ## 243 ############################################################ 244 # 245 # First, we create the intial Matrix filled with 0's. Then we check 246 # each element of the @locations array: Add 1 to each element of the 247 # corresponding column and row. Then just walk the matrix once, counting 248 # the odd elements. 249 # 250 251 use v5.36; 252 253 odd_matrix(2, 3, [0,1],[1,1]); 254 odd_matrix(2, 2, [1,1],[0,0]); 255 odd_matrix(3, 3, [0,0],[1,2],[2,1]); 256 odd_matrix(1, 5, [0,2],[0,4]); 257 odd_matrix(4, 2, [1,0],[3,1],[2,0],[0,1]); 258 259 sub odd_matrix($row, $col, @locations) { 260 say "Input: $row, $col, (" . join(", ", map { "[$_->[0],$_->[1]]" } @locations), ")"; 261 my $matrix; 262 foreach my $r (0..$row-1) { 263 my $tmp = []; 264 foreach my $c (0..$col-1) { 265 push @$tmp, 0; 266 } 267 push @$matrix, $tmp; 268 } 269 foreach my $loc (@locations) { 270 my ($r, $c) = @$loc; 271 foreach my $col1 (0..scalar(@{$matrix->[0]})-1) { 272 $matrix->[$r]->[$col1]++; 273 } 274 foreach my $row1 (0..scalar(@{$matrix})-1) { 275 $matrix->[$row1]->[$c]++; 276 } 277 } 278 my $output = 0; 279 foreach my $r (0..$row-1) { 280 foreach my $c (0..$col-1) { 281 $output++ if $matrix->[$r]->[$c] % 2; 282 } 283 } 284 say "Output: $output"; 285 } 286 287