diff options
| author | Abigail <abigail@abigail.be> | 2021-01-26 19:22:57 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-26 19:22:57 +0100 |
| commit | 18f3e7ff84691edc8c9d8241f72005c0e6cb8f7e (patch) | |
| tree | 742e1992f7db2f276c522c62dc29e2a4c844fdc9 /challenge-097 | |
| parent | f4e861bc27167c67ebb9bbda9e03884778cdd525 (diff) | |
| download | perlweeklychallenge-club-18f3e7ff84691edc8c9d8241f72005c0e6cb8f7e.tar.gz perlweeklychallenge-club-18f3e7ff84691edc8c9d8241f72005c0e6cb8f7e.tar.bz2 perlweeklychallenge-club-18f3e7ff84691edc8c9d8241f72005c0e6cb8f7e.zip | |
Perl solution for week 97, part 2
Diffstat (limited to 'challenge-097')
| -rw-r--r-- | challenge-097/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-097/abigail/perl/ch-2.pl | 42 |
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 0e2295286a..1761fd8622 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -66,5 +66,6 @@ Binary Substrings: ~~~~ ### Solutions +* [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-097/abigail/perl/ch-2.pl b/challenge-097/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..b0d145069e --- /dev/null +++ b/challenge-097/abigail/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl -s SECTIONS < input-file +# + +use Getopt::Long; +use List::Util qw [min]; + +GetOptions 's=i' => \my $sections; + +die "-s option required" unless defined $sections && $sections > 0; + +# +# To get the minimum number of flips, for each position take the +# minimum of the number of 0s and the number of 1s, and sum over +# all the positions +# + +while (<>) { + my $sum = 0; + chomp; + my @chunks = /.{$sections}/g; + for (my $i = 0; $i < length $chunks [0]; $i ++) { + my $zeros = grep {substr ($_, $i, 1) eq "0"} @chunks; + $sum += min $zeros, @chunks - $zeros; + } + say $sum; +} |
