From 5fa1257b05397b05cdb77788a017347f38835d62 Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Thu, 27 May 2021 07:26:46 +0200 Subject: Add polettix's solution to challenge-114 --- challenge-114/polettix/blog.txt | 1 + challenge-114/polettix/blog1.txt | 1 + challenge-114/polettix/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-114/polettix/perl/ch-2.pl | 17 +++++++++++++++++ challenge-114/polettix/raku/ch-1.raku | 28 ++++++++++++++++++++++++++++ challenge-114/polettix/raku/ch-2.raku | 16 ++++++++++++++++ 6 files changed, 92 insertions(+) create mode 100644 challenge-114/polettix/blog.txt create mode 100644 challenge-114/polettix/blog1.txt create mode 100644 challenge-114/polettix/perl/ch-1.pl create mode 100644 challenge-114/polettix/perl/ch-2.pl create mode 100644 challenge-114/polettix/raku/ch-1.raku create mode 100644 challenge-114/polettix/raku/ch-2.raku diff --git a/challenge-114/polettix/blog.txt b/challenge-114/polettix/blog.txt new file mode 100644 index 0000000000..b5e34e5793 --- /dev/null +++ b/challenge-114/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/05/26/pwc114-next-palindrome-number/ diff --git a/challenge-114/polettix/blog1.txt b/challenge-114/polettix/blog1.txt new file mode 100644 index 0000000000..e52bd77988 --- /dev/null +++ b/challenge-114/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/05/27/pwc114-higher-integer-set-bits/ diff --git a/challenge-114/polettix/perl/ch-1.pl b/challenge-114/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..9329e4ba94 --- /dev/null +++ b/challenge-114/polettix/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub next_palindrome_number ($N) { + my $l = length $N; + return '1' . ('0' x ($l - 1)) . '1' unless $N =~ m{[0-8]}mxs; + my $n = substr $N, 0, $l / 2; + my $mid = $l % 2 ? substr($N, $l / 2, 1) : ''; + + # just try to build straight from the inputs... + if ((my $candidate = $n . $mid . reverse($n)) > $N) { + return $candidate; + } + + # if there's a "$mid", try increasing that + if ($l % 2) { + return $n . ($mid + 1) . reverse($n) if $mid != 9; + $mid = 0; + } + + ++$n; + return $n . $mid . reverse($n); +} + +@ARGV = (1234) unless @ARGV; +say next_palindrome_number($_) for @ARGV; diff --git a/challenge-114/polettix/perl/ch-2.pl b/challenge-114/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..07c7d74298 --- /dev/null +++ b/challenge-114/polettix/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub higher_integer_set_bits ($N) { + sub n_bits ($x) { sprintf('%b', $x) =~ tr/1/1/ }; + my $initial = n_bits($N); + while ('necessary') { + ++$N; + return $N if $initial == n_bits($N); + } +} + +@ARGV = 3 unless @ARGV; +say higher_integer_set_bits($_) for @ARGV; diff --git a/challenge-114/polettix/raku/ch-1.raku b/challenge-114/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..23a678aa2e --- /dev/null +++ b/challenge-114/polettix/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub next-palindrome-number (IntStr $N) { + my $l = $N.chars; + return '1' ~ ('0' x ($l - 1)) ~ '1' unless $N ~~ m{<[0 .. 8]>}; + my $n = $N.substr(0, $l / 2); + my $mid = $l % 2 ?? $N.substr($l / 2, 1) !! ''; + + # just try to build straight from the inputs... + if (my $candidate = $n ~ $mid ~ $n.flip) > $N { + return $candidate; + } + + # if there's a "$mid", try increasing that + if ($l % 2) { + return $n ~ ($mid + 1) ~ $n.flip if $mid < 9; + $mid = 0; + } + + ++$n; + return $n ~ $mid ~ $n.flip; +} + +sub MAIN (*@inputs is copy) { + @inputs.push(1234) unless @inputs.elems; + next-palindrome-number($_).say for @inputs; +} diff --git a/challenge-114/polettix/raku/ch-2.raku b/challenge-114/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..a02224e675 --- /dev/null +++ b/challenge-114/polettix/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +use v6; + +sub higher-integer-set-bits (Int $N is copy) { + sub n-bits ($x) { ($x.base(2) ~~ m:g/1/).elems }; + my $initial = n-bits($N); + while True { + ++$N; + return $N if $initial == n-bits($N); + } +} + +sub MAIN (*@inputs is copy) { + @inputs.push(3) unless @inputs.elems; + higher-integer-set-bits($_).say for @inputs; +} -- cgit