Tuesday, September 16, 2025

TWC339

Challenge Link

Task1

We sort the array then find the difference of the two smallest and largest pairs:
#!/usr/bin/env perl
use strict;
use warnings;

sub max_diff{
  my ($arr) = @_;
  @$arr = sort{$a <=> $b} @$arr;
  $arr->[-1] * $arr->[-2] - $arr->[0] * $arr->[1]
}

printf "%d\n",max_diff([5,9,3,4,6]);
printf "%d\n",max_diff([1,-2,3,-4]);
printf "%d\n",max_diff([-3,-1,-2,-4]);
printf "%d\n",max_diff([10,2,0,5,1]);
printf "%d\n",max_diff([7,8,9,10,10]);

Task2

We take the maximum of the running sum of the given array:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub peak_point{
  my $s = 0;
  max 0,map {$s += $_} @{$_[0]}
}

printf "%d\n",peak_point([-5,1,5,-9,2]);
printf "%d\n",peak_point([10,10,10,-25]);
printf "%d\n",peak_point([3,-4,2,5,-6,1]);
printf "%d\n",peak_point([-1,-2,-3,-4]);
printf "%d\n",peak_point([-10,15,5]);

Monday, September 8, 2025

TWC338

Challenge Link

Task1

Sum each row and find their maximum:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0 max);

sub highest_row{
  max map{sum0 @$_} @{$_[0]}
}

printf "%d\n",highest_row([[4,4,4,4],
			   [10,0,0,0],
			   [2,2,2,9]]);
printf "%d\n",highest_row([[1,5],
			   [7,3],
			   [3,5]]);
printf "%d\n",highest_row([[1,2,3],
			   [3,2,1]]);
printf "%d\n",highest_row([[2,8,7],
			   [7,1,3],
			   [1,9,5]]);
printf "%d\n",highest_row([[10,20,30],
			   [5,5,5],
			   [0,100,0],
			   [25,25,25]]);

Task2

Find maximum distance between all elements of two lists:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub max_distance{
  my @pairs = map{
    my $x = $_;
    map {[$x,$_]} @{$_[1]}
  } @{$_[0]};
  max map {abs $_->[0] - $_->[1]} @pairs;
}

printf "%d\n",max_distance([4,5,7],[9,1,3,4]);
printf "%d\n",max_distance([2,3,5,4],[3,2,5,5,8,7]);
printf "%d\n",max_distance([2,1,11,3],[2,5,10,2]);
printf "%d\n",max_distance([1,2,3],[3,2,1]);
printf "%d\n",max_distance([1,0,2,3],[5,0]);

Wednesday, September 3, 2025

TWC337

Challenge Link

Task1

We count the numbers which are less than each element and store them at that element's index:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(sum0);
use List::MoreUtils qw(frequency);

sub smaller_than_current{
  my %h = frequency(@{$_[0]});
  my @arr;
  foreach my $i(0..$#{$_[0]}) {
    my %h2 = %h{grep{$_ <= $_[0]->[$i]} keys %h};
    @arr[$i] = sum0(values %h2) - 1
  }
  @arr
}

print show smaller_than_current([6,5,4,8]);
print show smaller_than_current([7,7,7,7]);
print show smaller_than_current([5,4,3,2,1]);
print show smaller_than_current([-1,0,3,-2,1]);
print show smaller_than_current([0,1,1,2,0]);

Task2

We do the simulation on a row x col matrix and count the odd numbers:
#!/usr/bin/env perl
use strict;
use warnings;

sub odd_matrix{
  my ($row,$col,$m) = @_;
  my @g;
  push @g,[(0) x $col] foreach(0..$row-1);
  foreach my $p(@$m){
    foreach(0..$col-1) {
      ++$g[$p->[0]]->[$_]
    }
    foreach(0..$row-1){
      ++$g[$_]->[$p->[1]]
    }
  }
  my $res = 0;
  foreach my $i(0..$row-1){
    foreach my $j(0..$col-1) {
      $res++ if $g[$i][$j] % 2
    }
  }
  $res
}

printf "%d\n", odd_matrix(2,3,[[0,1],[1,1]]);
printf "%d\n", odd_matrix(2,2,[[1,1],[0,0]]);
printf "%d\n", odd_matrix(3,3,[[0,0],[1,2],[2,1]]);
printf "%d\n", odd_matrix(1,5,[[0,2],[0,4]]);
printf "%d\n", odd_matrix(4,2,[[1,0],[3,1],[2,0],[0,1]]);

Monday, August 25, 2025

TWC336

Challenge Link

Task1

We check if the gcd of the frequencies of the elements is greater than one:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(reduce);
use ntheory qw(gcd);

sub equal_group{
  my %h;
  $h{$_}++ foreach @{$_[0]};
  (reduce{gcd($a,$b)} values %h) > 1
}

printf "%d\n",equal_group([1,1,2,2,2,2]);
printf "%d\n",equal_group([1,1,1,2,2,2,3,3]);
printf "%d\n",equal_group([5,5,5,5,5,5,7,7,7,7,7,7]);
printf "%d\n",equal_group([1,2,3,4]);
printf "%d\n",equal_group([8,8,9,9,10,10,11,11]);

Task2

We use a stack to evaluate the RPN expression:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub final_score{
  my @stk;
  for(@{$_[0]}) {
    if(/\d+/) {
      push @stk,$_
    } elsif($_ eq 'C') {
      pop @stk
    } elsif($_ eq 'D') {
      push @stk,$stk[-1] * 2
    } else {
      push @stk,$stk[-2] + $stk[-1]
    }
  }
  sum0 @stk
}

printf "%d\n",final_score(['5','2','C','D','+']);
printf "%d\n",final_score(['5','-2','4','C','D','9','+','+']);
printf "%d\n",final_score(['7','D','D','C','+','3']);
printf "%d\n",final_score(['-5','-10','+','D','C','+']);
printf "%d\n",final_score(['3','6','+','D','C','8','+','D',
			   '-2','C','+']);

Monday, August 18, 2025

TWC335

Challenge Link

Task1

We find the common characters between all words:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(min);
use List::MoreUtils qw(frequency);

sub common_characters{
  my @f = map{{frequency split ''}} @{$_[0]};
  map{my $l = $_;
      ($l) x min map{$_->{$l} // 0} @f
    } 'a'..'z'
}

print show common_characters(["bella","label","roller"]);
print show common_characters(["cool","lock","cook"]);
print show common_characters(["hello","world","pole"]);
print show common_characters(["abc","def","ghi"]);
print show common_characters(["aab","aac","aaa"]);

Task2

We find the winner by only keeping track of the moves, instead of simulating the whole grid:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(any);

sub find_winner{
  my ($m) = @_;
  my @cnt = (0) x 8;
  for(my $k = $#$m; $k >= 0; $k -= 2){
    my ($i,$j) = ($m->[$k][0],$m->[$k][1]);
    $cnt[$i]++;
    $cnt[$j+3]++;
    $cnt[6]++ if $i == $j;
    $cnt[7]++ if $i + $j == 2;
    if(any{$_ == 3} ($cnt[$i],$cnt[$j+3],$cnt[6],$cnt[7])) {
      return $k % 2 == 0 ? 'A' : 'B'
    }
  }
  @$m == 9 ? 'Draw' : 'Pending'
}

printf "%s\n",find_winner([[0,0],[2,0],[1,1],[2,1],[2,2]]);
printf "%s\n",find_winner([[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]);
printf "%s\n",find_winner([[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],
			   [0,1],[0,2],[2,2]]);
printf "%s\n",find_winner([[0,0],[1,1]]);
printf "%s\n",find_winner([[1,1],[0,0],[2,2],[0,1],[1,0],[0,2]]);

Monday, August 11, 2025

TWC334

Challenge Link

Task1

We find the sum of the given range:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub range_sum{
  sum0 @{$_[0]}[$_[1]..$_[2]];
}

printf "%d\n",range_sum([-2,0,3,-5,2,-1],0,2);
printf "%d\n",range_sum([1,-2,3,-4,5],1,3);
printf "%d\n",range_sum([1,0,2,-1,3],3,4);
printf "%d\n",range_sum([-5,4,-3,2,-1,0],0,3);
printf "%d\n",range_sum([-1,0,2,-3,-2,1],0,2);

Task2

We find the nearest point to the given x and y using Manhattan distance:
#!/usr/bin/env perl
use strict;
use warnings;

sub nearest_valid_point{
  my ($p,$x,$y) = @_;
  my ($res,$min) = (-1,1000);
  foreach my $i(0..$#$p) {
    my ($a,$b) = ($p->[$i][0],$p->[$i][1]);
    if($a == $x || $b == $y){
      my $d = abs($a - $x) + abs($b - $y);
      if($d < $min) {
	$min = $d;
	$res = $i
      }
    }
  }
  $res
}

printf "%d\n",nearest_valid_point([[1,2],[3,1],[2,4],[2,3]],3,4);
printf "%d\n",nearest_valid_point([[3,4],[2,3],[1,5],[2,5]],2,5);
printf "%d\n",nearest_valid_point([[2,2],[3,3],[4,4]],1,1);
printf "%d\n",nearest_valid_point([[0,1],[1,0],[0,2],[2,0]],0,0);
printf "%d\n",nearest_valid_point([[5,6],[6,5],[5,4],[4,5]],5,5);

Monday, August 4, 2025

TWC333

Challenge Link

Task1

We check if the coordinates form a straight line:
#!/usr/bin/env perl
use strict;
use warnings;

sub straight_line{
  my ($m) = @_;
  my ($x1,$y1,$x2,$y2) = ($m->[0][0],$m->[0][1],$m->[1][0],$m->[1][1]);
  for(my $i = 2; $i < @$m; ++$i) {
    my ($x,$y) = ($m->[$i][0],$m->[$i][1]);
    return 0 if(($x - $x1) * ($y2 - $y1) != ($y - $y1) * ($x2 - $x1))
  }
  1
}

printf "%d\n",straight_line([[2,1],[2,3],[2,5]]);
printf "%d\n",straight_line([[1,4],[3,4],[10,4]]);
printf "%d\n",straight_line([[0,0],[1,1],[2,3]]);
printf "%d\n",straight_line([[1,1],[1,1],[1,1]]);
printf "%d\n",straight_line([[1000000,1000000],
			     [2000000,2000000],
			     [3000000,3000000]]);

Task2

We duplicate each zero twice until we reach array size and stop:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub duplicate_zeros{
  my ($arr) = @_;
  my ($i,@stk) = (0);
  while($i < @$arr && @stk != @$arr){
    if($arr->[$i] == 0 && (@stk+1 < @$arr)) {
      push @stk,0 for 0..1;
      $i++
    } else {
      push @stk,$arr->[$i++]
    }
  }
  @stk
}

print show duplicate_zeros([1,0,2,3,0,4,5,0]);
print show duplicate_zeros([1,2,3]);
print show duplicate_zeros([1,2,3,0]);
print show duplicate_zeros([0,0,1,2]);
print show duplicate_zeros([1,2,0,3,4]);