Last active
April 10, 2019 18:44
-
-
Save unclechu/b38ce477a274aef0f95f729a8cb6c9d3 to your computer and use it in GitHub Desktop.
277777788888899.pl6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use v6; | |
my Int \magic = 277777788888899; | |
my Str \separator = "\n" ~ "~" x 60 ~ "\n"; | |
my Int \cores = try { $*KERNEL.cpu-cores } // 1; | |
('CPU cores: ' ~ cores).say; | |
class CalcResult { | |
has @.steps is required; | |
has $.result is required; | |
method Str { | |
(($_+1) ~ ": @!steps[$_]" for ^@!steps).join("\n") | |
~ "\nResult: $!result\nTaken steps: " ~ @!steps.elems; | |
} | |
} | |
multi steps(Int $n --> CalcResult) { steps [], $n } | |
multi steps(@steps, Int $n --> CalcResult) { | |
@steps.push: $n; | |
my \digits = $n.split('').grep({$_ ne ''}).map(*.Int); | |
my \result = digits.reduce(&[*]); | |
return steps @steps, result if result > 9; | |
CalcResult.new(steps => @steps, result => result) | |
} | |
sub handle-result(Int $n) { | |
my CalcResult \r = steps $n; | |
next if r.steps.elems <= 10; | |
r.Str.say; | |
separator.say; | |
} | |
say(); | |
{ | |
"*** ! Magic number first ! ***\n".say; | |
my CalcResult \r = steps magic; | |
r.Str.say; | |
separator.say; | |
} | |
# single thread solution | |
#`[ | |
loop (my $i = 1; $i <= (9 xx 20).join('').Int; ++$i) {handle-result $i} | |
] | |
my $last_ts = now; | |
sub do_a_digit(Int $digit) { | |
my \ts = now; | |
my $diff = ts - $last_ts; | |
"*** | Digit $digit goes (time diff: $diff)... | ***\n".say; | |
$last_ts = ts; | |
my \base = 10 ** $digit; | |
my \intervals = ((base * $_, (base * ($_+1)) - 1) for 1..9); | |
# await Supply.from-list(intervals).throttle(cores, -> (Int $from, Int $to) { | |
# loop (my $i = $from; $i <= $to; ++$i) {handle-result $i} | |
# }) | |
await intervals | |
.hyper(:batch(1), :degree(cores)) | |
.map(-> (Int $from, Int $to) { | |
loop (my $i = $from; $i <= $to; ++$i) {handle-result $i} | |
}); | |
} | |
do_a_digit($_) for 1..20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment