diff options
| -rw-r--r-- | challenge-286/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-286/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/elixir/ch-1.exs | 7 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/elixir/ch-2.exs | 50 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/perl/ch-1.pl | 11 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/perl/ch-2.pl | 49 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/python/ch-1.py | 9 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/python/ch-2.py | 49 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/raku/ch-1.raku | 6 | ||||
| -rwxr-xr-x | challenge-286/packy-anderson/raku/ch-2.raku | 47 |
10 files changed, 230 insertions, 1 deletions
diff --git a/challenge-286/packy-anderson/README.md b/challenge-286/packy-anderson/README.md index af883912e0..6669e4dd9b 100644 --- a/challenge-286/packy-anderson/README.md +++ b/challenge-286/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Perl Weekly Challenge: Exact Change Only!](https://packy.dardan.com/b/Qq) +[Perl Weekly Challenge: Spammer Game](https://packy.dardan.com/b/R9) diff --git a/challenge-286/packy-anderson/blog.txt b/challenge-286/packy-anderson/blog.txt new file mode 100644 index 0000000000..ceed0fee92 --- /dev/null +++ b/challenge-286/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/R9
\ No newline at end of file diff --git a/challenge-286/packy-anderson/elixir/ch-1.exs b/challenge-286/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..9090b2923b --- /dev/null +++ b/challenge-286/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,7 @@ +#!/usr/bin/env elixir + +{:ok, contents} = File.read(__ENV__.file) +word = contents + |> String.split(~r{\s+}, trim: true) + |> Enum.random +IO.puts(word) diff --git a/challenge-286/packy-anderson/elixir/ch-2.exs b/challenge-286/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..338a5bc419 --- /dev/null +++ b/challenge-286/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,50 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def orderGame(ints, explain, loop) when length(ints) == 1 do + { List.first(ints), Enum.join(explain, "\n") } + end + + def orderGame(ints, explain, loop) do + pairs = Enum.chunk_every(ints, 2) + explain = explain ++ [ "Operation #{loop}:", "" ] + acc = %{ flip: 1, explain: explain } + { newints, acc } = Enum.map_reduce(pairs, acc, fn x, acc -> + a = List.first(x) + b = List.last(x) + { val, func } = if acc[:flip] == 1 do # min + { min(a, b), "min" } + else # max + { max(a, b), "max" } + end + { + val, + acc + |> Map.put(:explain, acc[:explain] ++ + [ " #{func}(#{a}, #{b}) = #{val}" ]) + |> Map.put(:flip, acc[:flip] * -1) + } + end) + acc = acc |> Map.put(:explain, acc[:explain] ++ [ "" ]) + orderGame(newints, acc[:explain], loop+1) + end + + def orderGame(ints) do + orderGame(ints, [], 1) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + {output, explain} = orderGame(ints) + IO.puts("Output: #{output}\n\n#{explain}") + end +end + +IO.puts("Example 1:") +PWC.solution([2, 1, 4, 5, 6, 3, 0, 2]) + +IO.puts("\nExample 2:") +PWC.solution([0, 5, 3, 2]) + +IO.puts("\nExample 3:") +PWC.solution([9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8]) diff --git a/challenge-286/packy-anderson/perl/ch-1.pl b/challenge-286/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..04868612db --- /dev/null +++ b/challenge-286/packy-anderson/perl/ch-1.pl @@ -0,0 +1,11 @@ +#!/usr/bin/env perl +use v5.40; + +my @words; +open my $fh, '<', __FILE__; +while (my $line = <$fh>) { + push @words, grep { length($_) } split /\s+/, $line; +} +close $fh; + +say $words[ int(rand($#words + 1)) ]; diff --git a/challenge-286/packy-anderson/perl/ch-2.pl b/challenge-286/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..7cb592a897 --- /dev/null +++ b/challenge-286/packy-anderson/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( min max ); + +sub orderGame(@ints) { + my $loop = 0; + my @explain; + while (@ints > 1) { + my @new; + $loop++; + push @explain, "Operation $loop:"; + push @explain, ""; + my $flip = 1; + foreach my($a, $b) ( @ints ) { + my ($val, $func); + if ($flip == 1) { + $val = min($a, $b); + $func = 'min'; + } + else { # $flip is -1 + $val = max($a, $b); + $func = 'max'; + } + push @new, $val; + push @explain, " $func($a, $b) = $val"; + $flip *= -1; # switch from min to max and back + } + push @explain, ""; + @ints = @new; # replace list with new list + } + return $ints[0], join("\n", @explain); +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + my ($output, $explain) = orderGame(@$ints); + say 'Output: ' . $output; + say "\n$explain"; +} + +say "Example 1:"; +solution([2, 1, 4, 5, 6, 3, 0, 2]); + +say "\nExample 2:"; +solution([0, 5, 3, 2]); + +say "\nExample 3:"; +solution([9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8]); diff --git a/challenge-286/packy-anderson/python/ch-1.py b/challenge-286/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..bfe31a7b8b --- /dev/null +++ b/challenge-286/packy-anderson/python/ch-1.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +from random import randrange + +words = [] +with open(__file__) as fh: + words.extend(fh.read().split()) + +print(f'{ words[ randrange( len(words) ) ] }')
\ No newline at end of file diff --git a/challenge-286/packy-anderson/python/ch-2.py b/challenge-286/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..6047131b92 --- /dev/null +++ b/challenge-286/packy-anderson/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +def pairwise(iterable): + # pairwise('ABCDEFGH') → AB CD EF GH + iterator = iter(iterable) + a = next(iterator, None) + for b in iterator: + yield a, b + a = next(iterator, None) + +def orderGame(ints): + loop = 0 + explain = [] + while (len(ints) > 1): + new = [] + loop += 1 + explain.append(f"Operation {loop}:") + explain.append("") + flip = 1 + for a, b in pairwise(ints): + if (flip == 1): + val = min(a, b) + func = 'min' + else: # $flip is -1 + val = max(a, b) + func = 'max' + new.append(val) + explain.append(f" {func}({a}, {b}) = {val}") + flip *= -1; # switch from min to max and back + explain.append("") + ints = new # replace list with new list + return ints[0], "\n".join(explain) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + output, explain = orderGame(ints) + print(f'Output: {output}\n\n{explain}') + +print('Example 1:') +solution([2, 1, 4, 5, 6, 3, 0, 2]) + +print('\nExample 2:') +solution([0, 5, 3, 2]) + +print('\nExample 3:') +solution([9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8]) diff --git a/challenge-286/packy-anderson/raku/ch-1.raku b/challenge-286/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..6605c9fef5 --- /dev/null +++ b/challenge-286/packy-anderson/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku +use v6; + +my @words = $?FILE.IO.slurp.split(/\s+/, :skip-empty).Slip; + +say @words[ (0 .. @words.elems).rand.Int ]; diff --git a/challenge-286/packy-anderson/raku/ch-2.raku b/challenge-286/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..e1f6395ee9 --- /dev/null +++ b/challenge-286/packy-anderson/raku/ch-2.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku +use v6; + +sub orderGame(@ints is copy) { + my $loop = 0; + my @explain; + while (@ints.elems > 1) { + my @new; + $loop++; + @explain.push("Operation $loop:"); + @explain.push(""); + my $flip = 1; + for @ints -> $a, $b { + my ($val, $func); + if ($flip == 1) { + $val = min($a, $b); + $func = 'min'; + } + else { # $flip is -1 + $val = max($a, $b); + $func = 'max'; + } + @new.push($val); + @explain.push(" $func" ~ "($a, $b) = $val"); + $flip *= -1; # switch from min to max and back + } + @explain.push(""); + @ints = @new; # replace list with new list + } + return @ints[0], @explain.join("\n"); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my ($output, $explain) = orderGame(@ints); + say 'Output: ' ~ $output; + say "\n$explain"; +} + +say "Example 1:"; +solution([2, 1, 4, 5, 6, 3, 0, 2]); + +say "\nExample 2:"; +solution([0, 5, 3, 2]); + +say "\nExample 3:"; +solution([9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8]); |
