aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-219/sgreen/README.md4
-rw-r--r--challenge-219/sgreen/blog.txt1
-rwxr-xr-xchallenge-219/sgreen/perl/ch-1.pl13
-rwxr-xr-xchallenge-219/sgreen/perl/ch-2.pl44
-rwxr-xr-xchallenge-219/sgreen/python/ch-1.py13
-rwxr-xr-xchallenge-219/sgreen/python/ch-2.py37
6 files changed, 110 insertions, 2 deletions
diff --git a/challenge-219/sgreen/README.md b/challenge-219/sgreen/README.md
index 393dc9b36a..b0c3afab92 100644
--- a/challenge-219/sgreen/README.md
+++ b/challenge-219/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 218
+# The Weekly Challenge 219
-Blog: [The one about maximums](https://dev.to/simongreennet/the-one-about-maximums-40cm)
+Blog: [The cheapest way to the square](https://dev.to/simongreennet/the-cheapest-way-to-the-square-1mhb)
diff --git a/challenge-219/sgreen/blog.txt b/challenge-219/sgreen/blog.txt
new file mode 100644
index 0000000000..a586d603aa
--- /dev/null
+++ b/challenge-219/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-cheapest-way-to-the-square-1mhb \ No newline at end of file
diff --git a/challenge-219/sgreen/perl/ch-1.pl b/challenge-219/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..b6d438a88f
--- /dev/null
+++ b/challenge-219/sgreen/perl/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@array) {
+ @array = sort { $a <=> $b } map { $_**2 } @array;
+ say join ', ', @array;
+}
+
+main(@ARGV);
diff --git a/challenge-219/sgreen/perl/ch-2.pl b/challenge-219/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..cd81a701b6
--- /dev/null
+++ b/challenge-219/sgreen/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub buy_pass ( $passes, $travel_on ) {
+ if ( scalar(@$travel_on) == 0 ) {
+ return 0;
+ }
+
+ my $first_day = $travel_on->[0];
+ my $minimum_spend = undef;
+
+ foreach my $days ( keys %$passes ) {
+ my $cost = $passes->{$days};
+ my $spend =
+ $cost +
+ buy_pass( $passes,
+ [ grep { $_ >= $first_day + $days } @$travel_on ] );
+ if ( not defined($minimum_spend) or $spend < $minimum_spend ) {
+ $minimum_spend = $spend;
+ }
+ }
+
+ return $minimum_spend;
+}
+
+sub main (@array) {
+ # Take the first three values off
+ my %passes = ();
+ $passes{1} = shift @array;
+ $passes{7} = shift @array;
+ $passes{30} = shift @array;
+
+ # Note the days we want to travel on
+ my @travel_on = sort { $a <=> $b } @array;
+ my $spent = buy_pass( \%passes, \@travel_on );
+ say $spent;
+}
+
+main(@ARGV);
+
diff --git a/challenge-219/sgreen/python/ch-1.py b/challenge-219/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..f3e6fb57f9
--- /dev/null
+++ b/challenge-219/sgreen/python/ch-1.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(array):
+ solution = sorted([x ** 2 for x in array])
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)
diff --git a/challenge-219/sgreen/python/ch-2.py b/challenge-219/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..a421ef00d9
--- /dev/null
+++ b/challenge-219/sgreen/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def buy_pass(passes, travel_on):
+ if len(travel_on) == 0:
+ return 0
+
+ first_day = travel_on[0]
+ minimum_spend = None
+
+ for days, cost in passes.items():
+ spend = cost + \
+ buy_pass(passes, [i for i in travel_on if i >= first_day + days])
+ if minimum_spend is None or spend < minimum_spend:
+ minimum_spend = spend
+
+ return minimum_spend
+
+
+def main(array):
+ # Take the first three values off
+ passes = {}
+ passes[1] = array.pop(0)
+ passes[7] = array.pop(0)
+ passes[30] = array.pop(0)
+
+ # Note the days we want to travel on
+ travel_on = sorted(array)
+ spent = buy_pass(passes, travel_on)
+ print(spent)
+
+
+if __name__ == '__main__':
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)