From fbd90fc53f102bf89fb3774013094193b7c54ce2 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 20 May 2024 17:10:19 +0800 Subject: challenge 270, raku solutions --- challenge-270/feng-chang/raku/ch-1.raku | 13 +++++++++++++ challenge-270/feng-chang/raku/ch-2.raku | 31 +++++++++++++++++++++++++++++++ challenge-270/feng-chang/raku/test.raku | 23 +++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100755 challenge-270/feng-chang/raku/ch-1.raku create mode 100755 challenge-270/feng-chang/raku/ch-2.raku create mode 100755 challenge-270/feng-chang/raku/test.raku diff --git a/challenge-270/feng-chang/raku/ch-1.raku b/challenge-270/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..4c6cd2626f --- /dev/null +++ b/challenge-270/feng-chang/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s); + +use MONKEY-SEE-NO-EVAL; + +my @a = EVAL $s; +my (\rows, \cols) = +@a, +@a[0]; +put +(^rows X ^cols).grep(-> (\b,\c) { + @a[b;c] == 1 && + +@a[b].grep(0) == cols - 1 && + +@a[*;c].grep(0) == rows - 1 +}); diff --git a/challenge-270/feng-chang/raku/ch-2.raku b/challenge-270/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..943e9fb31a --- /dev/null +++ b/challenge-270/feng-chang/raku/ch-2.raku @@ -0,0 +1,31 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my \y = @ints.pop; +my \x = @ints.pop; + +my @a = @ints.sort; +my \mx = @a[*-1]; + +if 2*x ≤ y { + put @a.map(mx - *).sum * x; + exit 0; +} + +my $score = 0; +loop { + @a .= grep(* < mx).Array; + last unless @a; + + ++@a[0]; + if +@a > 1 { + ++@a[1]; + $score += y; + } else { + $score += x; + } + + @a .= sort; +} +put $score; diff --git a/challenge-270/feng-chang/raku/test.raku b/challenge-270/feng-chang/raku/test.raku new file mode 100755 index 0000000000..d4aaadf2a4 --- /dev/null +++ b/challenge-270/feng-chang/raku/test.raku @@ -0,0 +1,23 @@ +#!/bin/env raku + +# The Weekly Challenge 270 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Special Positions +pwc-test './ch-1.raku', '[1,0,0],[0,0,1],[1,0,0]', 1, 'Special Positions: [[1,0,0],[0,0,1],[1,0,0]] => 1'; +pwc-test './ch-1.raku', '[1,0,0],[0,1,0],[0,0,1]', 3, 'Special Positions: [[1,0,0],[0,1,0],[0,0,1]] => 3'; + +# Task 2, Distribute Elements +pwc-test './ch-2.raku', <4 1>, 3, 2, 9, 'Distribute Elements: @ints=(4,1), $x=3, $y=2 => 9'; +pwc-test './ch-2.raku', <2 3 3 3 5>, 2, 1, 6, 'Distribute Elements: @ints=(2,3,3,3,5), $x=2, $y=1 => 6'; +pwc-test './ch-2.raku', <1 2 1>, 1, 3, 2, 'Distribute Elements: @ints=(1,2,1), $x=1, $y=3 => 2'; -- cgit From bc1f7a803d7600e5c160ea957f12aa144b24cc1b Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Tue, 21 May 2024 19:20:24 -0500 Subject: Week 270 solutions --- challenge-270/bob-lied/README | 6 +-- challenge-270/bob-lied/perl/ch-1.pl | 95 +++++++++++++++++++++++++++++++++++++ challenge-270/bob-lied/perl/ch-2.pl | 94 ++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 challenge-270/bob-lied/perl/ch-1.pl create mode 100644 challenge-270/bob-lied/perl/ch-2.pl diff --git a/challenge-270/bob-lied/README b/challenge-270/bob-lied/README index 546c6eeb9a..3bc8c7afd0 100644 --- a/challenge-270/bob-lied/README +++ b/challenge-270/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 269 by Bob Lied +Solutions to weekly challenge 270 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-269/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-269/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-270/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-270/bob-lied diff --git a/challenge-270/bob-lied/perl/ch-1.pl b/challenge-270/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..5cbd51f959 --- /dev/null +++ b/challenge-270/bob-lied/perl/ch-1.pl @@ -0,0 +1,95 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-1.pl Perl Weekly Challenge 270 Task 1 Special Positions +#============================================================================= +# You are given a m x n binary matrix. +# Write a script to return the number of special positions in the given +# binary matrix. A position (i, j) is called special if +# $matrix[i][j] == 1 and all other elements in the row i and column j are 0. +# Example 1 Input: $matrix = [ [1, 0, 0], +# [0, 0, 1], +# [1, 0, 0], ] +# Output: 1 +# There is only one special position (1, 2) as $matrix[1][2] == 1 +# and all other elements in row 1 and column 2 are 0. +# Example 2 Input: $matrix = [ [1, 0, 0], +# [0, 1, 0], +# [0, 0, 1], ] +# Output: 3 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub columnOf($matrix, $col) +{ + map { $_->[$col] } $matrix->@*; +} + +sub specialPos($matrix) +{ + use List::Util qw/sum/; + use List::MoreUtils qw/indexes/; + + my $special = 0; + + for ( 0 .. $matrix->$#* ) + { + my @ones = indexes { $_ == 1 } $matrix->[$_]->@*; + $special++ unless ( @ones != 1 ) || (sum columnOf($matrix, $ones[0]) ) != 1; + } + + return $special; +} + +sub runTest +{ + use Test2::V0; + + my $matrix = [ [1,0,0], [0,0,1], [1,0,0] ]; + is( specialPos($matrix), 1, "Example 1"); + + $matrix = [ [1,0,0], [0,1,0], [0,0,1] ]; + is( specialPos($matrix), 3, "Example 2"); + + $matrix = [ [0,0,0,0], [0,0,0,0] ]; + is( specialPos($matrix), 0, "All zero"); + + $matrix = [ [1,1,1,1], [1,1,1,1], [1,1,1,1] ]; + is( specialPos($matrix), 0, "All one"); + + $matrix = [ [0,0], [1,0], [0,0], [1,1] ]; + is( specialPos($matrix), 0, "row > col"); + + $matrix = [ [1] ]; + is( specialPos($matrix), 1, "degenerate 1x1 with 1"); + + $matrix = [ [0] ]; + is( specialPos($matrix), 0, "degenerate 1x1 with 0"); + + $matrix = [ [0,0,0] ]; + is( specialPos($matrix), 0, "one row, 0"); + + $matrix = [ [0,1,0] ]; + is( specialPos($matrix), 1, "one row, 1"); + + $matrix = [ [1,1,0] ]; + is( specialPos($matrix), 0, "one row, 2"); + + $matrix = [ [1], [0], [0], [0] ]; + is( specialPos($matrix), 1, "one col, 1"); + + done_testing; +} diff --git a/challenge-270/bob-lied/perl/ch-2.pl b/challenge-270/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..cdc7442665 --- /dev/null +++ b/challenge-270/bob-lied/perl/ch-2.pl @@ -0,0 +1,94 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 270 Task 2 Equalize Array +#============================================================================= +# You are give an array of integers, @ints and two integers, $x and $y. +# Write a script to execute one of the two options: +# Level 1: Pick an index i of the given array and do $ints[i] += 1 +# Level 2: Pick two different indices i,j and do $ints[i] +=1 and $ints[j] += 1. +# You are allowed to perform as many levels as you want to make every elements +# in the given array equal. There is cost attach for each level, for Level 1, +# the cost is $x and $y for Level 2. +# In the end return the minimum cost to get the work done. +# +# Example 1 Input: @ints = (4, 1), $x = 3 and $y = 2 +# Output: 9 +# Level 1: i=1, so $ints[1] += 1. @ints = (4, 2) +# Level 1: i=1, so $ints[1] += 1. @ints = (4, 3) +# Level 1: i=1, so $ints[1] += 1. @ints = (4, 4) +# We performed operation Level 1 three times, +# so the total cost would be 3 x $x => 3 x 3 => 9 +# +# Example 2 Input: @ints = (2, 3, 3, 3, 5), $x = 2 and $y = 1 +# Output: 6 +# Level 2: i=0, j=1, $ints[0]++ and $ints[1]++, @ints = (3, 4, 3, 3, 5) +# Level 2: i=0, j=2, $ints[0]++ and $ints[2]++, @ints = (4, 4, 4, 3, 5) +# Level 2: i=0, j=3, $ints[0]++ and $ints[3]++, @ints = (5, 4, 4, 4, 5) +# Level 2: i=1, j=2, $ints[1]++ and $ints[2]++, @ints = (5, 5, 5, 4, 5) +# Level 1: i=3, so $ints[3] += 1 @ints = (5, 5, 5, 5, 5) +# We perforned operation Level 1 one time and Level 2 four times. +# So the total cost would be (1 x $x) + (4 x $y) => (1 x 2) + (4 x 1) => 6 +#============================================================================= + +use v5.38; + +use Getopt::Long; +my $X; my $Y; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "x:i" => \$X, "y:i" => \$Y); +exit(!runTest()) if $DoTest; + +say equalizeArray(\@ARGV, $X, $Y); + +sub equalizeArray($ints, $lvl1cost, $lvl2cost) +{ + use List::Util qw/max sum/; + + my $target = max $ints->@*; + my $addsNeeded = sum map { $target - $_ } $ints->@*; + + if ( $ints->$#* < 1 ) + { + # Nothing to add to, no moves possible + return 0; + } + elsif ( $ints->$#* == 1 ) + { + # Only Level 1 moves are possible. + return $addsNeeded * $lvl1cost; + } + + if ( $lvl2cost <= 2 * $lvl1cost ) + { + # Increment 2 at a time as long as we can + my $cost = $lvl2cost * int( $addsNeeded / 2 ); + $cost += $lvl1cost if $addsNeeded % 2; + return $cost; + } + else + { + # Cheaper just to Level 1 moves + return $addsNeeded * $lvl1cost; + } +} + +sub runTest +{ + use Test2::V0; + + is( equalizeArray([4,1], 3, 2), 9, "Example 1"); + is( equalizeArray([2,3,3,3,5], 2, 1), 6, "Example 2"); + is( equalizeArray([2,4,3,3,5], 2, 1), 4, "Example 2"); + + is( equalizeArray([5], 3, 2), 0, "No moves"); + is( equalizeArray([ ], 3, 2), 0, "Empty list"); + + is( equalizeArray([4,1,1], 3, 9), 18, "Expensive level 2"); + is( equalizeArray([4,1,1], 3, 6), 18, "lvl2 = 2 * lvl1"); + + done_testing; +} -- cgit From 4e32b6d7e4b63219b58bc5cab8a8402b33edf0ea Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 20 May 2024 15:09:14 +0200 Subject: PWC 270 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 and 2 PL/PgSQL done using PL/Perl Task 1 Python done Task 2 Python done Task 1 Java done Task 2 Java done --- challenge-270/luca-ferrari/blog-1.txt | 1 + challenge-270/luca-ferrari/blog-10.txt | 1 + challenge-270/luca-ferrari/blog-2.txt | 1 + challenge-270/luca-ferrari/blog-3.txt | 1 + challenge-270/luca-ferrari/blog-4.txt | 1 + challenge-270/luca-ferrari/blog-5.txt | 1 + challenge-270/luca-ferrari/blog-6.txt | 1 + challenge-270/luca-ferrari/blog-7.txt | 1 + challenge-270/luca-ferrari/blog-8.txt | 1 + challenge-270/luca-ferrari/blog-9.txt | 1 + challenge-270/luca-ferrari/pljava/pom.xml | 6 +- .../luca-ferrari/pljava/src/main/java/Task1.java | 99 ++++++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 92 ++++++++++++++++++++ challenge-270/luca-ferrari/plperl/ch-1.plperl | 41 +++++++++ challenge-270/luca-ferrari/plperl/ch-2.plperl | 56 ++++++++++++ challenge-270/luca-ferrari/plpgsql/ch-1.sql | 15 ++++ challenge-270/luca-ferrari/plpgsql/ch-2.sql | 15 ++++ challenge-270/luca-ferrari/python/ch-1.py | 46 ++++++++++ challenge-270/luca-ferrari/python/ch-2.py | 45 ++++++++++ challenge-270/luca-ferrari/raku/ch-1.raku | 26 ++++++ challenge-270/luca-ferrari/raku/ch-2.raku | 36 ++++++++ 21 files changed, 484 insertions(+), 3 deletions(-) create mode 100644 challenge-270/luca-ferrari/blog-1.txt create mode 100644 challenge-270/luca-ferrari/blog-10.txt create mode 100644 challenge-270/luca-ferrari/blog-2.txt create mode 100644 challenge-270/luca-ferrari/blog-3.txt create mode 100644 challenge-270/luca-ferrari/blog-4.txt create mode 100644 challenge-270/luca-ferrari/blog-5.txt create mode 100644 challenge-270/luca-ferrari/blog-6.txt create mode 100644 challenge-270/luca-ferrari/blog-7.txt create mode 100644 challenge-270/luca-ferrari/blog-8.txt create mode 100644 challenge-270/luca-ferrari/blog-9.txt create mode 100644 challenge-270/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-270/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-270/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-270/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-270/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-270/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-270/luca-ferrari/python/ch-1.py create mode 100644 challenge-270/luca-ferrari/python/ch-2.py create mode 100644 challenge-270/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-270/luca-ferrari/raku/ch-2.raku diff --git a/challenge-270/luca-ferrari/blog-1.txt b/challenge-270/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..3ff06d4523 --- /dev/null +++ b/challenge-270/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1 diff --git a/challenge-270/luca-ferrari/blog-10.txt b/challenge-270/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..a7d43eeb0f --- /dev/null +++ b/challenge-270/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge270.html#task2pljava diff --git a/challenge-270/luca-ferrari/blog-2.txt b/challenge-270/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..599b6e1514 --- /dev/null +++ b/challenge-270/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2 diff --git a/challenge-270/luca-ferrari/blog-3.txt b/challenge-270/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..ed515527ef --- /dev/null +++ b/challenge-270/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1plperl diff --git a/challenge-270/luca-ferrari/blog-4.txt b/challenge-270/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..c6e10ace9a --- /dev/null +++ b/challenge-270/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2plperl diff --git a/challenge-270/luca-ferrari/blog-5.txt b/challenge-270/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..abb7db2c9b --- /dev/null +++ b/challenge-270/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1plpgsql diff --git a/challenge-270/luca-ferrari/blog-6.txt b/challenge-270/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..d9a8d4be66 --- /dev/null +++ b/challenge-270/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2plpgsql diff --git a/challenge-270/luca-ferrari/blog-7.txt b/challenge-270/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..dbdb33d49a --- /dev/null +++ b/challenge-270/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1python diff --git a/challenge-270/luca-ferrari/blog-8.txt b/challenge-270/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..80a4ca20ad --- /dev/null +++ b/challenge-270/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task2python diff --git a/challenge-270/luca-ferrari/blog-9.txt b/challenge-270/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..5cb1b54dfa --- /dev/null +++ b/challenge-270/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/05/20/PerlWeeklyChallenge270.html#task1pljava diff --git a/challenge-270/luca-ferrari/pljava/pom.xml b/challenge-270/luca-ferrari/pljava/pom.xml index b5fe1e2ab4..4de418afd7 100644 --- a/challenge-270/luca-ferrari/pljava/pom.xml +++ b/challenge-270/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ PWC - PWC269 + PWC270 1 - Perl Weekly Challenge 269 with package PWC269 - Implementation of the tasks in PL/Java for PWC 269 + Perl Weekly Challenge 270 with package PWC270 + Implementation of the tasks in PL/Java for PWC 270 US-ASCII diff --git a/challenge-270/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-270/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..5a49f2024d --- /dev/null +++ b/challenge-270/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,99 @@ + + + +package PWC270; + +/** + * PL/Java implementation for PWC 270 + * Task 1 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC270-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', true ); + select sqlj.set_classpath( 'public', 'PWC270' ); + + select pwc270.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + + + + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc270", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String[] task1_pljava( int row_size, int[] matrix ) throws SQLException { + logger.log( Level.INFO, "Entering pwc270.task1_pljava" ); + + List rows = new LinkedList(); + List cols = new LinkedList(); + List result = new LinkedList(); + + // seek for ones + for( int row = 0; row < ( matrix.length / row_size ) ; row++ ) + for ( int col = 0; col < row_size; col++ ) + if ( matrix[ row * row_size + col ] == 1 ) { + rows.add( row ); + cols.add( col ); + } + + for ( int i = 0; i < rows.size(); i++ ) { + int current_row = rows.get( i ); + int current_col = rows.get( i ); + boolean ok = true; + + for ( int r = 0; r < rows.size(); r++ ) + if ( r == i ) + continue; + else if ( rows.get( r ) == current_row ) + ok = false; + + for ( int c = 0; c < cols.size(); c++ ) + if ( c == i ) + continue; + else if ( cols.get( c ) == current_col ) + ok = false; + + + if ( ok ) + result.add( String.format( "Row %d Col %d", current_row, current_col ) ); + } + + String[] to_return = new String[ result.size() ]; + int i = 0; + for ( String s : result ) + to_return[ i++ ] = s; + + return to_return; + + } +} diff --git a/challenge-270/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-270/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..5735002d25 --- /dev/null +++ b/challenge-270/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,92 @@ + + + +package PWC270; + +/** + * PL/Java implementation for PWC 270 + * Task 2 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC270-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', true ); + select sqlj.set_classpath( 'public', 'PWC270' ); + + select pwc270.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC270-1.jar', 'PWC270', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc270", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( int score_single, int score_double, int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering pwc270.task2_pljava" ); + + int score = 0; + List nums = Arrays.stream( numbers ).boxed().collect( Collectors.toList() ); + + // get the max + Integer max = nums.stream().max( Integer::compare ).get(); + + List needs_operation = new LinkedList(); + do { + needs_operation.clear(); + IntStream + .range( 0, nums.size() - 1 ) + .forEach( index -> { + if ( nums.get( index ) < max ) + needs_operation.add( index ); + } ); + + + if ( needs_operation.size() == 1 ) { + score += score_single; + int index = needs_operation.get( 0 ); + int value = nums.get( index ); + nums.set( index, ++value ); + } + else if ( needs_operation.size() >= 2 ) { + score += score_double; + + for ( int i = 0; i < 2; i ++ ) { + int index = needs_operation.get( i ); + int value = nums.get( index ); + nums.set( index, ++value ); + } + } + + } while ( ! needs_operation.isEmpty() ); + + return score; + + } +} diff --git a/challenge-270/luca-ferrari/plperl/ch-1.plperl b/challenge-270/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..ef65b0bdcb --- /dev/null +++ b/challenge-270/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,41 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +/** +testdb=> select distinct( r, c ) from pwc270.task1_plperl( array[ [ 1, 0, 0 ], [ 1, 0, 1 ], [0, 0 , 0 ] ] ); + row +------- + (1,2) +(1 row) + +*/ +CREATE OR REPLACE FUNCTION +pwc270.task1_plperl( int[][] ) +RETURNS TABLE( r int, c int ) +AS $CODE$ + + my ( $matrix ) = @_; + my @pnes; + + for my $row ( 0 .. $matrix->@* -1 ) { + for my $column ( 0 .. $matrix->@[ $row ]->@* -1 ) { + next if ( $matrix->@[ $row ]->@[ $column ] != 1 ); + push @ones, [ $row, $column, sprintf( '%02d-%02d', $row, $column ) ]; + } + } + + for my $position ( @ones ) { + if ( grep( { $position->[ 2 ] != $_->[ 2] && ( $position->[ 0 ] == $_->[ 0 ] || $position->[ 1 ] == $_->[ 1 ] ) } @ones ) == 0 ) { + return_next( { r => $position->@[ 0 ], c => $position->@[ 1 ] } ); + } + } + + + return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-270/luca-ferrari/plperl/ch-2.plperl b/challenge-270/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..e3579a141c --- /dev/null +++ b/challenge-270/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,56 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +CREATE OR REPLACE FUNCTION +pwc270.task2_plperl( int, int, int[] ) +RETURNS int +AS $CODE$ + + my ( $single, $double, $nums ) = @_; + my $score = 0; + + my $max = sub { + my ( $nums ) = @_; + my $value = undef; + + for ( $nums->@* ) { + $value = $_ if ( ! $value || $value < $_ ); + } + + return $value; + }; + + my $current_max = $max->( $nums ); + my @need_operation; + + + for ( 0 .. $nums->@* - 1 ) { + push @need_operation, $_ if ( $nums->@[ $_ ] < $current_max ); + } + + while ( @need_operation > 0 ) { + if ( @need_operation > 1 ) { + $nums->[ $need_operation[ 0 ] ]++; + $nums->[ $need_operation[ 1 ] ]++; + $score += $double; + } + elsif ( @need_operation == 1 ) { + $nums->[ $need_operation[ 0 ] ]++; + $score += $single; + } + + @need_operation = (); + for ( 0 .. $nums->@* - 1 ) { + push @need_operation, $_ if ( $nums->@[ $_ ] < $current_max ); + } + } + + return $score; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-270/luca-ferrari/plpgsql/ch-1.sql b/challenge-270/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..acd32b41cc --- /dev/null +++ b/challenge-270/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +CREATE OR REPLACE FUNCTION +pwc270.task1_plpgsql( matrix int[][] ) +RETURNS SETOF int[] +AS $CODE$ + SELECT pwc270.task1_plperl( matrix ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-270/luca-ferrari/plpgsql/ch-2.sql b/challenge-270/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..b2e37dae7e --- /dev/null +++ b/challenge-270/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,15 @@ +-- +-- Perl Weekly Challenge 270 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc270; + +CREATE OR REPLACE FUNCTION +pwc270.task2_plpgsql( s int, d int, nums int[] ) +RETURNS int +AS $CODE$ + SELECT pwc270.task2_plperl( s, d, nums ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-270/luca-ferrari/python/ch-1.py b/challenge-270/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..bb4786c9bb --- /dev/null +++ b/challenge-270/luca-ferrari/python/ch-1.py @@ -0,0 +1,46 @@ +#!python + +# +# Perl Weekly Challenge 270 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + row_size = int( args[ 0 ] ) + matrix = [] + + # build the matrix + current_row = 0 + matrix.append( [] ) + for x in args[ 1: ]: + + if len( matrix[ current_row ] ) >= row_size: + current_row += 1 + matrix.append( [] ) + + matrix[ current_row ].append( int( x ) ) + + + ones = [] + for r in range( 0, len( matrix ) ): + for c in range( 0, len( matrix[ r ] ) ): + if matrix[ r ][ c ] == 1: + ones.append( [ r, c ] ) + + for current in ones: + found = list( filter( lambda x: x[ 0 ] == current[ 0 ] or x[ 1 ] == current[ 1 ], ones ) ) + if len( found ) == 1: + print( current ) + + + return "" + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-270/luca-ferrari/python/ch-2.py b/challenge-270/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..1b9794bb2e --- /dev/null +++ b/challenge-270/luca-ferrari/python/ch-2.py @@ -0,0 +1,45 @@ +#!python + +# +# Perl Weekly Challenge 270 +# Task 2 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + single = int( args[ 0 ] ) + double = int( args[ 1 ] ) + nums = list( map( int, args[ 2: ] ) ) + + # compute the current max + max = None + for v in nums: + if not max or v > max: + max = v + + need_operation = list( filter( lambda x: nums[ x ] < max, range( 0, len( nums ) ) ) ) + score = 0 + while len( need_operation ) > 0: + if len( need_operation ) > 1 : + score += double + nums[ need_operation[ 0 ] ] += 1 + nums[ need_operation[ 1 ] ] += 1 + need_operation = need_operation[ 2: ] + elif len( need_operation ) == 1: + score += single + nums[ need_operation[ 0 ] ] += 1 + need_operation = need_operation[ 2: ] + + + return score + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-270/luca-ferrari/raku/ch-1.raku b/challenge-270/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..490b3b4679 --- /dev/null +++ b/challenge-270/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!raku + +# +# Perl Weekly Challenge 270 +# Task 1 +# +# See +# + +sub MAIN() { + my @matrix = [1, 0, 0], + [0, 0, 1], + [1, 0, 0], + ; + + my @ones; + for 0 ..^ @matrix.elems -> $row { + @ones.push: @matrix[ $row ].grep( { $_ == 1 }, :k ).map( { $row, $_, '%02d-%02d'.sprintf( $row, $_ ) } ).flat; + } + + + for @ones -> $current_one { + $current_one[ 0, 1 ].join( ',' ).say if @ones.grep( { $current_one[ 2 ] ne $_[ 2 ] && ( $_[ 1 ] == $current_one[ 1 ] || $_[ 0 ] == $current_one[ 0 ] ) } ).elems == 0 + } + +} diff --git a/challenge-270/luca-ferrari/raku/ch-2.raku b/challenge-270/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..b5ca114def --- /dev/null +++ b/challenge-270/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!raku + +# +# Perl Weekly Challenge 270 +# Task 2 +# +# See +# + +sub MAIN( Int $single, + Int $double, + *@nums is copy where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + + my $current_max = @nums.max; + my @need_operation = @nums.grep( * < $current_max, :k ); + my $score = 0; + + while ( @need_operation ) { + + if @need_operation.elems == 1 { + # single operation + @nums[ @need_operation[ 0 ] ] += 1; + $score += $single; + } + elsif @need_operation.elems > 1 { + @nums[ @need_operation[ 0 ] ] += 1; + @nums[ @need_operation[ 1 ] ] += 1; + $score += $double; + } + + @need_operation = @nums.grep( * < $current_max, :k ); + } + + $score.say; + +} -- cgit From 8bdae88ceb5af244cc67681affca82886d7325a5 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 22 May 2024 16:31:48 -0400 Subject: DAJ 270 --- challenge-270/dave-jacoby/perl/ch-1.pl | 47 ++++++++++++++++++++++++++++++++ challenge-270/dave-jacoby/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 challenge-270/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-270/dave-jacoby/perl/ch-2.pl diff --git a/challenge-270/dave-jacoby/perl/ch-1.pl b/challenge-270/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..6e771215b4 --- /dev/null +++ b/challenge-270/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +my @examples = ( + + [ [ 1, 0, 0 ], [ 0, 0, 1 ], [ 1, 0, 0 ], ], + [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ], + ], + +); +for my $example (@examples) { + my $output = special_positions($example); + my $input = display_matrix( $example); +say <<"END"; + Input: \$matrix = + [ $input ] + Output: $output +END +} + +sub special_positions ($matrix) { + my $output = 0; +OUTER: for my $i ( 0 .. -1 + scalar keys $matrix->@* ) { + for my $j ( 0 .. -1 + scalar keys $matrix->[$i]->@* ) { + for my $x ( 0 .. -1 + scalar keys $matrix->[$i]->@* ) { + my $v = $matrix->[$i][$x]; + next OUTER if $v == 0 && $x == $j; + next OUTER if $v != 0 && $x != $j; + } + for my $y ( 0 .. -1 + scalar keys $matrix->@* ) { + my $v = $matrix->[$y][$j]; + next OUTER if $v == 0 && $y == $i; + next OUTER if $v != 0 && $y != $i; + } + $output++; + } + } + return $output; +} + +sub display_matrix ($matrix) { + return join ",\n ", + map { join ' ', '[', ( join ', ', $_->@* ), ']' } $matrix->@*; +} diff --git a/challenge-270/dave-jacoby/perl/ch-2.pl b/challenge-270/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..c3c1c36776 --- /dev/null +++ b/challenge-270/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use List::Util qw{ first max }; + +my @examples = ( + + [ 3, 2, 4, 1 ], + [ 2, 1, 2, 3, 3, 3, 5 ], + [ 1, 3, 2, 3, 3, 3, 5 ], +); + +for my $example (@examples) { + my $output = distribute_elements( $example->@* ); + my ( $x, $y, @ints ) = $example->@*; + my $ints = join ', ', @ints; + + say <<"END"; + Input: \@ints = ($ints), \$x = $x, \$y = $y + Output: $output +END +} + +sub distribute_elements (@input) { + my ( $x, $y, @ints ) = @input; + my $max = max @ints; + my $count = scalar grep { $_ < $max } @ints; + my $cost = 0; + my $check = $x * 2 >= $y ? 1 : 0; + while ($count) { + if ( $check && $count > 1 ) { + my $f = first { $ints[$_] < $max } 0 .. -1 + scalar @ints; + my $s = + first { $ints[$_] < $max && $_ != $f } 0 .. -1 + scalar @ints; + $ints[$f]++; + $ints[$s]++; + $cost += $y; + } + else { + my $f = ( first { $ints[$_] < $max } 0 .. -1 + scalar @ints ); + $ints[$f]++; + $cost += $x; + } + $count = scalar grep { $_ < $max } @ints; + } + return $cost; +} -- cgit From 51d9a3008486a34868dffc593a4ae41d947954b6 Mon Sep 17 00:00:00 2001 From: lancew Date: Wed, 22 May 2024 21:38:55 +0100 Subject: Perl by Lance --- challenge-270/lance-wicks/perl/lib/Special.pm | 58 +++++++++++++++++++++ challenge-270/lance-wicks/perl/t/ch-1.t | 73 +++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 challenge-270/lance-wicks/perl/lib/Special.pm create mode 100644 challenge-270/lance-wicks/perl/t/ch-1.t diff --git a/challenge-270/lance-wicks/perl/lib/Special.pm b/challenge-270/lance-wicks/perl/lib/Special.pm new file mode 100644 index 0000000000..9b05c0c0c2 --- /dev/null +++ b/challenge-270/lance-wicks/perl/lib/Special.pm @@ -0,0 +1,58 @@ +package Special; + +use Data::Dumper; + +# A position (i, j) is called special if +# $matrix[i][j] == 1 +# and all other elements in the row i +# and column j are 0. + +sub count_positions { + my ( $self, $matrix ) = @_; + + my $positions = 0; + for ( @{ $self->coords_list } ) { + + $positions++ if $self->is_special( $matrix, $_->[0], $_->[1], ); + } + + return $positions; +} + +sub coords_list { + return [ + [ 0, 0 ], [ 0, 1 ], [ 0, 2 ], # + [ 1, 0 ], [ 1, 1 ], [ 1, 2 ], # + [ 2, 0 ], [ 2, 1 ], [ 2, 2 ], # + ]; +} + +sub is_special { + my ( $self, $matrix_orig, $i, $j ) = @_; + + my $matrix; + for ( 0, 1, 2 ) { + push @$matrix, [ @{ $matrix_orig->[$_] } ]; + } + + return 0 if $matrix->[$i][$j] == 0; + + my $row = $matrix->[$i]; + delete $row->[$j]; + + my $result = grep { $_ != 0 } @$row; + return 0 if $result; + + my @column; + for ( 0, 1, 2 ) { + push @column, $matrix->[$_][$j]; + } + delete $column[$i]; + + $result = grep { $_ != 0 } @column; + return 0 if $result; + + return 1; +} + +1; diff --git a/challenge-270/lance-wicks/perl/t/ch-1.t b/challenge-270/lance-wicks/perl/t/ch-1.t new file mode 100644 index 0000000000..5d8595ef2d --- /dev/null +++ b/challenge-270/lance-wicks/perl/t/ch-1.t @@ -0,0 +1,73 @@ +use Test2::V0 -target => 'Special'; + +subtest 'Example 1' => sub { + my $matrix = [ + [ 1, 0, 0 ], # + [ 0, 0, 1 ], # + [ 1, 0, 0 ], # + ]; + + my $got = $CLASS->count_positions($matrix); + is $got, 1; +}; + +subtest 'Example 2' => sub { + my $matrix = [ + [ 1, 0, 0 ], # + [ 0, 1, 0 ], # + [ 0, 0, 1 ], # + ]; + my $got = $CLASS->count_positions($matrix); + is $got, 3; +}; + +subtest 'Extra test 1' => sub { + my $matrix = [ + [ 1, 0, 0 ], # + [ 1, 0, 0 ], # + [ 1, 0, 0 ], # + ]; + my $got = $CLASS->count_positions($matrix); + is $got, 0; +}; + +subtest 'Extra test 2' => sub { + my $matrix = [ + [ 1, 1, 1 ], # + [ 0, 0, 0 ], # + [ 0, 0, 0 ], # + ]; + my $got = $CLASS->count_positions($matrix); + is $got, 0; +}; + +subtest "Positions generator" => sub { + is $CLASS->coords_list, [ + [ 0, 0 ], [ 0, 1 ], [ 0, 2 ], # + [ 1, 0 ], [ 1, 1 ], [ 1, 2 ], # + [ 2, 0 ], [ 2, 1 ], [ 2, 2 ], # + ], + 'Should return the correct set of coords'; +}; + +subtest "Position test" => sub { + my $matrix = [ + [ 1, 0, 0 ], # + [ 0, 0, 1 ], # + [ 1, 0, 0 ], # + ]; + + is $CLASS->is_special( $matrix, 0, 0 ), 0; + is $CLASS->is_special( $matrix, 0, 1 ), 0; + is $CLASS->is_special( $matrix, 0, 2 ), 0; + + is $CLASS->is_special( $matrix, 1, 0 ), 0; + is $CLASS->is_special( $matrix, 1, 1 ), 0; + is $CLASS->is_special( $matrix, 1, 2 ), 1; + + is $CLASS->is_special( $matrix, 2, 0 ), 0; + is $CLASS->is_special( $matrix, 2, 1 ), 0; + is $CLASS->is_special( $matrix, 2, 2 ), 0; +}; + +done_testing; -- cgit From 61fb69f0898c6c0541221e364e0874a66bf9e7cf Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Thu, 23 May 2024 01:04:31 +0000 Subject: ch-1.raku yet again --- challenge-270/mark-anderson/raku/ch-1.raku | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/challenge-270/mark-anderson/raku/ch-1.raku b/challenge-270/mark-anderson/raku/ch-1.raku index 99db6da982..e4f09baed1 100644 --- a/challenge-270/mark-anderson/raku/ch-1.raku +++ b/challenge-270/mark-anderson/raku/ch-1.raku @@ -15,8 +15,9 @@ is special-positions([ sub special-positions(@m) { - my @rows = @m .map({ ($++, .grep(1, :k)).join("|") }); - my @cols = ([Z] @m).map({ (.grep(1, :k), $++ ).join("|") }); - - + (@rows (&) @cols) + my @indices = @m.map({ $++, .grep(1, :k).List }); + my @cols = flat @indices>>.[1]; + @cols = (@cols (-) @cols.repeated).keys; + @indices .= grep(*.[1] == 1); + (@indices>>.[1] (&) @cols).elems } -- cgit From 9d0c5ca88a0b3a711174e4a6ac9037364d08d882 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Thu, 23 May 2024 02:58:21 +0000 Subject: ch-1.raku yet again --- challenge-270/mark-anderson/raku/ch-1.raku | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/challenge-270/mark-anderson/raku/ch-1.raku b/challenge-270/mark-anderson/raku/ch-1.raku index e4f09baed1..6babc2f8e3 100644 --- a/challenge-270/mark-anderson/raku/ch-1.raku +++ b/challenge-270/mark-anderson/raku/ch-1.raku @@ -15,9 +15,8 @@ is special-positions([ sub special-positions(@m) { - my @indices = @m.map({ $++, .grep(1, :k).List }); - my @cols = flat @indices>>.[1]; - @cols = (@cols (-) @cols.repeated).keys; - @indices .= grep(*.[1] == 1); - (@indices>>.[1] (&) @cols).elems + my $cols = @m.map({ .grep(1, :k).List }).List; + my $uniq-cols = ($cols.flat (-) $cols.flat.repeated).keys.List; + my $rows = $cols.grep(*.elems == 1); + ($rows (&) $uniq-cols).elems } -- cgit From 17827394e9b1931098ee64ce16d09f16d948472d Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Thu, 23 May 2024 03:32:43 +0000 Subject: ch-1.raku yet again --- challenge-270/mark-anderson/raku/ch-1.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-270/mark-anderson/raku/ch-1.raku b/challenge-270/mark-anderson/raku/ch-1.raku index 6babc2f8e3..8c3e8a8152 100644 --- a/challenge-270/mark-anderson/raku/ch-1.raku +++ b/challenge-270/mark-anderson/raku/ch-1.raku @@ -17,6 +17,6 @@ sub special-positions(@m) { my $cols = @m.map({ .grep(1, :k).List }).List; my $uniq-cols = ($cols.flat (-) $cols.flat.repeated).keys.List; - my $rows = $cols.grep(*.elems == 1); - ($rows (&) $uniq-cols).elems + my $rows = $cols.grep(1); + + ($rows (&) $uniq-cols) } -- cgit From e3748d9f0832a47e066fae13df2d8ce689029135 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Thu, 23 May 2024 03:37:33 +0000 Subject: ch-1.raku yet again --- challenge-270/mark-anderson/raku/ch-1.raku | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-270/mark-anderson/raku/ch-1.raku b/challenge-270/mark-anderson/raku/ch-1.raku index 8c3e8a8152..e1972ea18c 100644 --- a/challenge-270/mark-anderson/raku/ch-1.raku +++ b/challenge-270/mark-anderson/raku/ch-1.raku @@ -17,6 +17,5 @@ sub special-positions(@m) { my $cols = @m.map({ .grep(1, :k).List }).List; my $uniq-cols = ($cols.flat (-) $cols.flat.repeated).keys.List; - my $rows = $cols.grep(1); - + ($rows (&) $uniq-cols) + + ($cols.grep(1) (&) $uniq-cols) } -- cgit From 7876026111315164db8a23fb26af80801b894aa5 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 23 May 2024 02:26:14 -0400 Subject: Challenge 270 solutions by Packy Anderson * Raku that maybe looks like Raku * Perl * Python that definitely looks like Perl * Elixir that looks like a hybrid between Perl and Elixir 1 Blog post --- challenge-270/packy-anderson/README.md | 4 +- challenge-270/packy-anderson/blog.txt | 1 + challenge-270/packy-anderson/elixir/ch-1.exs | 71 +++++++++++ challenge-270/packy-anderson/elixir/ch-2.exs | 180 +++++++++++++++++++++++++++ challenge-270/packy-anderson/perl/ch-1.pl | 69 ++++++++++ challenge-270/packy-anderson/perl/ch-2.pl | 91 ++++++++++++++ challenge-270/packy-anderson/python/ch-1.py | 56 +++++++++ challenge-270/packy-anderson/python/ch-2.py | 109 ++++++++++++++++ challenge-270/packy-anderson/raku/ch-1.raku | 64 ++++++++++ challenge-270/packy-anderson/raku/ch-2.raku | 94 ++++++++++++++ 10 files changed, 738 insertions(+), 1 deletion(-) create mode 100644 challenge-270/packy-anderson/blog.txt create mode 100755 challenge-270/packy-anderson/elixir/ch-1.exs create mode 100755 challenge-270/packy-anderson/elixir/ch-2.exs create mode 100755 challenge-270/packy-anderson/perl/ch-1.pl create mode 100755 challenge-270/packy-anderson/perl/ch-2.pl create mode 100755 challenge-270/packy-anderson/python/ch-1.py create mode 100755 challenge-270/packy-anderson/python/ch-2.py create mode 100755 challenge-270/packy-anderson/raku/ch-1.raku create mode 100755 challenge-270/packy-anderson/raku/ch-2.raku diff --git a/challenge-270/packy-anderson/README.md b/challenge-270/packy-anderson/README.md index 3f65972d0f..bf39aceac0 100644 --- a/challenge-270/packy-anderson/README.md +++ b/challenge-270/packy-anderson/README.md @@ -11,13 +11,15 @@ * [Task 2](perl/ch-2.pl) ## Guest Language: Python + * [Task 1](python/ch-1.py) * [Task 2](python/ch-2.py) ## Guest Language: Elixir + * [Task 1](elixir/ch-1.exs) * [Task 2](elixir/ch-2.exs) ## Blog Post -[Bitwise Distribution](https://packy.dardan.com/b/LS) +[Elements are Special](https://packy.dardan.com/b/LX) diff --git a/challenge-270/packy-anderson/blog.txt b/challenge-270/packy-anderson/blog.txt new file mode 100644 index 0000000000..1fd039ca7b --- /dev/null +++ b/challenge-270/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/LX \ No newline at end of file diff --git a/challenge-270/packy-anderson/elixir/ch-1.exs b/challenge-270/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..f1e5d6c534 --- /dev/null +++ b/challenge-270/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,71 @@ +#!/usr/bin/env elixir + +defmodule PWC do + + def sumRow(matrix, i) do + Enum.sum(Enum.at(matrix, i)) + end + + def sumCol(matrix, j) do + Enum.sum(Enum.map(matrix, fn x -> Enum.at(x, j) end)) + end + + def specialPositions(matrix) do + special = [] + special = for i <- 0..length(matrix)-1 do + row = Enum.at(matrix, i) + for j <- 0..length(row)-1 do + value = Enum.at(row, j) + if value == 1 and + sumRow(matrix, i) == 1 and + sumCol(matrix, j) == 1 do + # it's special + special ++ [ "(#{i}, #{j})" ] + else + special + end + end + end + special = List.flatten(special) + { + length(special), + case length(special) do + 1 -> "Special position is " + _ -> "Special positions are " + end <> Enum.join(special, ", ") + } + end + + # default value for indent + def formatMatrix(matrix), do: formatMatrix(matrix, 17) + + def formatMatrix(matrix, indent) do + output = for row <- matrix do + String.duplicate(" ", indent) <> " [" <> + Enum.join(row, ", ") <> "]" + end + "[\n" <> Enum.join(output, ",\n") <> "\n" <> + String.duplicate(" ", indent) <> "]" + end + + def solution(matrix) do + IO.puts("Input: $matrix = " <> formatMatrix(matrix)) + {count, explain} = PWC.specialPositions(matrix) + IO.puts("Output: " <> to_string(count) ) + IO.puts("\n" <> explain) + end +end + +IO.puts("Example 1:") +PWC.solution([ + [1,0,0], + [0,0,1], + [1,0,0] +]) + +IO.puts("\nExample 2:") +PWC.solution([ + [1,0,0], + [0,1,0], + [0,0,1] +]) diff --git a/challenge-270/packy-anderson/elixir/ch-2.exs b/challenge-270/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..2904331718 --- /dev/null +++ b/challenge-270/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,180 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # get just the indices stored in the map of maps + defp indicesInMap(mapping) do + List.flatten( for i <- Map.values(mapping), do: Map.keys(i) ) + end + + def fmtInts(ints) do + "@ints = (" <> Enum.join(ints, ", ") <> ")" + end + + def addToMap(mapping, intVal, i) do + submap = Map.put(Map.get(mapping, intVal, %{}), i, 1) + Map.put(mapping, intVal, submap) + end + + def removeFromMap(mapping, intVal, i) do + submap = Map.delete(Map.get(mapping, intVal, %{}), i) + if submap == %{} do + Map.delete(mapping, intVal) + else + Map.put(mapping, intVal, submap) + end + end + + # Increments a value in a list at a given index + def incrementAt(ints, index) do + ints + |> Enum.with_index() # produces tuples of {value, index} + |> Enum.map(fn + {val, ^index} -> val + 1 # ^ matches on index + {val, _} -> val + end) + end + + # just remove the old value from the map + # if the new value is maxVal + def reMap(mapping, value, maxVal, index) when value == maxVal do + mapping |> removeFromMap(value - 1, index) + end + + # if the number we incremented is less than the max, + # put it back in the mapping + def reMap(mapping, value, _, index) do + mapping + |> removeFromMap(value - 1, index) + |> addToMap(value, index) + end + + # stopping case + defp equalize(params = %{indices: indices}) + when length(indices) == 0, do: params + + # level 2 case + defp equalize(params = %{indices: indices, x: x, y: y}) + when length(indices) > 1 and x * 2 >= y do + {i, indices} = List.pop_at(indices, 0) + {j, _} = List.pop_at(indices, 0) + mapping = params[:mapping] + steps = params[:steps] + ints = params[:ints] + + # increment the values + ints = incrementAt(ints, i) + ints = incrementAt(ints, j) + steps = steps ++ [ + "Level 2: i=#{i}, j=#{j}, so $ints[#{i}] += 1 and " <> + "$ints[#{j}] += 1\n" <> fmtInts(ints) + ] + params = %{params | L2: params[:L2] + 1} + + mapping = reMap(mapping, Enum.at(ints, i), params[:maxVal], i) + mapping = reMap(mapping, Enum.at(ints, j), params[:maxVal], j) + params = %{params | + indices: indicesInMap(mapping), + mapping: mapping, + steps: steps, + ints: ints + } + equalize(params) + end + + # level 1 case + defp equalize(params = %{indices: indices}) do + {i, _} = List.pop_at(indices, 0) + mapping = params[:mapping] + steps = params[:steps] + ints = params[:ints] + + # increment the values + ints = incrementAt(ints, i) + steps = steps ++ [ + "Level 1: i=#{i}, so $ints[#{i}] += 1\n" <> + fmtInts(ints) + ] + params = %{params | L1: params[:L1] + 1} + + mapping = reMap(mapping, Enum.at(ints, i), params[:maxVal], i) + params = %{params | + indices: indicesInMap(mapping), + mapping: mapping, + steps: steps, + ints: ints + } + equalize(params) + end + + # once the list of ints is empty, return the results + defp makeMapping([], maxVal, mapping, _), do: {maxVal, mapping} + + # process the first int off the list and recurse to + # process the rest + defp makeMapping([intVal | ints], maxVal, mapping, index) do + maxVal = Enum.max([intVal, maxVal]) + mapping = addToMap(mapping, intVal, index) + makeMapping(ints, maxVal, mapping, index + 1) + end + + defp inflectTimes(n) do + case n do + 1 -> "time" + _ -> "times" + end + end + + defp equalizeArray(ints, x, y) do + {maxVal, mapping} = makeMapping(ints, -1, %{}, 0) + # we don't need to operate on values already at the max + mapping = Map.delete(mapping, maxVal) + + params = equalize(%{ + indices: indicesInMap(mapping), + mapping: mapping, + maxVal: maxVal, + steps: [], + ints: ints, + L1: 0, + L2: 0, + x: x, + y: y, + }) + steps = params[:steps] + l1 = params[:L1] + l2 = params[:L2] + + cost = (l1 * x) + (l2 * y) + operations = if l1 > 0 do + [ "Level 1, #{l1} " <> inflectTimes(l1) ] + else + [] + end + operations = if l2 > 0 do + operations ++ [ "Level 2, #{l2} " <> inflectTimes(l2) ] + else + operations + end + steps = steps ++ [ + "We performed operation " <> + Enum.join(operations, " and ") <> + "\nSo the total cost would be " <> + "(#{l1} × $x) + (#{l2} × $y) => " <> + "(#{l1} × #{x}) + (#{l2} × #{y}) => #{cost}" + ] + { cost, Enum.join(steps, "\n\n") } + end + + def solution(ints, x, y) do + IO.puts("Input: " <> fmtInts(ints) <> ", $x = #{x}, $y = #{y}") + {cost, steps} = equalizeArray(ints, x, y) + IO.puts("Output: " <> to_string(cost) ) + IO.puts("\n" <> steps) + end +end + +IO.puts("Example 1:") +PWC.solution([4,1], 3, 2) + +IO.puts("\nExample 2:") +PWC.solution([2, 3, 3, 3, 5], 2, 1) diff --git a/challenge-270/packy-anderson/perl/ch-1.pl b/challenge-270/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..a819a416eb --- /dev/null +++ b/challenge-270/packy-anderson/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( sum ); +use Lingua::EN::Inflexion qw( wordlist ); + +sub sumRow($matrix, $i) { + return sum @{ $matrix->[$i] }; +} + +sub sumCol($matrix, $j) { + return sum map { $_->[$j] } @$matrix; +} + +sub specialPositions(@matrix) { + my @special; + foreach my $i ( 0 .. $#matrix ) { + my @row = @{$matrix[$i]}; + foreach my $j ( 0 .. $#row ) { + my $value = $row[$j]; + # not special unless = 1 + next unless $value == 1; + # not special unless only 1 in row + next unless sumRow(\@matrix, $i) == 1; + # not special unless only 1 in column + next unless sumCol(\@matrix, $j) == 1; + # it's special! + push @special, "($i, $j)"; + } + } + my $list = wordlist(@special, { sep => ',' }); + my $explain = 'Special position'; + $explain .= (@special == 1) ? ' is ' : 's are '; + $explain .= $list; + return scalar(@special), $explain; +} + +sub formatMatrix($matrix, $indent=17) { + my @output; + foreach my $row (@$matrix) { + my $output_row = q{ } x $indent . " ["; + $output_row .= join(', ', map { sprintf "%1d", $_ } @$row) . ']'; + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} + +sub solution($matrix) { + say 'Input: $matrix = ' . formatMatrix($matrix); + my ($count, $explain) = specialPositions(@$matrix); + say "Output: $count\n\n$explain"; +} + +say "Example 1:"; +solution([ + [1,0,0], + [0,0,1], + [1,0,0] +]); + +say "\nExample 2:"; +solution([ + [1,0,0], + [0,1,0], + [0,0,1] +]); diff --git a/challenge-270/packy-anderson/perl/ch-2.pl b/challenge-270/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..80a8887bbe --- /dev/null +++ b/challenge-270/packy-anderson/perl/ch-2.pl @@ -0,0 +1,91 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( min ); + +sub fmtInts(@ints) { + return '@ints = (' . join(', ', @ints) . ')'; +} + +sub getNextIndex($mapping) { + my $min = min(keys %$mapping); # note the current min val + my $i = min(keys %{$mapping->{$min}}); # get the smallest index + delete $mapping->{$min}{$i}; # remove index from map + unless (values %{$mapping->{$min}}) { # no more of this value + delete $mapping->{$min}; # remove value from map + } + return $i; # return the index +} + +sub equalizeArray($ints, $x, $y) { + # track how many of each op, and terminal value + my $max = -Inf; + my %mapping; + my @steps; + my ($L1, $L2) = (0, 0); + # loop over the array to determine max value + # and where the lower numbers are + foreach my $i ( 0 .. $#{$ints} ) { + my $int = $ints->[$i]; + $max = $int if ($int > $max); + $mapping{$int}{$i} = 1; + } + # we don't need to operate on values already at the max + delete $mapping{$max}; + + while (keys %mapping) { + my $elems = scalar(map { keys %$_ } values %mapping); + if ($elems > 1 && $x * 2 >= $y) { + # get the two indices + my $i = getNextIndex(\%mapping); + my $j = getNextIndex(\%mapping); + + # increment the values + $ints->[$i]++; + $ints->[$j]++; + push @steps, + "Level 2: i=$i, j=$j, so \$ints[$i] += 1 and " . + "\$ints[$j] += 1\n" . fmtInts(@$ints); + $L2++; + # if the numbers we incremented are less than the max, + # put them back in the mapping + $mapping{$ints->[$i]}{$i} = 1 if ($ints->[$i] < $max); + $mapping{$ints->[$j]}{$j} = 1 if ($ints->[$j] < $max); + } + else { + # get the index + my $i = getNextIndex(\%mapping); + # increment the value + $ints->[$i]++; + push @steps, "Level 1: i=$i, so \$ints[$i] += 1\n" . + fmtInts(@$ints); + $L1++; + # if the number we incremented is less than the max, + # put it back in the mapping + $mapping{$ints->[$i]}{$i} = 1 if ($ints->[$i] < $max); + } + } + my $cost = ($L1 * $x) + ($L2 * $y); + my @operations; + push @operations, "Level 1, $L1 " . ($L1 == 1 ? 'time' : 'times') + if $L1; + push @operations, "Level 2, $L2 " . ($L2 == 1 ? 'time' : 'times') + if $L2; + push @steps, + 'We performed operation ' . join(" and ", @operations) . + "\nSo the total cost would be ($L1 × \$x) + ($L2 × \$y) => " . + "($L1 × $x) + ($L2 × $y) => $cost"; + return $cost, join("\n\n", @steps); +} + +sub solution($ints, $x, $y) { + say 'Input: ' . fmtInts(@$ints) . ", \$x = $x and \$y = $y"; + my ($cost, $steps) = equalizeArray($ints, $x, $y); + say "Output: $cost\n\n$steps"; +} + +say "Example 1:"; +solution([4,1], 3, 2); + +say "\nExample 2:"; +solution([2, 3, 3, 3, 5], 2, 1); diff --git a/challenge-270/packy-anderson/python/ch-1.py b/challenge-270/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..1aed4639f5 --- /dev/null +++ b/challenge-270/packy-anderson/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +def sumRow(matrix, i): + return sum(matrix[i]) + +def sumCol(matrix, j): + return sum([ i[j] for i in matrix ]) + +def specialPositions(matrix): + special = [] + for i, row in enumerate(matrix): + for j, value in enumerate(row): + # not special unless = 1 + if (value == 1 and + # not special unless only 1 in row + sumRow(matrix, i) == 1 and + # not special unless only 1 in column + sumCol(matrix, j) == 1): + # it's special! + special.append( f"({i}, {j})" ) + explain = 'Special position' + explain += ' is ' if len(special) == 1 else 's are ' + explain += ', '.join(special) + return len(special), explain + +def formatMatrix(matrix, indent=17): + output = [] + for row in matrix: + output_row = ' ' * indent + ' [' + output_row += ', '.join(map(lambda i: str(i), row)) + output_row += ']' + output.append(output_row) + + return( + "[\n" + ",\n".join(output) + "\n" + + ' ' * indent + ']' + ) + +def solution(matrix): + print(f'Input: $matrix = {formatMatrix(matrix)}') + count, explain = specialPositions(matrix) + print(f'Output: {count}\n\n{explain}') + +print('Example 1:') +solution([ + [1,0,0], + [0,0,1], + [1,0,0] +]) + +print('\nExample 2:') +solution([ + [1,0,0], + [0,1,0], + [0,0,1] +]) \ No newline at end of file diff --git a/challenge-270/packy-anderson/python/ch-2.py b/challenge-270/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..94a874ac2c --- /dev/null +++ b/challenge-270/packy-anderson/python/ch-2.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python + +import sys + +def items_in_dict(d): + items = 0 + for v, d2 in d.items(): + for i in d2.keys(): + items += 1 + return items + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def fmtInts(ints): + return f'@ints = ({comma_join(ints)})' + +def setMap(mapping, intVal, i): + if mapping.get(intVal, None) is None: + mapping[intVal] = {} + mapping[intVal][i] = 1 + +def getNextIndex(mapping): + minVal = min(mapping.keys()) # note the current min val + i = min(mapping[minVal].keys()) # minimum index for that val + del mapping[minVal][i] # remove index from map + if not mapping[minVal].values(): # no more of this value + del mapping[minVal] # remove value from map + return i # return the index + +def equalizeArray(ints, x, y): + # track how many of each op, and terminal value + maxVal = -1 * sys.maxsize + mapping = {} + steps = [] + L1 = 0 + L2 = 0 + # loop over the array to determine max value + # and where the lower numbers are + for i, intVal in enumerate(ints): + if intVal > maxVal: + maxVal = intVal + setMap(mapping, intVal, i) + + # we don't need to operate on values already at the max + del mapping[maxVal] + + while mapping.keys(): + if items_in_dict(mapping) > 1 and x * 2 >= y: + # get the two indices + i = getNextIndex(mapping) + j = getNextIndex(mapping) + + # increment the values + ints[i] += 1 + ints[j] += 1 + steps.append( + f"Level 2: i={i}, j={j}, so $ints[{i}] += 1 and " + + f"$ints[{j}] += 1\n" + fmtInts(ints) + ) + L2 += 1 + # if the numbers we incremented are less than the max, + # put them back in the mapping + if ints[i] < maxVal: + setMap(mapping, ints[i], i) + if ints[j] < maxVal: + setMap(mapping, ints[j], j) + else: + # get the index + i = getNextIndex(mapping) + # increment the value + ints[i] += 1 + steps.append( + f"Level 1: i={i}, so $ints[{i}] += 1\n" + + fmtInts(ints) + ) + L1 += 1 + # if the number we incremented is less than the max, + # put it back in the mapping + if ints[i] < maxVal: + setMap(mapping, ints[i], i) + + cost = (L1 * x) + (L2 * y) + operations = [] + if L1: + operations.append( + f"Level 1, {L1} " + ('time' if L1 == 1 else 'times') + ) + if L2: + operations.append( + f"Level 2, {L2} " + ('time' if L2 == 1 else 'times') + ) + steps.append( + 'We performed operation ' + ' and '.join(operations) + + f"\nSo the total cost would be ({L1} × $x) + ({L2} × $y) => " + + f"({L1} × {x}) + ({L2} × {y}) => {cost}" + ) + return cost, "\n\n".join(steps) + +def solution(ints, x, y): + print(f'Input: {fmtInts(ints)}, $x = {x}, $y = {y}') + cost, steps = equalizeArray(ints, x, y) + print(f'Output: {cost}\n\n{steps}') + +print('Example 1:') +solution([4,1], 3, 2) + +print('\nExample 2:') +solution([2, 3, 3, 3, 5], 2, 1) diff --git a/challenge-270/packy-anderson/raku/ch-1.raku b/challenge-270/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..39c335e748 --- /dev/null +++ b/challenge-270/packy-anderson/raku/ch-1.raku @@ -0,0 +1,64 @@ +#!/usr/bin/env raku +use v6; + +use Lingua::Conjunction; + +sub sumRow(@matrix, $i) { + return [+] @matrix[$i].values; +} + +sub sumCol(@matrix, $j) { + return [+] ( @matrix.map: { .[$j] } ); +} + +sub specialPositions(@matrix) { + my @special; + for @matrix.kv -> $i, @row { + for @row.kv -> $j, $value { + # not special unless = 1 + next unless $value == 1; + # not special unless only 1 in row + next unless sumRow(@matrix, $i) == 1; + # not special unless only 1 in column + next unless sumCol(@matrix, $j) == 1; + # it's special! + @special.push( "($i, $j)" ); + } + } + my $str = 'Special position[|s] [is|are] |list|'; + return @special.elems, conjunction(@special, :$str, :alt<,>); +} + +sub formatMatrix(@matrix, $indent=17) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " ["; + $output_row ~= @row.map({ sprintf "%1d", $_ }) + .join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} + +sub solution(@matrix) { + say 'Input: $matrix = ' ~ formatMatrix(@matrix); + my ($count, $explain) = specialPositions(@matrix); + say "Output: $count\n\n$explain"; +} + +say "Example 1:"; +solution([ + [1,0,0], + [0,0,1], + [1,0,0] +]); + +say "\nExample 2:"; +solution([ + [1,0,0], + [0,1,0], + [0,0,1] +]); diff --git a/challenge-270/packy-anderson/raku/ch-2.raku b/challenge-270/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..897ddc89a2 --- /dev/null +++ b/challenge-270/packy-anderson/raku/ch-2.raku @@ -0,0 +1,94 @@ +#!/usr/bin/env raku +use v6; + +sub fmtInts(@ints) { + return '@ints = (' ~ @ints.join(', ') ~ ')'; +} + +sub getNextIndex(%mapping) { + my $min = min(%mapping.keys); # note the current min val + my $i = min(%mapping{$min}.keys); # get the smallest index + %mapping{$min}{$i}:delete; # remove index from map + unless (%mapping{$min}.values) { # no more of this value + %mapping{$min}:delete; # remove value from map + } + return $i; # return the index +} + +sub equalizeArray(@ints, $x, $y) { + # track how many of each op, and terminal value + my $max = -Inf; + my %mapping; + my @steps; + my ($L1, $L2) = (0, 0); + # loop over the array to determine max value + # and where the lower numbers are + for @ints.kv -> $i, $int { + $max = $int if ($int > $max); + %mapping{$int}{$i} = 1; + } + # we don't need to operate on values already at the max + %mapping{$max}:delete; + + while (%mapping.keys) { + my $elems = %mapping.values».List.flat.elems; + if ($elems > 1 && $x * 2 >= $y) { + # get the two indices + my $i = getNextIndex(%mapping); + my $j = getNextIndex(%mapping); + + # increment the values + @ints[$i]++; + @ints[$j]++; + @steps.push( + "Level 2: i=$i, j=$j, so \$ints[$i] += 1 and " ~ + "\$ints[$j] += 1\n" ~ fmtInts(@ints) + ); + $L2++; + # if the numbers we incremented are less than t