aboutsummaryrefslogtreecommitdiff
path: root/challenge-043
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-27 18:53:15 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-03-27 18:53:15 +0200
commit81d0737ace1c412703eb9b8d6f31883a63f0ead9 (patch)
tree5012c7343a1d8c7d0d70ed5425ac0122c0e5f217 /challenge-043
parent8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff)
downloadperlweeklychallenge-club-81d0737ace1c412703eb9b8d6f31883a63f0ead9.tar.gz
perlweeklychallenge-club-81d0737ace1c412703eb9b8d6f31883a63f0ead9.tar.bz2
perlweeklychallenge-club-81d0737ace1c412703eb9b8d6f31883a63f0ead9.zip
Challenges 043 044 LK Perl Python
Diffstat (limited to 'challenge-043')
-rw-r--r--challenge-043/lubos-kolouch/perl/ch-1.pl56
-rw-r--r--challenge-043/lubos-kolouch/perl/ch-2.pl25
-rw-r--r--challenge-043/lubos-kolouch/python/ch-1.py29
-rw-r--r--challenge-043/lubos-kolouch/python/ch-2.py18
4 files changed, 128 insertions, 0 deletions
diff --git a/challenge-043/lubos-kolouch/perl/ch-1.pl b/challenge-043/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..b30d576f86
--- /dev/null
+++ b/challenge-043/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+use Algorithm::Combinatorics qw(permutations);
+
+# Initialize the ring numbers and available numbers
+my %rings = (
+ Blue => 8,
+ Black => undef,
+ Red => 9,
+ Yellow => 7,
+ Green => 5,
+);
+my @available = ( 1, 2, 3, 4, 6 );
+
+# Generate all possible permutations of the available numbers
+my $iter = permutations( \@available );
+
+# Try each permutation for a valid solution
+my @solutions;
+while ( my $p = $iter->next ) {
+ my ( $b, $k, $r, $y, $g ) = @$p;
+
+ # Skip if any number is repeated
+ next if ( $b == $k || $b == $r || $b == $y || $b == $g );
+ next if ( $k == $r || $k == $y || $k == $g );
+ next if ( $r == $y || $r == $g );
+ next if ( $y == $g );
+
+ # Check if the sum of numbers in each ring is 11
+ if ( $b + $k + $r == 11 && $r + $y + $g == 11 ) {
+
+ # Save the solution
+ $rings{Blue} = $b;
+ $rings{Black} = $k;
+ $rings{Red} = $r;
+ $rings{Yellow} = $y;
+ $rings{Green} = $g;
+ push @solutions, {%rings};
+ }
+}
+
+# Print the solutions
+if (@solutions) {
+ print "Found ", scalar @solutions, " solutions:\n";
+ for my $i ( 0 .. $#solutions ) {
+ print "Solution ", $i + 1, ":\n";
+ print " Blue: ", $solutions[$i]{Blue}, "\n";
+ print " Black: ", $solutions[$i]{Black}, "\n";
+ print " Red: ", $solutions[$i]{Red}, "\n";
+ print " Yellow: ", $solutions[$i]{Yellow}, "\n";
+ print " Green: ", $solutions[$i]{Green}, "\n";
+ }
+}
+else {
+ print "No solutions found.\n";
+}
diff --git a/challenge-043/lubos-kolouch/perl/ch-2.pl b/challenge-043/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..00ff0a69f1
--- /dev/null
+++ b/challenge-043/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+
+sub self_descriptive {
+ my ($base) = @_;
+ my $n = $base;
+ while (1) {
+ if ( sd( $n, $base ) ) {
+ print "$n\n";
+ }
+ $n++;
+ }
+}
+
+sub sd {
+ my ( $n, $base ) = @_;
+ my @digits = split //, $n;
+ my @counts = (0) x $base;
+ for my $d (@digits) {
+ $counts[$d]++;
+ }
+ return join( '', @counts ) == $n;
+}
+
+self_descriptive(10);
diff --git a/challenge-043/lubos-kolouch/python/ch-1.py b/challenge-043/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..39d4aa8bb9
--- /dev/null
+++ b/challenge-043/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from itertools import permutations
+
+# Initialize the ring numbers and available numbers
+rings = {"Blue": 8, "Black": None, "Red": 9, "Yellow": 7, "Green": 5}
+available = [1, 2, 3, 4, 6]
+
+# Generate all possible permutations of the available numbers
+for b, k, r, y, g in permutations(available):
+ # Skip if any number is repeated
+ if b == k or b == r or b == y or b == g:
+ continue
+ if k == r or k == y or k == g:
+ continue
+ if r == y or r == g:
+ continue
+ if y == g:
+ continue
+ # Check if the sum of numbers in each ring is 11
+ if b + k + r == 11 and r + y + g == 11:
+ # Save the solution
+ rings["Blue"] = b
+ rings["Black"] = k
+ rings["Red"] = r
+ rings["Yellow"] = y
+ rings["Green"] = g
+ print(rings)
diff --git a/challenge-043/lubos-kolouch/python/ch-2.py b/challenge-043/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7faf8d6be1
--- /dev/null
+++ b/challenge-043/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,18 @@
+def self_descriptive(base):
+ def sd(n):
+ digits = [int(d) for d in str(n)]
+ counts = [0] * base
+ for d in digits:
+ counts[d] += 1
+ return int("".join(map(str, counts))) == n
+
+ n = base
+ while True:
+ if sd(n):
+ yield n
+ n += 1
+
+
+gen = self_descriptive(10)
+for _ in range(5):
+ print(next(gen))