aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-02-08 06:08:38 +0000
committerGitHub <noreply@github.com>2020-02-08 06:08:38 +0000
commit80bb58ec96e9a5469afcf4c0be1f4f6bbce57aa6 (patch)
tree6443331a5cc187c8509417e4f358ec665cee3c1c
parent90ba2a3e9be09c64ed20d58b0145668dafb22d11 (diff)
parent9e8b32d226c1dbb2e84ff58264d0763afb1ec881 (diff)
downloadperlweeklychallenge-club-80bb58ec96e9a5469afcf4c0be1f4f6bbce57aa6.tar.gz
perlweeklychallenge-club-80bb58ec96e9a5469afcf4c0be1f4f6bbce57aa6.tar.bz2
perlweeklychallenge-club-80bb58ec96e9a5469afcf4c0be1f4f6bbce57aa6.zip
Merge pull request #1222 from holli-holzer/master
Improved solution #2
-rw-r--r--challenge-046/markus-holzer/raku/ch-1.p62
-rw-r--r--challenge-046/markus-holzer/raku/ch-2.p632
2 files changed, 25 insertions, 9 deletions
diff --git a/challenge-046/markus-holzer/raku/ch-1.p6 b/challenge-046/markus-holzer/raku/ch-1.p6
index 7199005c6e..2cc9eca2ca 100644
--- a/challenge-046/markus-holzer/raku/ch-1.p6
+++ b/challenge-046/markus-holzer/raku/ch-1.p6
@@ -12,7 +12,7 @@ say decrypt( $message );
sub decrypt( $encrypted )
{
- join '', zip(
+ [~] zip(
$encrypted.lines.map({ .split(/ \s /) })
).map({
.Bag.first({ .value > 1 }).key
diff --git a/challenge-046/markus-holzer/raku/ch-2.p6 b/challenge-046/markus-holzer/raku/ch-2.p6
index 1fac6c7585..0923f3e2a5 100644
--- a/challenge-046/markus-holzer/raku/ch-2.p6
+++ b/challenge-046/markus-holzer/raku/ch-2.p6
@@ -1,12 +1,28 @@
-say "Open rooms: \n", (1..^500).grep({
- is-open( $_ )
-}).join(",");
+# Each door will only be visited by employees whos number is a divisor
+# of the room number. For example room number 12 will be visited
+# by the employees 1, 2, 3, 4, 6 and 12. That is 6 visits. As each
+# pair of visits cancels itself out, the only rooms that will be open
+# at the end are the ones with a number that has an ODD number of divisors.
+# From that we can already tell, doors with prime numbers will never be open
+# because a prime number always has only 2 divisors.
+#
+# Now divisors always come in pairs, in the case of the 12 these are
+# (1, 12), (2,6) and (3,4) as each of the pairs multiply out to 12.
+#
+# In the case of a square number however, there is always one pair for which both elements are the same.
+# 16 for example, has the divisor pairs are (1,16), (2, 8) and (4,4).
+# This last pair contains the same number twice.
+# And that is what makes the total number of divisors odd.
+# And then that is what tells us the door 16 will be open.
+#
+# Thus we can solve by
-sub is-open( $i )
+say "Open rooms: ", join ',', ( 1..500 ).grep: *.&is-open;
+
+sub is-open( $room )
{
- my $is-open = True;
- $is-open = !$is-open if $i %% $_ for 2 .. 500;
- $is-open;
+ my $sqrt = $room.sqrt;
+ $sqrt == $sqrt.Int
}
-# Open rooms: 1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484
+# Open rooms: 1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484 \ No newline at end of file