From ff372a96c896751bacce3f2521ba5d880041ff42 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 14 Feb 2022 09:03:10 +0000 Subject: initial 152 --- challenge-152/mark-anderson/raku/ch-1.raku | 11 +++++++++++ challenge-152/mark-anderson/raku/ch-2.raku | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge-152/mark-anderson/raku/ch-1.raku create mode 100644 challenge-152/mark-anderson/raku/ch-2.raku (limited to 'challenge-152') diff --git a/challenge-152/mark-anderson/raku/ch-1.raku b/challenge-152/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..bfe8e1272e --- /dev/null +++ b/challenge-152/mark-anderson/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku + +use Test; + +is triangle-sum([ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ]), 8; +is triangle-sum([ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ]), 9; + +sub triangle-sum(@a) +{ + [+] @a>>.min; +} diff --git a/challenge-152/mark-anderson/raku/ch-2.raku b/challenge-152/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..25144dbcc2 --- /dev/null +++ b/challenge-152/mark-anderson/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use Test; + +is rectangle-area((-1,0), (2,2), (0,-1), (4,4)), 22; +is rectangle-area((-3,-1), (1,3), (-1,-3), (2,2)), 25; +is rectangle-area((1,1), (2,2), (3,3), (4,4)), 2; + +sub rectangle-area($bl_1, $tr_1, $bl_2, $tr_2) +{ + my $area_1 = abs($bl_1[0] - $tr_1[0]) * abs($bl_1[1] - $tr_1[1]); + my $area_2 = abs($bl_2[0] - $tr_2[0]) * abs($bl_2[1] - $tr_2[1]); + + my $int_bl_x = max($bl_1[0], $bl_2[0]); + my $int_tr_x = min($tr_1[0], $tr_2[0]); + my $int_bl_y = max($bl_1[1], $bl_2[1]); + my $int_tr_y = min($tr_1[1], $tr_2[1]); + + my $int_x_dist = $int_tr_x - $int_bl_x; + my $int_y_dist = $int_tr_y - $int_bl_y; + + my $area_int = $int_x_dist|$int_y_dist < 0 ?? 0 !! $int_x_dist*$int_y_dist; + + return $area_1 + $area_2 - $area_int; +} -- cgit From 7ff17a30dcda31d1a8644598b6ba6548362cf581 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 14 Feb 2022 09:14:14 +0000 Subject: Solutions for challenge #152 --- challenge-152/roger-bell-west/javascript/ch-1.js | 24 +++++++++ challenge-152/roger-bell-west/javascript/ch-2.js | 48 +++++++++++++++++ challenge-152/roger-bell-west/kotlin/ch-1.kt | 25 +++++++++ challenge-152/roger-bell-west/kotlin/ch-2.kt | 55 ++++++++++++++++++++ challenge-152/roger-bell-west/lua/ch-1.lua | 27 ++++++++++ challenge-152/roger-bell-west/lua/ch-2.lua | 42 +++++++++++++++ challenge-152/roger-bell-west/perl/ch-1.pl | 21 ++++++++ challenge-152/roger-bell-west/perl/ch-2.pl | 59 +++++++++++++++++++++ challenge-152/roger-bell-west/postscript/ch-1.ps | 22 ++++++++ challenge-152/roger-bell-west/postscript/ch-2.ps | 65 ++++++++++++++++++++++++ challenge-152/roger-bell-west/python/ch-1.py | 19 +++++++ challenge-152/roger-bell-west/python/ch-2.py | 35 +++++++++++++ challenge-152/roger-bell-west/raku/ch-1.p6 | 17 +++++++ challenge-152/roger-bell-west/raku/ch-2.p6 | 42 +++++++++++++++ challenge-152/roger-bell-west/ruby/ch-1.rb | 23 +++++++++ challenge-152/roger-bell-west/ruby/ch-2.rb | 46 +++++++++++++++++ challenge-152/roger-bell-west/rust/ch-1.rs | 38 ++++++++++++++ challenge-152/roger-bell-west/rust/ch-2.rs | 58 +++++++++++++++++++++ 18 files changed, 666 insertions(+) create mode 100755 challenge-152/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-152/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-152/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-152/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-152/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-152/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-152/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-152/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-152/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-152/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-152/roger-bell-west/python/ch-1.py create mode 100755 challenge-152/roger-bell-west/python/ch-2.py create mode 100755 challenge-152/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-152/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-152/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-152/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-152/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-152/roger-bell-west/rust/ch-2.rs (limited to 'challenge-152') diff --git a/challenge-152/roger-bell-west/javascript/ch-1.js b/challenge-152/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..59217c6c86 --- /dev/null +++ b/challenge-152/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,24 @@ +#! /usr/bin/node + +function tsp (tree) { + let mp=0; + for (let r of tree) { + mp += Math.min(...r); + } + return mp; +} + +if (tsp([[1],[5,3],[2,3,4],[7,1,0,2],[6,4,5,2,8]]) == 8) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (tsp([[5],[2,3],[4,1,4],[0,1,2,3],[7,2,4,1,9]]) == 9) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-152/roger-bell-west/javascript/ch-2.js b/challenge-152/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..6a97ee21b0 --- /dev/null +++ b/challenge-152/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,48 @@ +#! /usr/bin/node + +function Rect(xy1,xy2) { + let r=Object.create(Rect.methods); + r.xy1=[Math.min(xy1[0],xy2[0]),Math.min(xy1[1],xy2[1])]; + r.xy2=[Math.max(xy1[0],xy2[0]),Math.max(xy1[1],xy2[1])]; + return r; +} + +Rect.methods = { + area() { + let area=1; + for (let axis=0;axis<=1;axis++) { + area *= this.xy2[axis]-this.xy1[axis]; + } + return area; + }, + + overlap(other) { + let area=1; + for (let axis=0;axis<=1;axis++) { + area *= Math.max(0, + Math.min(this.xy2[axis],other.xy2[axis])- + Math.max(this.xy1[axis],other.xy1[axis]) + ); + } + return area; + }, + + fullarea(other) { + return this.area()+other.area()-this.overlap(other); + } +} + +if (new Rect([-1,0],[2,2]).fullarea(new Rect([0,-1],[4,4])) == 22) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (new Rect([-3,-1],[1,3]).fullarea(new Rect([-1,-3],[2,2])) == 25) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); + diff --git a/challenge-152/roger-bell-west/kotlin/ch-1.kt b/challenge-152/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..84bd5bfe8b --- /dev/null +++ b/challenge-152/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,25 @@ +fun tsp(tree: List>): Int { + var mp=0 + for (r in tree) { + val rt = r.minOrNull() + if (rt != null) { + mp += rt + } + } + return mp +} + +fun main() { + if (tsp(listOf(listOf(1),listOf(5,3),listOf(2,3,4),listOf(7,1,0,2),listOf(6,4,5,2,8))) == 8) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (tsp(listOf(listOf(5),listOf(2,3),listOf(4,1,4),listOf(0,1,2,3),listOf(7,2,4,1,9))) == 9) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-152/roger-bell-west/kotlin/ch-2.kt b/challenge-152/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c1baafecd8 --- /dev/null +++ b/challenge-152/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,55 @@ +import kotlin.math.min +import kotlin.math.max + +class Rect(_xy1: List, _xy2: List) { + val xy1=listOf( + min(_xy1[0],_xy2[0]), + min(_xy1[1],_xy2[1]), + ); + val xy2=listOf( + max(_xy1[0],_xy2[0]), + max(_xy1[1],_xy2[1]), + ); + + fun area(): Int { + var area=1 + for (axis in 0..1) { + area *= xy2[axis]-xy1[axis] + } + return area + } + + fun overlap(other: Rect): Int { + var area=1 + for (axis in 0..1) { + area *= max(0, + min(xy2[axis],other.xy2[axis])- + max(xy1[axis],other.xy1[axis]) + ) + } + return area + } + + fun fullarea(other: Rect): Int { + return area()+other.area()-overlap(other) + } +} + +fun main() { + if (Rect(listOf(-1,0),listOf(2,2)) + .fullarea(Rect(listOf(0,-1),listOf(4,4))) + == 22) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (Rect(listOf(-3,-1),listOf(1,3)) + .fullarea(Rect(listOf(-1,-3),listOf(2,2))) + == 25) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-152/roger-bell-west/lua/ch-1.lua b/challenge-152/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..ecb3fb5a2a --- /dev/null +++ b/challenge-152/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,27 @@ +#! /usr/bin/lua + +function tsp(tree) + mp=0 + for k1,v1 in pairs(tree) do + mr=v1[1] + for k2,v2 in pairs(v1) do + mr=math.min(mr,v2) + end + mp = mp + mr + end + return mp +end + +if tsp({{1},{5,3},{2,3,4},{7,1,0,2},{6,4,5,2,8}}) == 8 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if tsp({{5},{2,3},{4,1,4},{0,1,2,3},{7,2,4,1,9}}) == 9 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-152/roger-bell-west/lua/ch-2.lua b/challenge-152/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..4e9a29e330 --- /dev/null +++ b/challenge-152/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,42 @@ +#! /usr/bin/lua + +Rect = {x1 = 0, y1 = 0, x2 = 0, y2 = 0, a = {}, b = {}} + +function Rect:new(a,b) + local o = {} + self.__index = self + setmetatable(o,self) + o.x1=math.min(a[1],b[1]) + o.x2=math.max(a[1],b[1]) + o.y1=math.min(a[2],b[2]) + o.y2=math.max(a[2],b[2]) + return o +end + +function Rect:area() + return (self.x2-self.x1)*(self.y2-self.y1) +end + +function Rect:overlap(other) + local x=math.max(0,math.min(self.x2,other.x2)-math.max(self.x1,other.x1)) + local y=math.max(0,math.min(self.y2,other.y2)-math.max(self.y1,other.y1)) + return x*y +end + +function Rect:fullarea(other) + return self:area()+other:area()-self:overlap(other) +end + +if Rect:new({-1,0},{2,2}):fullarea(Rect:new({0,-1},{4,4})) == 22 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if Rect:new({-3,-1},{1,3}):fullarea(Rect:new({-1,-3},{2,2})) == 25 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-152/roger-bell-west/perl/ch-1.pl b/challenge-152/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..54328af70c --- /dev/null +++ b/challenge-152/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is(tsp([[1],[5,3],[2,3,4],[7,1,0,2],[6,4,5,2,8]]),8,'example 1'); + +is(tsp([[5],[2,3],[4,1,4],[0,1,2,3],[7,2,4,1,9]]),9,'example 2'); + +use List::Util qw(min); + +sub tsp { + my $tree = shift; + my $mp=0; + foreach my $r (@{$tree}) { + $mp+=min(@{$r}); + } + return $mp; +} diff --git a/challenge-152/roger-bell-west/perl/ch-2.pl b/challenge-152/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..9bf21fcfcc --- /dev/null +++ b/challenge-152/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,59 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is(Rect->new([-1,0],[2,2])->fullarea(Rect->new([0,-1],[4,4])),22,'example 1'); + +is(Rect->new([-3,-1],[1,3])->fullarea(Rect->new([-1,-3],[2,2])),25,'example 2'); + +package Rect; + +use List::Util qw(min max); + +sub new { + my ($class,$xy1,$xy2)=@_; + my $self={ + xy1 => [ + min($xy1->[0],$xy2->[0]), + min($xy1->[1],$xy2->[1]), + ], + xy2 => [ + max($xy1->[0],$xy2->[0]), + max($xy1->[1],$xy2->[1]), + ], + }; + bless $self,$class; + return $self; +} + +sub area { + my $self=shift; + my $area=1; + foreach my $axis (0,1) { + $area *= $self->{xy2}[$axis] - $self->{xy1}[$axis]; + } + return $area; +} + + +sub overlap { + my $self=shift; + my $other=shift; + my $area=1; + foreach my $axis (0,1) { + $area *= max(0, + min($self->{xy2}[$axis],$other->{xy2}[$axis])- + max($self->{xy1}[$axis],$other->{xy1}[$axis]) + ); + } + return $area; +} + +sub fullarea { + my $self=shift; + my $other=shift; + return $self->area()+$other->area()-$self->overlap($other); +} diff --git a/challenge-152/roger-bell-west/postscript/ch-1.ps b/challenge-152/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..b08abc9a45 --- /dev/null +++ b/challenge-152/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,22 @@ +%!PS + +/tsp { + 2 dict begin + /mp 0 def + { + dup 0 get /mr exch def + { + dup mr lt { + /mr exch def + } { + pop + } ifelse + } forall + /mp mp mr add def + } forall + mp + end +} bind def + +[ [ 1 ] [ 5 3 ] [ 2 3 4 ] [ 7 1 0 2 ] [ 6 4 5 2 8 ] ] tsp 8 eq { (Pass) } { (FAIL) } ifelse print ( ) print +[ [ 5 ] [ 2 3 ] [ 4 1 4 ] [ 0 1 2 3 ] [ 7 2 4 1 9 ] ] tsp 9 eq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-152/roger-bell-west/postscript/ch-2.ps b/challenge-152/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..31453f353c --- /dev/null +++ b/challenge-152/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,65 @@ +%!PS + +/rect.new { + 2 dict begin + dup + 0 get /a exch def + 1 get /b exch def + [ + [ + a 0 get b 0 get min + a 1 get b 1 get min + ] + [ + a 0 get b 0 get max + a 1 get b 1 get max + ] + ] + end +} bind def + +/rect.area { + 2 dict begin + dup + 0 get /a exch def + 1 get /b exch def + b 0 get a 0 get sub + b 1 get a 1 get sub + mul + end +} bind def + +/rect.overlap { + 5 dict begin + dup + 0 get /a exch def + 1 get /b exch def + dup + 0 get /c exch def + 1 get /d exch def + /area 1 def + 0 1 1 { + /axis exch def + b axis get d axis get min + a axis get c axis get max + sub + 0 max area mul /area exch def + } for + area + end +} bind def + +/rect.fullarea { + 1 dict begin + /area 0 def + dup rect.area area add /area exch def + exch + dup rect.area area add /area exch def + rect.overlap area exch sub + end +} bind def + +[ [ -1 0 ] [ 2 2 ] ] rect.new [ [ 0 -1 ] [ 4 4 ] ] rect.new rect.fullarea +22 eq { (Pass) } { (FAIL) } ifelse print ( ) print +[ [ -3 -1 ] [ 1 3 ] ] rect.new [ [ -1 -3 ] [ 2 2 ] ] rect.new rect.fullarea +25 eq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-152/roger-bell-west/python/ch-1.py b/challenge-152/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..9da8902aeb --- /dev/null +++ b/challenge-152/roger-bell-west/python/ch-1.py @@ -0,0 +1,19 @@ +#! /usr/bin/python3 + +import unittest + +def tsp(tree): + mp=0 + for r in tree: + mp += min(r) + return mp + +class TestTsp(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(tsp([[1],[5,3],[2,3,4],[7,1,0,2],[6,4,5,2,8]]),8,'example 1') + + def test_ex2(self): + self.assertEqual(tsp([[5],[2,3],[4,1,4],[0,1,2,3],[7,2,4,1,9]]),9,'example 2') + +unittest.main() diff --git a/challenge-152/roger-bell-west/python/ch-2.py b/challenge-152/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..52518be11a --- /dev/null +++ b/challenge-152/roger-bell-west/python/ch-2.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +import unittest + +class Rect(object): + + def __init__(self,_xy1,_xy2): + self.xy1=[min(_xy1[0],_xy2[0]),min(_xy1[1],_xy2[1])] + self.xy2=[max(_xy1[0],_xy2[0]),max(_xy1[1],_xy2[1])] + + def area(self): + area=1 + for axis in range(2): + area *= self.xy2[axis]-self.xy1[axis] + return area + + def overlap(self,other): + area=1 + for axis in range(2): + area *= max(0, + min(self.xy2[axis],other.xy2[axis])- + max(self.xy1[axis],other.xy1[axis])) + return area + + def fullarea(self,other): + return self.area()+other.area()-self.overlap(other) + +class TestRect(unittest.TestCase): + def test_ex1(self): + self.assertEqual(Rect([-1,0],[2,2]).fullarea(Rect([0,-1],[4,4])),22,'example 1') + + def test_ex2(self): + self.assertEqual(Rect([-3,-1],[1,3]).fullarea(Rect([-1,-3],[2,2])),25,'example 1') + +unittest.main() diff --git a/challenge-152/roger-bell-west/raku/ch-1.p6 b/challenge-152/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..9806128e9f --- /dev/null +++ b/challenge-152/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(tsp([[1],[5,3],[2,3,4],[7,1,0,2],[6,4,5,2,8]]),8,'example 1'); + +is(tsp([[5],[2,3],[4,1,4],[0,1,2,3],[7,2,4,1,9]]),9,'example 2'); + +sub tsp(@ttree) { + my $mp=0; + for @ttree -> @r { + $mp += min(@r); + } + return $mp; +} diff --git a/challenge-152/roger-bell-west/raku/ch-2.p6 b/challenge-152/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..521d266c3c --- /dev/null +++ b/challenge-152/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,42 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +class Rect { + has @.xy1 is rw; + has @.xy2 is rw; + + submethod BUILD(:@xy1,:@xy2) { + @!xy1=[min(@xy1[0],@xy2[0]),min(@xy1[1],@xy2[1])]; + @!xy2=[max(@xy1[0],@xy2[0]),max(@xy1[1],@xy2[1])]; + } + + method area() { + my $area=1; + for (0,1) -> $axis { + $area *= @.xy2[$axis]-@.xy1[$axis]; + } + return $area; + } + + method overlap($other) { + my $area=1; + for (0,1) -> $axis { + $area *= max(0, + min(@.xy2[$axis],$other.xy2[$axis])- + max(@.xy1[$axis],$other.xy1[$axis]) + ); + } + return $area; + } + + method fullarea($other) { + return self.area()+$other.area()-self.overlap($other); + } +} + +is(Rect.new(xy1 => [-1,0],xy2 => [2,2]).fullarea(Rect.new(xy1 => [0,-1],xy2 => [4,4])),22,'example 1'); + +is(Rect.new(xy1 => [-3,-1],xy2 => [1,3]).fullarea(Rect.new(xy1 => [-1,-3],xy2 => [2,2])),25,'example 2'); diff --git a/challenge-152/roger-bell-west/ruby/ch-1.rb b/challenge-152/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..02e5c7f37f --- /dev/null +++ b/challenge-152/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,23 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def tsp(tree) + mp=0 + tree.each do |r| + mp += r.min + end + return mp +end + +class TestTsp < Test::Unit::TestCase + + def test_ex1 + assert_equal(8,tsp([[1],[5,3],[2,3,4],[7,1,0,2],[6,4,5,2,8]])) + end + + def test_ex2 + assert_equal(9,tsp([[5],[2,3],[4,1,4],[0,1,2,3],[7,2,4,1,9]])) + end + +end diff --git a/challenge-152/roger-bell-west/ruby/ch-2.rb b/challenge-152/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..56ce377ebb --- /dev/null +++ b/challenge-152/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,46 @@ +#! /usr/bin/ruby + +require 'test/unit' + +class Rect + attr_reader :xy1, :xy2 + def initialize(_xy1,_xy2) + @xy1=[[_xy1[0],_xy2[0]].min,[_xy1[1],_xy2[1]].min] + @xy2=[[_xy1[0],_xy2[0]].max,[_xy1[1],_xy2[1]].max] + end + + def area + area=1 + 0.upto(1) do |axis| + area *= @xy2[axis]-@xy1[axis] + end + return area + end + + def overlap(other) + area=1 + 0.upto(1) do |axis| + area *= [0, + [@xy2[axis],other.xy2[axis]].min- + [@xy1[axis],other.xy1[axis]].max + ].max + end + return area + end + + def fullarea(other) + return area()+other.area()-overlap(other) + end +end + +class TestRect < Test::Unit::TestCase + + def test_ex1 + assert_equal(22,Rect.new([-1,0],[2,2]).fullarea(Rect.new([0,-1],[4,4]))) + end + + def test_ex2 + assert_equal(25,Rect.new([-3,-1],[1,3]).fullarea(Rect.new([-1,-3],[2,2]))) + end + +end diff --git a/challenge-152/roger-bell-west/rust/ch-1.rs b/challenge-152/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..38c7457369 --- /dev/null +++ b/challenge-152/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,38 @@ +#! /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!( + tsp(vec![ + vec![1], + vec![5, 3], + vec![2, 3, 4], + vec![7, 1, 0, 2], + vec![6, 4, 5, 2, 8] + ]), + 8 + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + tsp(vec![ + vec![5], + vec![2, 3], + vec![4, 1, 4], + vec![0, 1, 2, 3], + vec![7, 2, 4, 1, 9] + ]), + 9 + ); +} + +fn tsp(tree: Vec>) -> u32 { + let mut mp = 0; + for r in tree { + mp += r.iter().min().unwrap(); + } + return mp; +} diff --git a/challenge-152/roger-bell-west/rust/ch-2.rs b/challenge-152/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..dcbd9dabf0 --- /dev/null +++ b/challenge-152/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,58 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::cmp::{max, min}; + +#[test] +fn test_ex1() { + assert_eq!( + Rect::new([-1, 0], [2, 2]).fullarea(Rect::new([0, -1], [4, 4])), + 22 + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + Rect::new([-3, -1], [1, 3]).fullarea(Rect::new([-1, -3], [2, 2])), + 25 + ); +} + +pub struct Rect { + xy1: [i32; 2], + xy2: [i32; 2], +} + +impl Rect { + pub fn new(xy1: [i32; 2], xy2: [i32; 2]) -> Rect { + Rect { + xy1: [min(xy1[0], xy2[0]), min(xy1[1], xy2[1])], + xy2: [max(xy1[0], xy2[0]), max(xy1[1], xy2[1])], + } + } + + pub fn area(&self) -> i32 { + let mut area = 1; + for axis in 0..=1 { + area *= self.xy2[axis] - self.xy1[axis]; + } + return area; + } + + pub fn overlap(&self, other: Rect) -> i32 { + let mut area = 1; + for axis in 0..=1 { + area *= max( + 0, + min(self.xy2[axis], other.xy2[axis]) + - max(self.xy1[axis], other.xy1[axis]), + ); + } + return area; + } + + pub fn fullarea(&self, other: Rect) -> i32 { + return self.area() + other.area() - self.overlap(other); + } +} -- cgit From f5432112f6df7ec1b7c7cbf7cb636e8970e3d011 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Feb 2022 11:14:34 +0100 Subject: Task 1 done --- challenge-152/luca-ferrari/raku/ch-1.p6 | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 challenge-152/luca-ferrari/raku/ch-1.p6 (limited to 'challenge-152') diff --git a/challenge-152/luca-ferrari/raku/ch-1.p6 b/challenge-152/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..d4414394c1 --- /dev/null +++ b/challenge-152/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,7 @@ +#!raku + +sub MAIN() { + my @triangle = [ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ]; + my $sum += $_.min for @triangle; + $sum.say; +} -- cgit From 7010422e62df71969f2037b48abd2e3e62125591 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Feb 2022 12:38:59 +0100 Subject: Task 2 done --- challenge-152/luca-ferrari/raku/ch-2.p6 | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 challenge-152/luca-ferrari/raku/ch-2.p6 (limited to 'challenge-152') diff --git a/challenge-152/luca-ferrari/raku/ch-2.p6 b/challenge-152/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..051e63023b --- /dev/null +++ b/challenge-152/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,47 @@ +#!raku + +class Point { + has Int $.x; + has Int $.y; +} + + +class Rectangle { + has Point $.corner-left; + has Point $.corner-right; + + method area() { + abs( $!corner-right.x - $!corner-left.x ) + * abs( $!corner-right.y - $!corner-left.y ); + } + + method overlapping-area( Rectangle $r ) { + my $left = Point.new( x => max( $!corner-left.x, $r.corner-left.x ), + y => max( $!corner-left.y, $r.corner-left.y ) + ); + my $right = Point.new( x => min( $!corner-right.x, $r.corner-right.x ), + y => min( $!corner-right.y, $r.corner-right.y ) + ); + + my Rectangle $overlapping = Rectangle.new: + corner-left => $left, + corner-right => $right; + return $overlapping.area; + + } +} + +# Example: +# $ raku ch-2.p6 -1 0 2 2 0 -1 4 4 +# 22 +# +# $ raku ch-2.p6 -3 -1 1 3 -1 -3 2 2 +# 25 +sub MAIN( *@points where { @points.elems == 8 && @points.grep( * ~~ Int ) == @points.elems } ) { + my @corners; + @corners.push: Point.new( x => $_[ 0 ].Int, y => $_[ 1 ].Int ) for @points.rotor( 2 ); + my Rectangle $r1 = Rectangle.new: corner-left => @corners[ 0 ], corner-right => @corners[ 1 ]; + my Rectangle $r2 = Rectangle.new: corner-left => @corners[ 2 ], corner-right => @corners[ 3 ]; + + "{ $r1.area + $r2.area - $r1.overlapping-area( $r2 ) }".say; +} -- cgit From e7e88e0ea98728111f4a31b39c213cf6d6ac5c8e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Feb 2022 13:25:55 +0100 Subject: Improved task 2: avoid non overlapping rectangles. --- challenge-152/luca-ferrari/raku/ch-2.p6 | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'challenge-152') diff --git a/challenge-152/luca-ferrari/raku/ch-2.p6 b/challenge-152/luca-ferrari/raku/ch-2.p6 index 051e63023b..65be181aea 100755 --- a/challenge-152/luca-ferrari/raku/ch-2.p6 +++ b/challenge-152/luca-ferrari/raku/ch-2.p6 @@ -15,7 +15,19 @@ class Rectangle { * abs( $!corner-right.y - $!corner-left.y ); } + + method !is-overlapping( Rectangle $r ) { + ( $!corner-left.x <= $r.corner-left.x <= $!corner-right.x + || $!corner-left.x <= $r.corner-righ.x <= $!corner-right.x ) + && + ( $!corner-left.y <= $r.corner-left.y <= $!corner-right.y + || $!corner-left.y <= $r.corner-right.y <= $!corner-right.y ); + + } + method overlapping-area( Rectangle $r ) { + return 0 if ! self!is-overlapping( $r ); + my $left = Point.new( x => max( $!corner-left.x, $r.corner-left.x ), y => max( $!corner-left.y, $r.corner-left.y ) ); -- cgit From d863e9de7ddd1634fd55dc48bed0f52143c4ae2c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Feb 2022 13:39:40 +0100 Subject: Improved Task 2. Blog references --- challenge-152/luca-ferrari/blog-1.txt | 1 + challenge-152/luca-ferrari/blog-2.txt | 1 + challenge-152/luca-ferrari/raku/ch-2.p6 | 9 ++++----- 3 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 challenge-152/luca-ferrari/blog-1.txt create mode 100644 challenge-152/luca-ferrari/blog-2.txt (limited to 'challenge-152') diff --git a/challenge-152/luca-ferrari/blog-1.txt b/challenge-152/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..1ff054de6b --- /dev/null +++ b/challenge-152/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/02/14/PerlWeeklyChallenge152.html#task1 diff --git a/challenge-152/luca-ferrari/blog-2.txt b/challenge-152/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e86f18d955 --- /dev/null +++ b/challenge-152/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/02/14/PerlWeeklyChallenge152.html#task2 diff --git a/challenge-152/luca-ferrari/raku/ch-2.p6 b/challenge-152/luca-ferrari/raku/ch-2.p6 index 65be181aea..c68c8c447a 100755 --- a/challenge-152/luca-ferrari/raku/ch-2.p6 +++ b/challenge-152/luca-ferrari/raku/ch-2.p6 @@ -50,10 +50,9 @@ class Rectangle { # $ raku ch-2.p6 -3 -1 1 3 -1 -3 2 2 # 25 sub MAIN( *@points where { @points.elems == 8 && @points.grep( * ~~ Int ) == @points.elems } ) { - my @corners; - @corners.push: Point.new( x => $_[ 0 ].Int, y => $_[ 1 ].Int ) for @points.rotor( 2 ); - my Rectangle $r1 = Rectangle.new: corner-left => @corners[ 0 ], corner-right => @corners[ 1 ]; - my Rectangle $r2 = Rectangle.new: corner-left => @corners[ 2 ], corner-right => @corners[ 3 ]; - + my @corners = @points.rotor( 2 ).map: { Point.new( x => $_[ 0 ].Int, y => $_[ 1 ].Int ) }; + my Rectangle ( $r1, $r2 ) = @corners.rotor( 2 ).map: { Rectangle.new( + corner-left => $_[ 0 ], + corner-right => $_[ 1 ] ) }; "{ $r1.area + $r2.area - $r1.overlapping-area( $r2 ) }".say; } -- cgit From 9cd8ddc288cd207c1c719ade20843bcb9833b1d4 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 14 Feb 2022 16:19:34 +0000 Subject: Challenge 152 Solutions (Raku) --- challenge-152/mark-anderson/raku/ch-2.raku | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'challenge-152') diff --git a/challenge-152/mark-anderson/raku/ch-2.raku b/challenge-152/mark-anderson/raku/ch-2.raku index 25144dbcc2..10e30451d0 100644 --- a/challenge-152/mark-anderson/raku/ch-2.raku +++ b/challenge-152/mark-anderson/raku/ch-2.raku @@ -2,24 +2,24 @@ use Test; -is rectangle-area((-1,0), (2,2), (0,-1), (4,4)), 22; -is rectangle-area((-3,-1), (1,3), (-1,-3), (2,2)), 25; -is rectangle-area((1,1), (2,2), (3,3), (4,4)), 2; +is rectangle-area((-1,0), (2,2), (0,-1), (4,4)), 22; +is rectangle-area((-3,-1), (1,3), (-1,-3), (2,2)), 25; +is rectangle-area((1,1), (2,2), (3,3), (4,4)), 2; +is rectangle-area((-1,-1), (-2,-2), (1,1), (2,2)), 2; +is rectangle-area((1,1), (2,2), (-1,-1), (-2,-2)), 2; +is rectangle-area((3,3), (4,4), (1,1), (2,2)), 2; +is rectangle-area((4,2), (7,5), (2,3), (5,7)), 19; -sub rectangle-area($bl_1, $tr_1, $bl_2, $tr_2) +sub rectangle-area(+@p) { - my $area_1 = abs($bl_1[0] - $tr_1[0]) * abs($bl_1[1] - $tr_1[1]); - my $area_2 = abs($bl_2[0] - $tr_2[0]) * abs($bl_2[1] - $tr_2[1]); - - my $int_bl_x = max($bl_1[0], $bl_2[0]); - my $int_tr_x = min($tr_1[0], $tr_2[0]); - my $int_bl_y = max($bl_1[1], $bl_2[1]); - my $int_tr_y = min($tr_1[1], $tr_2[1]); + my $p = @p.map(|*); + + my $int = flat (($p[0,4], $p[1,5])>>.max, ($p[2,6], $p[3,7])>>.min); - my $int_x_dist = $int_tr_x - $int_bl_x; - my $int_y_dist = $int_tr_y - $int_bl_y; + my $int-x-dist = [-] $int[2,0]; + my $int-y-dist = [-] $int[3,1]; + + my $int-area = $int-x-dist|$int-y-dist < 0 ?? 0 !! $int-x-dist * $int-y-dist; - my $area_int = $int_x_dist|$int_y_dist < 0 ?? 0 !! $int_x_dist*$int_y_dist; - - return $area_1 + $area_2 - $area_int; + abs(([-] $p[0,2]) * ([-] $p[1,3])) + abs(([-] $p[4,6]) * ([-] $p[5,7])) - $int-area; } -- cgit From f0d3af79ed8ef93469b40045eeea72ac221b68d2 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 14 Feb 2022 16:25:54 +0000 Subject: Challenge 152 Solutions (Raku) --- challenge-152/mark-anderson/raku/ch-2.raku | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'challenge-152') diff --git a/challenge-152/mark-anderson/raku/ch-2.raku b/challenge-152/mark-anderson/raku/ch-2.raku index 10e30451d0..68d0d5f0b7 100644 --- a/challenge-152/mark-anderson/raku/ch-2.raku +++ b/challenge-152/mark-anderson/raku/ch-2.raku @@ -16,10 +16,10 @@ sub rectangle-area(+@p) my $int = flat (($p[0,4], $p[1,5])>>.max, ($p[2,6], $p[3,7])>>.min); - my $int-x-dist = [-] $int[2,0]; - my $int-y-dist = [-] $int[3,1]; + my $int-x = [-] $int[2,0]; + my $int-y = [-] $int[3,1]; - my $int-area = $int-x-dist|$int-y-dist < 0 ?? 0 !! $int-x-dist * $int-y-dist; + my $int-area = $int-x|$int-y < 0 ?? 0 !! $int-x * $int-y; abs(([-] $p[0,2]) * ([-] $p[1,3])) + abs(([-] $p[4,6]) * ([-] $p[5,7])) - $int-area; } -- cgit From 712c3c3b0d3cc9c06e24047637dc0c97dbee57d7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 14 Feb 2022 16:34:05 +0000 Subject: Challenge 152 Solutions (Raku) --- challenge-152/mark-anderson/raku/ch-2.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-152') diff --git a/challenge-152/mark-anderson/raku/ch-2.raku b/challenge-152/mark-anderson/raku/ch-2.raku index 68d0d5f0b7..c325a98db0 100644 --- a/challenge-152/mark-anderson/raku/ch-2.raku +++ b/challenge-152/mark-anderson/raku/ch-2.raku @@ -14,12 +14,12 @@ sub rectangle-area(+@p) { my $p = @p.map(|*); - my $int = flat (($p[0,4], $p[1,5])>>.max, ($p[2,6], $p[3,7])>>.min); + my $int = flat ($p[0,4], $p[1,5])>>.max, ($p[2,6], $p[3,7])>>.min; my $int-x = [-] $int[2,0]; my $int-y = [-] $int[3,1]; my $int-area = $int-x|$int-y < 0 ?? 0 !! $int-x * $int-y; - abs(([-] $p[0,2]) * ([-] $p[1,3])) + abs(([-] $p[4,6]) * ([-] $p[5,7])) - $int-area; + ([-] $p[0,2]) * ([-] $p[1,3]) + ([-] $p[4,6]) * ([-] $p[5,7]) - $int-area; } -- cgit From d6bba8467dbed369a17a8c8924d4cf51de6563b6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 14 Feb 2022 19:56:09 +0100 Subject: Week 152: Solutions in 8 languages. --- challenge-152/abigail/README.md | 6 ---- challenge-152/abigail/awk/ch-1.awk | 30 ++++++++++++++++ challenge-152/abigail/awk/ch-2.awk | 23 ++++++++++++ challenge-152/abigail/bash/ch-1.sh | 32 +++++++++++++++++ challenge-152/abigail/bash/ch-2.sh | 36 +++++++++++++++++++ challenge-152/abigail/c/ch-1.c | 42 ++++++++++++++++++++++ challenge-152/abigail/c/ch-2.c | 31 ++++++++++++++++ challenge-152/abigail/lua/ch-1.lua | 30 ++++++++++++++++ challenge-152/abigail/lua/ch-2.lua | 29 +++++++++++++++ challenge-152/abigail/node/ch-1.js | 21 +++++++++++ challenge-152/abigail/node/ch-2.js | 25 +++++++++++++ challenge-152/abigail/perl/ch-1.pl | 40 +++++++++++++++++++++ challenge-152/abigail/perl/ch-2.pl | 64 ++++++++++++++++++++++++++++++++++ challenge-152/abigail/python/ch-1.py | 28 +++++++++++++++ challenge-152/abigail/python/ch-2.py | 23 ++++++++++++ challenge-152/abigail/ruby/ch-1.rb | 30 ++++++++++++++++ challenge-152/abigail/ruby/ch-2.rb | 24 +++++++++++++ challenge-152/abigail/t/ctest.ini | 9 +++++ challenge-152/abigail/t/input-1-1 | 2 ++ challenge-152/abigail/t/input-2-1 | 2 ++ challenge-152/abigail/t/input-2-2 | 2 ++ challenge-152/abigail/t/output-1-1.exp | 2 ++ challenge-152/abigail/t/output-2-1.exp | 2 ++ challenge-152/abigail/t/output-2-2.exp | 2 ++ 24 files changed, 529 insertions(+), 6 deletions(-) create mode 100644 challenge-152/abigail/awk/ch-1.awk create mode 100644 challenge-152/abigail/awk/ch-2.awk create mode 100644 challenge-152/abigail/bash/ch-1.sh create mode 100644 challenge-152/abigail/bash/ch-2.sh create mode 100644 challenge-152/abigail/c/ch-1.c create mode 100644 challenge-152/abigail/c/ch-2.c create mode 100644 challenge-152/abigail/lua/ch-1.lua create mode 100644 challenge-152/abigail/lua/ch-2.lua create mode 100644 challenge-152/abigail/node/ch-1.js create mode 100644 challenge-152/abigail/node/ch-2.js create mode 100644 challenge-152/abigail/perl/ch-1.pl create mode 100644 challenge-152/abigail/perl/ch-2.pl create mode 100644 challenge-152/abigail/python/ch-1.py create mode 100644 challenge-152/abigail/python/ch-2.py create mode 100644 challenge-152/abigail/ruby/ch-1.rb create mode 100644 challenge-152/abigail/ruby/ch-2.rb create mode 100644 challenge-152/abigail/t/ctest.ini create mode 100644 challenge-152/abigail/t/input-1-1 create mode 100644 challenge-152/abigail/t/input-2-1 create mode 100644 challenge-152/abigail/t/input-2-2 create mode 100644 challenge-152/abigail/t/output-1-1.exp create mode 100644 challenge-152/abigail/t/output-2-1.exp create mode 100644 challenge-152/abigail/t/output-2-2.exp (limited to 'challenge-152') diff --git a/challenge-152/abigail/README.md b/challenge-152/abigail/README.md index 7c068e4d4a..9b1fd5d762 100644 --- a/challenge-152/abigail/README.md +++ b/challenge-152/abigail/README.md @@ -16,15 +16,9 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [bc](bc/ch-2.bc) * [C](c/ch-2.c) -* [Go](go/ch-2.go) -* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) -* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) -* [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) -* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-152/abigail/awk/ch-1.awk b/challenge-152/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..76ebeb1aff --- /dev/null +++ b/challenge-152/abigail/awk/ch-1.awk @@ -0,0 +1,30 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + minsum = $1 + + min = 0 + n = 2 + m = n + for (i = 2; i <= NF; i ++) { + if (m == n || $i < min) { + min = $i + } + if (-- m == 0) { + minsum += min + m = ++ n + min = 0 + } + } + + print minsum +} + diff --git a/challenge-152/abigail/awk/ch-2.awk b/challenge-152/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..9af83cb5a7 --- /dev/null +++ b/challenge-152/abigail/awk/ch-2.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function min (a, b) {return a < b ? a : b} +function max (a, b) {return a < b ? b : a} + +{ + print ((max($1, $3) - min($1, $3)) * \ + (max($2, $4) - min($2, $4)) + \ + (max($5, $7) - min($5, $7)) * \ + (max($6, $8) - min($6, $8)) - \ + max(0, min(max($1, $3), max($5, $7)) - \ + max(min($1, $3), min($5, $7))) * \ + max(0, min(max($2, $4), max($6, $8)) - \ + max(min($2, $4), min($6, $8)))) +} diff --git a/challenge-152/abigail/bash/ch-1.sh b/challenge-152/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..941876c840 --- /dev/null +++ b/challenge-152/abigail/bash/ch-1.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read -a F +do ((minsum = 0)) + ((n = 1)) + ((m = n)) + ((min = 0)) + for num in ${F[@]} + do if ((n == m || num < min)) + then ((min = num)) + fi + + if ((-- m == 0)) + then ((minsum += min)) + ((m = ++ n)) + ((min = 0)) + fi + done + + echo $minsum + +done diff --git a/challenge-152/abigail/bash/ch-2.sh b/challenge-152/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..cdbc6b2d22 --- /dev/null +++ b/challenge-152/abigail/bash/ch-2.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +while read a_x1 a_y1 a_x2 a_y2 b_x1 b_y1 b_x2 b_y2 +do + ((a_x_min = a_x1 < a_x2 ? a_x1 : a_x2)) + ((a_x_max = a_x1 < a_x2 ? a_x2 : a_x1)) + ((a_y_min = a_y1 < a_y2 ? a_y1 : a_y2)) + ((a_y_max = a_y1 < a_y2 ? a_y2 : a_y1)) + ((b_x_min = b_x1 < b_x2 ? b_x1 : b_x2)) + ((b_x_max = b_x1 < b_x2 ? b_x2 : b_x1)) + ((b_y_min = b_y1 < b_y2 ? b_y1 : b_y2)) + ((b_y_max = b_y1 < b_y2 ? b_y2 : b_y1)) + + ((a_area = (a_x_max - a_x_min) * (a_y_max - a_y_min))) + ((b_area = (b_x_max - b_x_min) * (b_y_max - b_y_min))) + + ((x_overlap = (a_x_max < b_x_max ? a_x_max : b_x_max) - + (a_x_min < b_x_min ? b_x_min : a_x_min))) + ((y_overlap = (a_y_max < b_y_max ? a_y_max : b_y_max) - + (a_y_min < b_y_min ? b_y_min : a_y_min))) + + ((overlap = (x_overlap < 0 ? 0 : x_overlap) * + (y_overlap < 0 ? 0 : y_overlap))) + + echo $((a_area + b_area - overlap)) +done diff --git a/challenge-152/abigail/c/ch-1.c b/challenge-152/abigail/c/ch-1.c new file mode 100644 index 0000000000..cce0895298 --- /dev/null +++ b/challenge-152/abigail/c/ch-1.c @@ -0,0 +1,42 @@ +# include +# include +# include + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + int minsum = 0; + int n = 1; + int m = 1; + int num; + int skip; + int min = 0; + while (sscanf (line_ptr, "%d%n", &num, &skip) == 1) { + line_ptr += skip; + if (n == m || num < min) { + min = num; + } + if (!-- m) { + minsum += min; + m = ++ n; + min = 0; + } + } + printf ("%d\n", minsum); + } + free (line); + + return (0); +} diff --git a/challenge-152/abigail/c/ch-2.c b/challenge-152/abigail/c/ch-2.c new file mode 100644 index 0000000000..fab9dc2f1c --- /dev/null +++ b/challenge-152/abigail/c/ch-2.c @@ -0,0 +1,31 @@ +# include +# include +# include + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int min (int a, int b) {return a < b ? a : b;} +int max (int a, int b) {return a < b ? b : a;} + +int main (void) { + int a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2; + + while (scanf ("%d%d%d%d%d%d%d%d", &a_x1, &a_y1, &a_x2, &a_y2, + &b_x1, &b_y1, &b_x2, &b_y2) == 8) { + printf ("%d\n", (max (a_x1, a_x2) - min (a_x1, a_x2)) * + (max (a_y1, a_y2) - min (a_y1, a_y2)) + + (max (b_x1, b_x2) - min (b_x1, b_x2)) * + (max (b_y1, b_y2) - min (b_y1, b_y2)) - + max (0, min (max (a_x1, a_x2), max (b_x1, b_x2)) - + max (min (a_x1, a_x2), min (b_x1, b_x2))) * + max (0, min (max (a_y1, a_y2), max (b_y1, b_y2)) - + max (min (a_y1, a_y2), min (b_y1, b_y2)))); + } + return (0); +} diff --git a/challenge-152/abigail/lua/ch-1.lua b/challenge-152/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..eb0579ee47 --- /dev/null +++ b/challenge-152/abigail/lua/ch-1.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + local minsum = 0 + local n = 1 + local m = n + local min = 0 + for num in line : gmatch ("-?[0-9]+") do + num = tonumber (num) + if (m == n) or (num < min) then + min = num + end + m = m - 1 + if m == 0 then + minsum = minsum + min + n = n + 1 + m = n + min = 0 + end + end + print (minsum) +end diff --git a/challenge-152/abigail/lua/ch-2.lua b/challenge-152/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..d7a0f6f594 --- /dev/null +++ b/challenge-152/abigail/lua/ch-2.lua @@ -0,0 +1,29 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local i_pat = "(-?%d+)" +local pattern = i_pat +for i = 2, 8 do + pattern = pattern .. "%s+" .. i_pat +end + +for line in io . lines () do + local _, _, a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2 = + line : find (pattern) + print ( (math . max (a_x1, a_x2) - math . min (a_x1, a_x2)) * + (math . max (a_y1, a_y2) - math . min (a_y1, a_y2)) + + (math . max (b_x1, b_x2) - math . min (b_x1, b_x2)) * + (math . max (b_y1, b_y2) - math . min (b_y1, b_y2)) - +math . max (0, math . min (math . max (a_x1, a_x2), math . max (b_x1, b_x2)) - + math . max (math . min (a_x1, a_x2), math . min (b_x1, b_x2))) * +math . max (0, math . min (math . max (a_y1, a_y2), math . max (b_y1, b_y2)) - + math . max (math . min (a_y1, a_y2), math . min (b_y1, b_y2)))) + +end diff --git a/challenge-152/abigail/node/ch-1.js b/challenge-152/abigail/node/ch-1.js new file mode 100644 index 0000000000..2a2a41279f --- /dev/null +++ b/challenge-152/abigail/node/ch-1.js @@ -0,0 +1,21 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let numbers = line . trim () . split (/ +/) . map (n => +n) + let minsum = 0 + let n = 1 + while (numbers . length) { + minsum += Math . min (... numbers . splice (0, n ++)) + } + console . log (minsum) +}) diff --git a/challenge-152/abigail/node/ch-2.js b/challenge-152/abigail/node/ch-2.js new file mode 100644 index 0000000000..c8b72f8786 --- /dev/null +++ b/challenge-152/abigail/node/ch-2.js @@ -0,0 +1,25 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2] = + line . trim () . split (/ +/) . map (n => +n) + console . log ( (Math . max (a_x1, a_x2) - Math . min (a_x1, a_x2)) * + (Math . max (a_y1, a_y2) - Math . min (a_y1, a_y2)) + + (Math . max (b_x1, b_x2) - Math . min (b_x1, b_x2)) * + (Math . max (b_y1, b_y2) - Math . min (b_y1, b_y2)) - +Math . max (0, Math . min (Math . max (a_x1, a_x2), Math . max (b_x1, b_x2)) - + Math . max (Math . min (a_x1, a_x2), Math . min (b_x1, b_x2))) * +Math . max (0, Math . min (Math . max (a_y1, a_y2), Math . max (b_y1, b_y2)) - + Math . max (Math . min (a_y1, a_y2), Math . min (b_y1, b_y2)))) + +}) diff --git a/challenge-152/abigail/perl/ch-1.pl b/challenge-152/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..fe23c8957f --- /dev/null +++ b/challenge-152/abigail/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: perl ch-1.pl < input-file +# +# We expect the entire triangle to be given as whitespace separated +# numbers, all on one line. Give it's a triangle, we don't need to +# know where the next row starts. +# Each new line is considered a different triangle. +# + +# +# At first, I thought "path" was used in the traditional sense: +# we were to view the triangle as a graph, each node having two +# edges downwards to the nearest nodes on the next row. +# But it looks like we can just take any node on each row, and +# they will all connect. Which makes the exercise trivial: we +# just sum the minimum value from each row. +# + +use List::Util qw [min]; + +while (<>) { + my ($n, $min_sum, @numbers) = (1, 0, split); + $min_sum += min splice @numbers, 0, $n ++ while @numbers; + say $min_sum; +} diff --git a/challenge-152/abigail/perl/ch-2.pl b/challenge-152/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..346de9b235 --- /dev/null +++ b/challenge-152/abigail/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: perl ch-2.pl < input-file +# +# Each line of input is taken as a set of two rectangles, and consists +# of 8 numbers: first the corners of the first rectangle, than the +# corners of the second rectangle. +# +# We calculate the total area as the sum of the areas of each +# rectangle, minus the area of the overlap. +# The area of a rectangle is the product of the absolute difference of the +# x-coordinates and the absolute difference of the y-coordinates. +# The absolute difference of two numbers can be calculated by subtracting +# the minimum of the two from the maximum of the two. +# To calculate the area of the overlap, we multiply the overlap in the +# x-dimension, and the overlap in the y-dimension. To get the overlap +# in the x-dimension, we take the minimum of the max x-values of both +# rectangles, and subtract the maximim of the min x-values of both +# rectangles. If this value is less than 0, the overlap is 0. +# For the overlap in the y-dimension, we do the same with the y values. +# +# min (@F [0, 2]): Smallest x-value for first rectangle +# max (@F [0, 2]): Largest x-value for first rectangle +# min (@F [1, 3]): Smallest y-value for first rectangle +# max (@F [1, 3]): Largest y-value for first rectangle +# min (@F [4, 5]): Smallest x-value for second rectangle +# max (@F [4, 5]): Largest x-value for second rectangle +# min (@F [6, 7]): Smallest y-value for second rectangle +# max (@F [6, 7]): Largest y-value for second rectangle +# +# See https://math.stackexchange.com/questions/99565/ +# simplest-way-to-calculate-the-intersect-area-of-two-rectangles +# + +use List::Util qw [min max]; + +while (<>) {my @F = split; # Mimic -a + + say +(max (@F [0, 2]) - min (@F [0, 2])) * + (max (@F [1, 3]) - min (@F [1, 3])) + + (max (@F [4, 6]) - min (@F [4, 6])) * + (max (@F [5, 7]) - min (@F [5, 7])) - + max (0, min (max (@F [0, 2]), max (@F [4, 6])) - + max (min (@F [0, 2]), min (@F [4, 6]))) * + max (0, min (max (@F [1, 3]), max (@F [5, 7])) - + max (min (@F [1, 3]), min (@F [5, 7]))); + +} + +__END__ diff --git a/challenge-152/abigail/python/ch-1.py b/challenge-152/abigail/python/ch-1.py new file mode 100644 index 0000000000..c9062f3070 --- /dev/null +++ b/challenge-152/abigail/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + minsum = 0 + n = 1 + m = n + min = 0 + for num in line . strip () . split (" "): + num = int (num) + if n == m or num < min: + min = num + m = m - 1 + if m == 0: + n = n + 1 + m = n + minsum = minsum + min + min = 0 + print (minsum) diff --git a/challenge-152/abigail/python/ch-2.py b/challenge-152/abigail/python/ch-2.py new file mode 100644 index 0000000000..08c884caad --- /dev/null +++ b/challenge-152/abigail/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + [a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2] = \ + [int (x) for x in line . split ()] + print ( (max (a_x1, a_x2) - min (a_x1, a_x2)) * \ + (max (a_y1, a_y2) - min (a_y1, a_y2)) + \ + (max (b_x1, b_x2) - min (b_x1, b_x2)) * \ + (max (b_y1, b_y2) - min (b_y1, b_y2)) - \ + max (0, min (max (a_x1, a_x2), max (b_x1, b_x2)) - \ + max (min (a_x1, a_x2), min (b_x1, b_x2))) * \ + max (0, min (max (a_y1, a_y2), max (b_y1, b_y2)) - \ + max (min (a_y1, a_y2), min (b_y1, b_y2)))) diff --git a/challenge-152/abigail/ruby/ch-1.rb b/challenge-152/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..7e8132e448 --- /dev/null +++ b/challenge-152/abigail/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |line| + minsum = 0 + n = 1 + m = n + min = 0 + line . split . map do |num| + num = num . to_i + if n == m or num < min then + min = num + end + m -= 1 + if m == 0 then + minsum += min + n += 1 + m = n + min = 0 + end + end + puts (minsum) +end diff --git a/challenge-152/abigail/ruby/ch-2.rb b/challenge-152/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..6dea375d17 --- /dev/null +++ b/challenge-152/abigail/ruby/ch-2.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do |line| + a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2 = + line . split . map do |n| n . to_i end + + puts (([a_x1, a_x2] . max - [a_x1, a_x2] . min) * + ([a_y1, a_y2] . max - [a_y1, a_y2] . min) + + ([b_x1, b_x2] . max - [b_x1, b_x2] . min) * + ([b_y1, b_y2] . max - [b_y1, b_y2] . min) - + [[[a_x1, a_x2] . max, [b_x1, b_x2] . max] . min - + [[a_x1, a_x2] . min, [b_x1, b_x2] . min] . max, 0] . max * + [[[a_y1, a_y2] . max, [b_y1, b_y2] . max] . min - + [[a_y1, a_y2] . min, [b_y1, b_y2] . min] . max, 0] . max) + +end diff --git a/challenge-152/abigail/t/ctest.ini b/challenge-152/abigail/t/ctest.ini new file mode 100644 index 0000000000..b26e09b1d8 --- /dev/null +++ b/challenge-152/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +2-1 = Given Examples +2-2 = Swapped corners diff --git a/challenge-152/abigail/t/input-1-1 b/challenge-152/abigail/t/input-1-1 new file mode 100644 index 0000000000..7978c8edd8 --- /dev/null +++ b/challenge-152/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +1 5 3 2 3 4 7 1 0 2 6 4 5 2 8 +5 2 3 4 1 5 0 1 2 3 7 2 4 1 9 diff --git a/challenge-152/abigail/t/input-2-1 b/challenge-152/abigail/t/input-2-1 new file mode 100644 index 0000000000..a692cc7c35 --- /dev/null +++ b/challenge-152/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +-1 0 2 2 0 -1 4 4 +-3 -1 1 3 -1 -3 2 2 diff --git a/challenge-152/abigail/t/input-2-2 b/challenge-152/abigail/t/input-2-2 new file mode 100644 index 0000000000..018833f504 --- /dev/null +++ b/challenge-152/abigail/t/input-2-2 @@ -0,0 +1,2 @@ + 2 2 -1 0 0 -1 4 4 +-3 -1 1 3 2 2 -1 -3 diff --git a/challenge-152/abigail/t/output-1-1.exp b/challenge-152/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..512858e604 --- /dev/null +++ b/challenge-152/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +8 +9 diff --git a/challenge-152/abigail/t/output-2-1.exp b/challenge-152/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..e688fd9423 --- /dev/null +++ b/challenge-152/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +22 +25 diff --git a/challenge-152/abigail/t/output-2-2.exp b/challenge-152/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..e688fd9423 --- /dev/null +++ b/challenge-152/abigail/t/output-2-2.exp @@ -0,0 +1,2 @@ +22 +25 -- cgit From 2ee6e3d83f25f396c714f189690f617b6f919f7f Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 14 Feb 2022 14:54:50 -0600 Subject: Solve PWC152 --- challenge-152/wlmb/blog.txt | 1 + challenge-152/wlmb/perl/ch-1.pl | 27 +++++++++++++++++++ challenge-152/wlmb/perl/ch-2.pl | 60 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 challenge-152/wlmb/blog.txt create mode 100755 challenge-152/wlmb/perl/ch-1.pl create mode 100755 challenge-152/wlmb/perl/ch-2.pl (limited to 'challenge-152') diff --git a/challenge-152/wlmb/blog.txt b/challenge-152/wlmb/blog.txt new file mode 100644 index 0000000000..7e179b07fc --- /dev/null +++ b/challenge-152/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2022/02/14/PWC152/ diff --git a/challenge-152/wlmb/perl/ch-1.pl b/challenge-152/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..8cfb5aaaa3 --- /dev/null +++ b/challenge-152/wlmb/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 152 +# Task 1: Minimum sum path +# +# See https://wlmb.github.io/2022/02/14/PWC152/#task-1-minimum-sum-path +use v5.12; +use warnings; +use List::Util qw(min sum0 all); +use Try::Tiny; +die "Usage: ./ch-1.pl T1 [T2]...\n" + . "where Ti are triangles of the form '[[T00],[T10,T11],[T20,T21,T22]...'" + unless @ARGV; +for my $triangle_string (@ARGV){ + try { + my $triangle=eval $triangle_string; + my @rows=@$triangle; + # Seems unnecesary, but test shape + die "Wrong row-size in $triangle_string" + unless all{$_+1==@{$rows[$_]}}(0..@rows-1); + my @minima=map {min @$_} @rows; + my $sum=sum0 @minima; + say "Input: $triangle_string\nOutput: $sum\nPath values: ", join "-", @minima; + } + catch { + say $_; + } +} diff --git a/challenge-152/wlmb/perl/ch-2.pl b/challenge-152/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..4e9ca2e932 --- /dev/null +++ b/challenge-152/wlmb/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl +# Perl weekly challenge 152 +# Task 2: Rectangle area +# +# See https://wlmb.github.io/2022/02/14/PWC152/#task-2-rectangle-area +use v5.12; +use warnings; +use List::Util qw(min sum0 all); +die "Usage: ./ch-2.pl L1 B1 R1 T1 L2 B2 R2 T2 ..." + . "where L's B's R's and T's denote left, bottom, right and top coordinates" + unless @ARGV; +my @non_ol; +my @input; +my @pending; +while(@ARGV){ + die "# coordinates must be multiple of four" unless @ARGV>=3; + my ($L,$B,$R,$T)=splice @ARGV, 0, 4; + ($L, $R)=($R, $L) if $L>$R; # reorder if necessary + ($B, $T)=($T, $B) if $B>$T; + push @input, {left=>$L, bottom=>$B, right=>$R, top=>$T}; +} +my @pending=@input; + ADD1: + while(@pending){ + my $rectangle=shift @pending; + foreach(@non_ol){ + my @fragments=divide($rectangle, $_); + next ADD1 if @fragments==1; # rectangle contained in some other piece + push(@pending, @fragments), next ADD1 if @fragments>1; + + } + push @non_ol, $rectangle; +} +say "Input: "; +say "\tRectangle $_: ($input[$_]->{left},$input[$_]->{bottom}), ", + "($input[$_]->{right},$input[$_]->{top})" for(0..@input-1); +say "Area: ", sum0 map {($_->{right}-$_->{left})*($_->{top}-$_->{bottom})} @non_ol; +say "Non-overlapping subregions: "; +say "\tRectangle $_: ($non_ol[$_]->{left},$non_ol[$_]->{bottom}), ", + "($non_ol[$_]->{right},$non_ol[$_]->{top})" for(0..@non_ol-1); +sub divide { + my ($p, $q)=@_; + return if $p->{left}>=$q->{right} # no overlap + or $p->{right}<=$q->{left} + or $p->{bottom}>=$q->{top} + or $p->{top}<=$q->{bottom}; + return ({left=>$p->{left}, bottom=>$p->{bottom}, right=>$q->{left}, top=>$p->{top}}, + {left=>$q->{left}, bottom=>$p->{bottom}, right=>$p->{right}, top=>$p->{top}}) + if $p->{left}<$q->{left}; # split left + return ({left=>$p->{left}, bottom=>$p->{bottom}, right=>$q->{right}, top=>$p->{top}}, + {left=>$q->{right}, bottom=>$p->{bottom}, right=>$p->{right}, top=>$p->{top}}) + if $p->{right}>$q->{right}; # split right + return ({left=>$p->{left}, bottom=>$p->{bottom}, right=>$p->{right}, top=>$q->{bottom}}, + {left=>$p->{left}, bottom=>$q->{bottom}, right=>$p->{right}, top=>$p->{top}}) + if $p->{bottom}<$q->{bottom}; # split bottom + return ({left=>$p->{left}, bottom=>$p->{bottom}, right=>$p->{right}, top=>$q->{top}}, + {left=>$p->{left}, bottom=>$q->{top}, right=>$p->{right}, top=>$p->{top}}) + if $p->{top}>$q->{top}; # split top + return $p; # $p contained in $q +} -- cgit From d6766dc194b9c13a6bdaf34b51dfb29472be9eb7 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 15 Feb 2022 14:59:35 +0000 Subject: Update README.md --- challenge-152/james-smith/README.md | 59 +++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'challenge-152') diff --git a/challenge-152/james-smith/README.md b/challenge-152/james-smith/README.md index a128b2762a..09f8ad0cb4 100644 --- a/challenge-152/james-smith/README.md +++ b/challenge-152/james-smith/README.md @@ -18,35 +18,68 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-152/ja ***You are given a triangle array. Write a script to find the minimum sum path from top to bottom.*** +I'm going to outline two different solutions here - the first one is my first approach - which assumed that as +you went down the triangle you could only move to the next line either to the one adjacent to the left or the +right. The second solution removes this constraint and gives the answer in the question... + ## Solution a - can on move down and left or down and right -This doesn't match the output supplied (but feels right) +This doesn't match the output supplied (but feels right). Note we are careful here to make the code "non-destructive" - care has to be taken that we do not shift/modify data from the rows passed in as this will affect the underlying structure. So we note that the `shift` is only done on the `@p` array of totals/paths. + +We start by initalizing a blank row {below the triangle} we than work up the triangle one row at a time, the lowest value for a given cell is the value of the cell plus the lowest value of either the left or right cell below. In the code `$p[0]` is the left hand cell and `$p[1]` is the right hand cell. + +Each time through the loop we generate a new version of `@p` with the best route for each entry. We can (with care) use a `map` to achieve this. We loop through each entry in the incoming data and combine this with the data for the two entries below. If the left hand entry is lower than the right we add the information from the left hand entry to the total, and to the list of numbers chosen to get there. We then need to remove the first entry of `@p` - we can do this with `shift @p` but we don't want that in the resultant array - to "hide" it we multiple the new array `($p[-1])` by `0` which gives us no copies of the array... bang the value we didn't want is gone...! + ```perl sub min_path { my @p = map { [0,[]] } 0,my @t = reverse @{$_[0]}; - for my $r (@t) { - my($l,@q,$z) = shift @p; - ( push @q, $l->[0] < $p[0][0] - ? [ $_+$l->[0], [ $_, @{$l->[1]} ] ] - : [ $_+$p[0][0], [ $_, @{$p[0][1]} ] ] - ), $l = shift @p for @{$r}; - @p = @q; - } + @p = map { $p[0][0] < $p[1][0] + ? [ $_+$p[0][0], [ $_, @{$p[0][1]} ] ] + : [ $_+$p[1][0], [ $_, @{$p[1][1]} ] ] + , (shift @p) x 0 + } @{$_} for @t; + say sprintf 'Minimum value %d: [ %s ]', $p[0][0], join ', ', @{$p[0][1]}; $p[0][0]; } ``` +We can simplify this if we are not worried by the order - by storing a simple value (the minimum total for the path) rather than the pair total/path. + +```perl +sub min_path_total { + my @p = map { 0 } 0, my @t = reverse @{$_[0