aboutsummaryrefslogtreecommitdiff
path: root/challenge-151
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-02-13 17:56:21 +1100
committerSimon Green <mail@simon.green>2022-02-13 17:56:21 +1100
commit93424d2f86a103e895912599f00b2e4049555708 (patch)
treedf5653eca3d101bd0b2d1303b0be9cbe2b9a0747 /challenge-151
parentd4fec6b7157921fd3a1f1178f0899696c4d405d5 (diff)
downloadperlweeklychallenge-club-93424d2f86a103e895912599f00b2e4049555708.tar.gz
perlweeklychallenge-club-93424d2f86a103e895912599f00b2e4049555708.tar.bz2
perlweeklychallenge-club-93424d2f86a103e895912599f00b2e4049555708.zip
sgreen solutions to challenge 151
Diffstat (limited to 'challenge-151')
-rw-r--r--challenge-151/sgreen/README.md4
-rw-r--r--challenge-151/sgreen/blog.txt1
-rwxr-xr-xchallenge-151/sgreen/perl/ch-1.pl39
-rwxr-xr-xchallenge-151/sgreen/perl/ch-2.pl32
-rwxr-xr-xchallenge-151/sgreen/python/ch-1.py31
-rwxr-xr-xchallenge-151/sgreen/python/ch-2.py28
6 files changed, 133 insertions, 2 deletions
diff --git a/challenge-151/sgreen/README.md b/challenge-151/sgreen/README.md
index ea1bfbf44a..f6354625bc 100644
--- a/challenge-151/sgreen/README.md
+++ b/challenge-151/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 150
+# The Weekly Challenge 151
-Solution by Simon Green. [Weekly Challenge 150](https://dev.to/simongreennet/weekly-challenge-150-4fmn)
+Solution by Simon Green. [Weekly Challenge 151](https://dev.to/simongreennet/weekly-challenge-151-2812)
diff --git a/challenge-151/sgreen/blog.txt b/challenge-151/sgreen/blog.txt
new file mode 100644
index 0000000000..a06ea8184b
--- /dev/null
+++ b/challenge-151/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-151-2812 \ No newline at end of file
diff --git a/challenge-151/sgreen/perl/ch-1.pl b/challenge-151/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..95bdadad2d
--- /dev/null
+++ b/challenge-151/sgreen/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub strip {
+ my $s = shift;
+ $s =~ s/^\s*//;
+ $s =~ s/\s*$//;
+ return $s;
+}
+
+sub main {
+ # Split the input on pipes
+ my $s = shift;
+ my @rows = split( /\|/, $s );
+ my $min_depth = 1;
+
+ ROW: foreach my $row ( @rows[ 1 .. $#rows ] ) {
+ # Split the row, and expand if missing values
+ my @v = split( /\s+/, strip($row) );
+ while ( @v < 2**$min_depth ) {
+ push @v, '*';
+ }
+
+ # Go through each pair, and exit if we find two '*'
+ for ( my $i = 0 ; $i < $#v ; $i += 2 ) {
+ if ( $v[$i] eq '*' and $v[ $i + 1 ] eq '*' ) {
+ last ROW;
+ }
+ }
+ $min_depth++;
+ }
+
+ say $min_depth;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-151/sgreen/perl/ch-2.pl b/challenge-151/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..53c08143c3
--- /dev/null
+++ b/challenge-151/sgreen/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use List::Util 'max';
+
+sub rob {
+ my ( $haul, $valuables ) = @_;
+ if ( @{$valuables} <= 2 ) {
+ # We rob the remaining house, and take off with the haul!
+ return $haul + $valuables->[0];
+ }
+
+ # Call the function recursively skipping either one or two houses
+ my @hauls = ();
+ push @hauls, rob( $haul + $valuables->[0], [ @{$valuables}[ 2 .. $#$valuables ] ] );
+ if ( @{$valuables} >= 4 ) {
+ push @hauls, rob( $haul + $valuables->[0], [ @{$valuables}[ 3 .. $#$valuables ] ] );
+ }
+
+ # Return the largest haul
+ return max(@hauls);
+}
+
+sub main {
+ my @valuables = @_;
+ my $largest_haul = rob( 0, \@valuables );
+ say $largest_haul;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-151/sgreen/python/ch-1.py b/challenge-151/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..7c14e1f791
--- /dev/null
+++ b/challenge-151/sgreen/python/ch-1.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(s):
+ # Split the input on pipes
+ rows = s.split('|')
+ min_depth = 1
+
+ for row in rows[1:]:
+ # Split the row, and expand if missing values
+ v = row.strip().split()
+ while len(v) < 2 ** min_depth:
+ v.append('*')
+
+ # Go through each pair, and exit if we find two '*'
+ for i in range(0, len(v), 2):
+ if v[i] == '*' and v[i+1] == '*':
+ break
+ else:
+ min_depth += 1
+ continue
+
+ break
+
+ print(min_depth)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])
diff --git a/challenge-151/sgreen/python/ch-2.py b/challenge-151/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..094f071037
--- /dev/null
+++ b/challenge-151/sgreen/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def rob(haul, valuables):
+ if len(valuables) <= 2:
+ # We rob the remaining house, and take off with the haul!
+ return haul + valuables[0]
+
+ # Call the function recursively skipping either one or two houses
+ hauls = []
+ hauls.append(rob(haul+valuables[0], valuables[2:]))
+ if len(valuables) >= 4:
+ hauls.append(rob(haul+valuables[0], valuables[3:]))
+
+ # Return the largest haul
+ return max(hauls)
+
+
+def main(inputs):
+ valuables = list(map(int, inputs))
+ largest_haul = rob(0, valuables)
+ print(largest_haul)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])