diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-08-18 15:07:49 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-08-18 15:07:49 -0400 |
| commit | 710837b2a38b5fbd4404f8d9be06ef368af0d366 (patch) | |
| tree | 8796e6c6c7be4f7f935e68f3f0bff99bed04eb57 /challenge-126/stuart-little | |
| parent | 637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3 (diff) | |
| parent | 73470591f10490d6385145990c49825266f80b2b (diff) | |
| download | perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.gz perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.bz2 perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-126/stuart-little')
| -rwxr-xr-x | challenge-126/stuart-little/haskell/ch-1.hs | 18 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/haskell/ch-2.hs | 25 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/lua/ch-1.lua | 11 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/lua/ch-2.lua | 43 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/node/ch-1.js | 11 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/node/ch-2.js | 22 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/python/ch-1.py | 14 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/python/ch-2.py | 19 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/raku/ch-1.raku | 12 | ||||
| -rwxr-xr-x | challenge-126/stuart-little/raku/ch-2.raku | 21 |
12 files changed, 243 insertions, 0 deletions
diff --git a/challenge-126/stuart-little/haskell/ch-1.hs b/challenge-126/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..3ad9b0fed8 --- /dev/null +++ b/challenge-126/stuart-little/haskell/ch-1.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env runghc + +-- run <script> <number> + +import Data.Char (digitToInt) +import System.Environment (getArgs) + +no1 :: String -> Integer +no1 nr + |length nr == 0 = 0 + |head nr == '1' = 9^(length nr -1) + |otherwise = ((toInteger $ digitToInt $ head nr) - 1) * 9^(length nr -1) + (no1 $ tail nr) + +adjNo1 :: String -> Integer +adjNo1 nr = if (not $ elem '1' nr) then (no1 nr) else (no1 nr - 1) + +main :: IO () +main = getArgs >>= putStrLn.show.adjNo1.head diff --git a/challenge-126/stuart-little/haskell/ch-2.hs b/challenge-126/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..115b56e33c --- /dev/null +++ b/challenge-126/stuart-little/haskell/ch-2.hs @@ -0,0 +1,25 @@ +#!/usr/bin/env runghc + +-- run <script> + +{-# LANGUAGE QuasiQuotes #-} + +import System.Environment (getArgs) +import Text.RawString.QQ + +nbrs :: [[a]] -> Int -> Int -> [(Int,Int)] +nbrs mat i j = filter (\p-> (fst p /= i || snd p /= j) && (0 <= fst p) && (fst p < length mat) && (0 <= snd p) && (snd p < length (head mat))) $ map (\[x,y]->(i+x,j+y)) $ sequence $ replicate 2 [-1..1] + +mines :: [[String]] -> [[String]] +mines xs = map (map (\(p,q,r)-> if r == "x" then "x" else show $ length $ filter (\nbr-> (xs !! (fst nbr)) !! (snd nbr) == "x") $ nbrs xs p q)) indexed where + indexed = map (\(i,ar)-> map (\(j,x) -> (i,j,x)) $ zip [0..] ar) $ zip [0..] xs + +inptStr :: String +inptStr=[r|x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x|] + +main :: IO () +main = putStrLn $ unlines $ map unwords $ mines $ map words $ lines inptStr diff --git a/challenge-126/stuart-little/lua/ch-1.lua b/challenge-126/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..031196b56e --- /dev/null +++ b/challenge-126/stuart-little/lua/ch-1.lua @@ -0,0 +1,11 @@ +#!/usr/bin/env lua + +-- run <script> <number> + +function no1(nr) + if (nr:len() == 0) then return 0 end + if (nr:sub(1,1) == '1') then return 9^(nr:len()-1) end + return (tonumber(nr:sub(0,1))-1) * 9^(nr:len()-1) + no1(nr:sub(2)) +end + +print((not arg[1]:find('1')) and math.floor(no1(arg[1])) or math.floor(no1(arg[1])-1)) diff --git a/challenge-126/stuart-little/lua/ch-2.lua b/challenge-126/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..bdbfdf1451 --- /dev/null +++ b/challenge-126/stuart-little/lua/ch-2.lua @@ -0,0 +1,43 @@ +#!/usr/bin/env lua + +-- run <script> + +function nbrs(mat,i,j) + local t={} + for _,x in ipairs({-1,0,1}) do + for _,y in ipairs({-1,0,1}) do + if ((x ~= 0 or y ~= 0) and 1 <= i+x and i+x <= #mat and 1 <= j+y and j+y <= #(mat[1])) then table.insert(t,{i+x,j+y}) end + end + end + return t +end + +local inptStr=[[x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +]] + +local inpt={} +for ln in inptStr:gmatch("([^\n]+)") do + local lnTab = {} + for chr in ln:gmatch("([^%s]+)") do table.insert(lnTab,chr) end + table.insert(inpt,lnTab) +end + +for i=1,#inpt do + local ln="" + for j=1,#(inpt[1]) do + if inpt[i][j] == 'x' then + ln = ln .. 'x' .. ' ' + else + local nr=0 + for _,v in ipairs(nbrs(inpt,i,j)) do + if inpt[v[1]][v[2]] == 'x' then nr = nr + 1 end + end + ln = ln .. tostring(nr) .. " " + end + end + print(ln) +end diff --git a/challenge-126/stuart-little/node/ch-1.js b/challenge-126/stuart-little/node/ch-1.js new file mode 100755 index 0000000000..c54750afee --- /dev/null +++ b/challenge-126/stuart-little/node/ch-1.js @@ -0,0 +1,11 @@ +#!/usr/bin/env node + +// run <script> <number> + +function no1(nr){ + if (nr.length === 0) { return 0 } + if (nr[0] === '1') { return 9**(nr.length-1) } + return (parseInt(nr[0])-1) * 9**(nr.length-1) + no1(nr.slice(1)) +} + +console.log((process.argv[2].indexOf('1') === -1) ? (no1(process.argv[2])) : (no1(process.argv[2])-1)) diff --git a/challenge-126/stuart-little/node/ch-2.js b/challenge-126/stuart-little/node/ch-2.js new file mode 100755 index 0000000000..1475a66d99 --- /dev/null +++ b/challenge-126/stuart-little/node/ch-2.js @@ -0,0 +1,22 @@ +#!/usr/bin/env node + +// run <script> + +function nbrs(mat,i,j) { + return [-1,0,1].map(x => [-1,0,1].map(y => [i+x,j+y])).flat().filter(p => p[0] !== i || p[1] !== j).filter(p => 0<=p[0] && p[0] < mat.length).filter(p => 0<=p[1] && p[1] < mat[0].length) +} + +inpt=`x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +`.split(/\n/).map(s => s.split(/\s+/)).filter(x => x[0].length>0) + +for (let i=0; i<inpt.length; i++) { + let str="" + for (let j=0; j<inpt[0].length; j++) { + str += (((inpt[i][j] === 'x') ? ('x') : (nbrs(inpt,i,j).filter(p => inpt[p[0]][p[1]] === 'x').length)) + " ") + } + console.log(str) +} diff --git a/challenge-126/stuart-little/perl/ch-1.pl b/challenge-126/stuart-little/perl/ch-1.pl new file mode 100755 index 0000000000..c421696dcc --- /dev/null +++ b/challenge-126/stuart-little/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use warnings; +use v5.12; + +# run <script> <number> + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +sub no1($nr) { + length($nr)==0 && return 0; + (substr($nr,0,1) eq '1') && return 9**(length($nr)-1); + return (int(substr($nr,0,1))-1) * 9**(length($nr)-1) + no1(substr($nr,1)); +} + +say(($ARGV[0] !~ m/1/) ? (no1($ARGV[0])) : (no1($ARGV[0])-1)) diff --git a/challenge-126/stuart-little/perl/ch-2.pl b/challenge-126/stuart-little/perl/ch-2.pl new file mode 100755 index 0000000000..86a3d4d6f2 --- /dev/null +++ b/challenge-126/stuart-little/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +use warnings; +use v5.12; + +# run <script> + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +sub nbrs($mat, $i, $j) { + my @pairs = grep {$_->[0] >= 0 && $_->[0] < scalar @{$mat} && $_->[1] >= 0 && $_->[1] < scalar @{$mat->[0]} && ($_->[0] != $i || $_->[1] != $j)} + map {my $co = $_; my @a = map {[$i+$_,$j+$co]} (-1..1); @a} + (-1..1); + return \@pairs; +} + +my @in = grep {scalar @{$_}} map {my @a = split /\s+/, $_; \@a} <DATA>; + +for my $i (0..(scalar @in)-1) { + for my $j (0..(scalar @{$in[0]})-1) { + print( (($in[$i]->[$j] eq 'x') ? ($in[$i]->[$j]) : (scalar grep { $in[$_->[0]]->[$_->[1]] eq 'x' } @{nbrs(\@in,$i,$j)})) . " "); + } + print("\n"); +} + +__DATA__ +x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x diff --git a/challenge-126/stuart-little/python/ch-1.py b/challenge-126/stuart-little/python/ch-1.py new file mode 100755 index 0000000000..aea89f2b33 --- /dev/null +++ b/challenge-126/stuart-little/python/ch-1.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +# run <script> <number> + +import sys + +def no1(nr): + if len(nr)==0: + return 0 + if nr[0] == '1': + return 9**(len(nr)-1) + return (int(nr[0])-1) * 9**(len(nr)-1) + no1(nr[1:]) + +print(no1(sys.argv[1]) if '1' not in sys.argv[1] else (no1(sys.argv[1])-1)) diff --git a/challenge-126/stuart-little/python/ch-2.py b/challenge-126/stuart-little/python/ch-2.py new file mode 100755 index 0000000000..971e35a639 --- /dev/null +++ b/challenge-126/stuart-little/python/ch-2.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +# run <script> + +def nbrs(mat, i, j): + return [(i+x,j+y) for x in range(-1,2) for y in range(-1,2) if (x != 0 or y != 0) and (0 <= i+x < len(mat)) and (0 <= j+y < len(mat[0]))] + +inptStr="""x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +""" +inpt = [s.split() for s in inptStr.splitlines()] + +for i in range(len(inpt)): + for j in range(len(inpt[0])): + print(inpt[i][j] if (inpt[i][j] == 'x') else len(list(filter(lambda p: inpt[p[0]][p[1]] == 'x', nbrs(inpt,i,j)))), end=" ") + print() diff --git a/challenge-126/stuart-little/raku/ch-1.raku b/challenge-126/stuart-little/raku/ch-1.raku new file mode 100755 index 0000000000..b219539164 --- /dev/null +++ b/challenge-126/stuart-little/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use v6; + +# run <script> <number> + +sub no1($nr) { + $nr.chars==0 && return 0; + ($nr.substr(0,1) eq '1') && return 9**($nr.chars-1); + return ($nr.substr(0,1).Int-1) * 9**($nr.chars-1) + no1($nr.substr(1)); +} + +say((@*ARGS[0] !~~ m/1/) ?? (no1(@*ARGS[0])) !! (no1(@*ARGS[0])-1)) diff --git a/challenge-126/stuart-little/raku/ch-2.raku b/challenge-126/stuart-little/raku/ch-2.raku new file mode 100755 index 0000000000..0acd479e12 --- /dev/null +++ b/challenge-126/stuart-little/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku +use v6; + +# run <script> + +sub nbrs(@mat, $i, $j) { + return ((-1..1) X (-1..1)).map({ [$i+$_.[0], $j+$_.[1]] }).grep({ 0 <= $_.[0] < @mat.elems && 0 <= $_.[1] < @mat[0].elems && ($_.[0] != $i || $_.[1] != $j) }).Array +} +my @in = $=finish.lines.map({ $_.split(/\s+/).Array }); + +for ((0..^@in.elems) X (0..^@in[0].elems)) -> ($i, $j) { + print( ((@in[$i][$j] eq 'x') ?? (@in[$i][$j]) !! (nbrs(@in,$i,$j).grep({ @in[$_[0]][$_[1]] eq 'x' }).elems)) ~ " " ); + ($j == @in[0].elems-1) && print("\n"); +} + +=finish +x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x |
