From a16bbea51edcaaf249eea11eb9028501fb2d4c1e Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Wed, 4 Jun 2025 15:46:25 +0100 Subject: RogerBW solutions for challenge no. 324 --- challenge-324/roger-bell-west/perl/ch-1.pl | 22 ++++ challenge-324/roger-bell-west/perl/ch-2.pl | 26 +++++ challenge-324/roger-bell-west/postscript/ch-1.ps | 135 +++++++++++++++++++++++ challenge-324/roger-bell-west/postscript/ch-2.ps | 101 +++++++++++++++++ challenge-324/roger-bell-west/raku/ch-1.p6 | 20 ++++ challenge-324/roger-bell-west/raku/ch-2.p6 | 18 +++ challenge-324/roger-bell-west/rust/ch-1.rs | 29 +++++ challenge-324/roger-bell-west/rust/ch-2.rs | 26 +++++ challenge-324/roger-bell-west/tests.json | 47 ++++++++ challenge-324/roger-bell-west/typst/ch-1.typ | 29 +++++ challenge-324/roger-bell-west/typst/ch-2.typ | 57 ++++++++++ 11 files changed, 510 insertions(+) create mode 100755 challenge-324/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-324/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-324/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-324/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-324/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-324/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-324/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-324/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-324/roger-bell-west/tests.json create mode 100644 challenge-324/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-324/roger-bell-west/typst/ch-2.typ diff --git a/challenge-324/roger-bell-west/perl/ch-1.pl b/challenge-324/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..b7ff021b67 --- /dev/null +++ b/challenge-324/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(twodarray([1, 2, 3, 4], 2, 2), [[1, 2], [3, 4]], 'example 1'); +is_deeply(twodarray([1, 2, 3], 1, 3), [[1, 2, 3]], 'example 2'); +is_deeply(twodarray([1, 2, 3, 4], 4, 1), [[1], [2], [3], [4]], 'example 3'); + +sub twodarray($i, $r, $c) { + my @out; + while (my ($ct, $n) = each @{$i}) { + if ($ct % $c == 0) { + push @out, []; + } + push @{$out[-1]}, $n; + } + \@out +} diff --git a/challenge-324/roger-bell-west/perl/ch-2.pl b/challenge-324/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..8679cf5674 --- /dev/null +++ b/challenge-324/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,26 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(totalxor([1, 3]), 6, 'example 1'); +is(totalxor([5, 1, 6]), 28, 'example 2'); +is(totalxor([3, 4, 5, 6, 7, 8]), 480, 'example 3'); + +use Algorithm::Combinatorics qw(combinations); + +use List::Util qw(reduce); + +sub totalxor($i) { + my $tot = 0; + foreach my $n (1 .. scalar @{$i}) { + my $ip = combinations($i, $n); + while (my $c = $ip->next) { + $tot += reduce {$a ^ $b} (0, @{$c}); + } + } + $tot +} diff --git a/challenge-324/roger-bell-west/postscript/ch-1.ps b/challenge-324/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..1774bec450 --- /dev/null +++ b/challenge-324/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,135 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + + +% end included library code + +/twodarray { + 0 dict begin + /c exch def + pop + /i exch def + [ + i enumerate.array { + aload pop + /n exch def + /ct exch def + ct c mod 0 eq { + ct 0 gt { + ] + } if + [ + } if + n + } forall + ] + ] + end +} bind def + +(twodarray) test.start +[1 2 3 4] 2 2 twodarray [[1 2] [3 4]] deepeq test +[1 2 3] 1 3 twodarray [[1 2 3]] deepeq test +[1 2 3 4] 4 1 twodarray [[1] [2] [3] [4]] deepeq test +test.end diff --git a/challenge-324/roger-bell-west/postscript/ch-2.ps b/challenge-324/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..55e2c57309 --- /dev/null +++ b/challenge-324/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,101 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + +/combinations { + 4 dict begin + /k exch def + /arr exch def + /c [ + 0 1 k 1 sub { } for + arr length + 0 + ] def + [ + { + [ + k 1 sub -1 0 { + c exch get arr exch get + } for + ] + /j 0 def + { + c j get 1 add c j 1 add get ne { + exit + } if + c j j put + /j j 1 add def + } loop + j k ge { + exit + } if + c j c j get 1 add put + } loop + ] + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + + +% end included library code + +/totalxor { + 0 dict begin + /i exch def + 0 + 1 1 i length { + /n exch def + i n combinations { + { xor } reduce add + } forall + } for + end +} bind def + +(totalxor) test.start +[1 3] totalxor 6 eq test +[5 1 6] totalxor 28 eq test +[3 4 5 6 7 8] totalxor 480 eq test +test.end diff --git a/challenge-324/roger-bell-west/raku/ch-1.p6 b/challenge-324/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..c9a15a22c0 --- /dev/null +++ b/challenge-324/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(twodarray([1, 2, 3, 4], 2, 2), [[1, 2], [3, 4]], 'example 1'); +is-deeply(twodarray([1, 2, 3], 1, 3), [[1, 2, 3],], 'example 2'); +is-deeply(twodarray([1, 2, 3, 4], 4, 1), [[1], [2], [3], [4]], 'example 3'); + +sub twodarray(@i, $r, $c) { + my @out; + for @i.kv -> $ct, $n { + if ($ct % $c == 0) { + @out.push([]); + } + @out[*-1].push($n); + } + @out +} diff --git a/challenge-324/roger-bell-west/raku/ch-2.p6 b/challenge-324/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..f74d12fc0e --- /dev/null +++ b/challenge-324/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(totalxor([1, 3]), 6, 'example 1'); +is(totalxor([5, 1, 6]), 28, 'example 2'); +is(totalxor([3, 4, 5, 6, 7, 8]), 480, 'example 3'); + +sub totalxor(@i) { + my $tot = 0; + for @i.combinations(1..@i.elems) -> @c { + $tot += reduce(&infix:<+^>, @c); + } + $tot +} + diff --git a/challenge-324/roger-bell-west/rust/ch-1.rs b/challenge-324/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..4ee795834d --- /dev/null +++ b/challenge-324/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(twodarray(vec![1, 2, 3, 4], 2, 2), vec![vec![1, 2], vec![3, 4]]); +} + +#[test] +fn test_ex2() { + assert_eq!(twodarray(vec![1, 2, 3], 1, 3), vec![vec![1, 2, 3]]); +} + +#[test] +fn test_ex3() { + assert_eq!(twodarray(vec![1, 2, 3, 4], 4, 1), vec![vec![1], vec![2], vec![3], vec![4]]); +} + +fn twodarray(i: Vec, _r: usize, c: usize) -> Vec> { + let mut out = Vec::new(); + for (ct, n) in i.into_iter().enumerate() { + if ct % c == 0 { + out.push(Vec::new()); + } + let ol = out.len() - 1; + out[ol].push(n); + } + out +} diff --git a/challenge-324/roger-bell-west/rust/ch-2.rs b/challenge-324/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..e9d159ecdc --- /dev/null +++ b/challenge-324/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,26 @@ +use permutator::Combination; + +#[test] +fn test_ex1() { + assert_eq!(totalxor(vec![1, 3]), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(totalxor(vec![5, 1, 6]), 28); +} + +#[test] +fn test_ex3() { + assert_eq!(totalxor(vec![3, 4, 5, 6, 7, 8]), 480); +} + +fn totalxor(i: Vec) -> i64 { + let mut tot = 0; + for n in 1 ..= i.len() { + i.combination(n).for_each(|l| { + tot += l.into_iter().fold(0, |acc, x| acc ^ x) + }) + } + tot +} diff --git a/challenge-324/roger-bell-west/tests.json b/challenge-324/roger-bell-west/tests.json new file mode 100644 index 0000000000..fdd4c3de0f --- /dev/null +++ b/challenge-324/roger-bell-west/tests.json @@ -0,0 +1,47 @@ +{ + "ch-1" : [ + { + "function" : "twodarray", + "multiarg" : true, + "arguments" : [ + [ 1, 2, 3, 4 ], + 2, + 2 + ], + "result" : [[1, 2], [3, 4]] + }, + { + "multiarg" : true, + "arguments" : [ + [ 1, 2, 3 ], + 1, + 3 + ], + "result" : [[1, 2, 3]] + }, + { + "multiarg" : true, + "arguments" : [ + [ 1, 2, 3, 4 ], + 4, + 1 + ], + "result" : [[1], [2], [3], [4]] + } + ], + "ch-2" : [ + { + "function" : "totalxor", + "arguments" : [ 1, 3 ], + "result" : 6 + }, + { + "arguments" : [ 5, 1, 6 ], + "result" : 28 + }, + { + "arguments" : [ 3, 4, 5, 6, 7, 8 ], + "result" : 480 + } + ] +} diff --git a/challenge-324/roger-bell-west/typst/ch-1.typ b/challenge-324/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..16805d913e --- /dev/null +++ b/challenge-324/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,29 @@ +#let twodarray(i, r, c) = { + let out = () + for (ct, n) in i.enumerate() { + if calc.rem-euclid(ct, c) == 0 { + out.push(()) + } + let ol = out.len() - 1 + out.at(ol).push(n) + } + out +} + +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +Test 1: + #testresult(twodarray((1, 2, 3, 4), 2, 2) == ((1, 2), (3, 4))) + +Test 2: + #testresult(twodarray((1, 2, 3), 1, 3) == ((1, 2, 3),)) + +Test 3: + #testresult(twodarray((1, 2, 3, 4), 4, 1) == ((1,), (2,), (3,), (4,))) + diff --git a/challenge-324/roger-bell-west/typst/ch-2.typ b/challenge-324/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..5d4496d0aa --- /dev/null +++ b/challenge-324/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,57 @@ +#let combinations(arr, k) = { + let c = () + for i in range(0, k) { + c.push(i) + } + c.push(arr.len()) + c.push(0) + let out = () + while true { + let inner = () + for i in range(k - 1, 0 - 1, step: -1) { + inner.push(arr.at(c.at(i))) + } + out.push(inner) + let j = 0 + while true { + if c.at(j) + 1 != c.at(j + 1) { + break + } + c.at(j) = j + j += 1 + } + if j >= k { + break + } + c.at(j) += 1 + } + out +} + +#let totalxor(i) = { + let tot = 0 + for n in range(1, i.len() + 1) { + for l in combinations(i, n) { + tot += l.reduce((a, b) => a.bit-xor(b)) + } + } + tot +} + +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +Test 1: + #testresult(totalxor((1, 3)) == 6) + +Test 2: + #testresult(totalxor((5, 1, 6)) == 28) + +Test 3: + #testresult(totalxor((3, 4, 5, 6, 7, 8)) == 480) + -- cgit