From 0e91f637fdb6d6113982a78a64ae9dcd36134ce3 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 27 Nov 2022 20:32:27 +1100 Subject: Simon's solution to challenge 192 --- challenge-192/sgreen/README.md | 4 +-- challenge-192/sgreen/blog.txt | 1 + challenge-192/sgreen/perl/ch-1.pl | 17 +++++++++++++ challenge-192/sgreen/perl/ch-2.pl | 51 +++++++++++++++++++++++++++++++++++++ challenge-192/sgreen/python/ch-1.py | 19 ++++++++++++++ challenge-192/sgreen/python/ch-2.py | 48 ++++++++++++++++++++++++++++++++++ 6 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 challenge-192/sgreen/blog.txt create mode 100755 challenge-192/sgreen/perl/ch-1.pl create mode 100755 challenge-192/sgreen/perl/ch-2.pl create mode 100755 challenge-192/sgreen/python/ch-1.py create mode 100755 challenge-192/sgreen/python/ch-2.py diff --git a/challenge-192/sgreen/README.md b/challenge-192/sgreen/README.md index ef0288441e..4a7b1a13f0 100644 --- a/challenge-192/sgreen/README.md +++ b/challenge-192/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 191 +# The Weekly Challenge 192 -Blog [The cute recursive function](https://dev.to/simongreennet/the-cute-recursive-function-2gdo) +Blog [Weekly Challenge 192](https://dev.to/simongreennet/weekly-challenge-192-30a6) diff --git a/challenge-192/sgreen/blog.txt b/challenge-192/sgreen/blog.txt new file mode 100644 index 0000000000..79bc040803 --- /dev/null +++ b/challenge-192/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-192-30a6 \ No newline at end of file diff --git a/challenge-192/sgreen/perl/ch-1.pl b/challenge-192/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..bc4133e6ff --- /dev/null +++ b/challenge-192/sgreen/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($n) { + # Find the first power of two greater than n + my $p = 1; + $p *= 2 while ( $p <= $n ); + + # Print the inverse of n + say $p- $n - 1; +} + +main( $ARGV[0] ); diff --git a/challenge-192/sgreen/perl/ch-2.pl b/challenge-192/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..858e33a7e7 --- /dev/null +++ b/challenge-192/sgreen/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw'min sum'; + +sub main (@nums) { + # See if there is a possible target + my $target = sum(@nums) / scalar(@nums); + my $items = scalar(@nums); + my $moves = 0; + + if ( $target != int($target) ) { + # There is no possible solution + say '-1'; + return; + } + + foreach my $i ( 0 .. $#nums ) { + # How many do we need to make the value the target + my $needed = $target - $nums[$i]; + + while ( $needed > 0 ) { + # Loop from the left to find which items we can remove numbers from + foreach my $j ( 0 .. $#nums ) { + my $has = $nums[$j]; + + # We have found a value we can take from + if ( $has > $target ) { + # Calculate how many we can take + my $take = min( $needed, $has - $target ); + + $nums[$i] += $take; + $nums[$j] -= $take; + $needed -= $take; + + # Record the number of moves it would take to steal + # from the other item + $moves += $take * abs( $i - $j ); + } + } + } + } + + say $moves; +} + +main(@ARGV) diff --git a/challenge-192/sgreen/python/ch-1.py b/challenge-192/sgreen/python/ch-1.py new file mode 100755 index 0000000000..dc43fc664c --- /dev/null +++ b/challenge-192/sgreen/python/ch-1.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +import sys + + +def main(n): + '''Main function''' + + # Find the first power of two greater than n + p = 1 + while p <= n: + p *= 2 + + # Print the inverse of n + print(p-n-1) + + +if __name__ == '__main__': + main(int(sys.argv[1])) diff --git a/challenge-192/sgreen/python/ch-2.py b/challenge-192/sgreen/python/ch-2.py new file mode 100755 index 0000000000..7a06fb1764 --- /dev/null +++ b/challenge-192/sgreen/python/ch-2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +import sys + + +def main(s): + '''Main function''' + # Turn input into integers + nums = [int(x) for x in s] + + # See if there is a possible target + target = sum(nums) / len(nums) + items = len(nums) + moves = 0 + + if target % 1 != 0: + # There is no possible solution + print(-1) + return + + target = int(target) + for i in range(items): + # How many do we need to make the value the target + needed = target - nums[i] + + while needed > 0: + # Loop from the left to find which items we can remove numbers from + for j in range(items): + has = nums[j] + + # We have found a value we can take from + if has > target: + # Calculate how many we can take + take = min(needed, has - target) + + nums[i] += take + nums[j] -= take + needed -= take + + # Record the number of moves it would take to steal + # from the other item + moves += take * abs(i-j) + + print(moves) + + +if __name__ == '__main__': + main(sys.argv[1:]) -- cgit