diff options
| author | Flavio Poletti <flavio@polettix.it> | 2023-10-07 19:41:05 +0200 |
|---|---|---|
| committer | Flavio Poletti <flavio@polettix.it> | 2023-10-07 19:41:05 +0200 |
| commit | 4aa31fa327730169f8d556000cc0fb21ea561fe4 (patch) | |
| tree | 97ff3ea21a589c0e2700e74d3bc88e40329ea51e | |
| parent | 61e2a29a7e3c0100cbf1560dab538a9fb8e78a66 (diff) | |
| download | perlweeklychallenge-club-4aa31fa327730169f8d556000cc0fb21ea561fe4.tar.gz perlweeklychallenge-club-4aa31fa327730169f8d556000cc0fb21ea561fe4.tar.bz2 perlweeklychallenge-club-4aa31fa327730169f8d556000cc0fb21ea561fe4.zip | |
Add polettix's solution to challenge-237
| -rw-r--r-- | challenge-237/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-237/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-237/polettix/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-237/polettix/perl/ch-2.pl | 52 | ||||
| -rw-r--r-- | challenge-237/polettix/raku/ch-1.raku | 16 | ||||
| -rw-r--r-- | challenge-237/polettix/raku/ch-2.raku | 18 |
6 files changed, 116 insertions, 0 deletions
diff --git a/challenge-237/polettix/blog.txt b/challenge-237/polettix/blog.txt new file mode 100644 index 0000000000..7d2d364e04 --- /dev/null +++ b/challenge-237/polettix/blog.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/10/07/pwc237-seize-the-day/ diff --git a/challenge-237/polettix/blog1.txt b/challenge-237/polettix/blog1.txt new file mode 100644 index 0000000000..aa39d88799 --- /dev/null +++ b/challenge-237/polettix/blog1.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/10/08/pwc237-maximise-greatness/ diff --git a/challenge-237/polettix/perl/ch-1.pl b/challenge-237/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..b04096977a --- /dev/null +++ b/challenge-237/polettix/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +use Time::Local 'timegm_modern'; + +say seize_the_day(@ARGV); + +sub seize_the_day ($year, $month, $weekday_in_month, $weekday) { + my $date = timegm_modern(0, 0, 12, 1, $month - 1, $year); + my $date_day_of_week = (gmtime($date))[6]; + + # how many days should we advance to find the first occurrence of the + # target $weekday? + my $delta = ($weekday + 7 - $date_day_of_week) % 7; + + # advance that many days to land on the first, then additional weeks + # to land on the target $weekday_in_month. + $date += ($delta + ($weekday_in_month - 1) * 7) * 24 * 3600; + + # get back the year and month of the date we landed on + my (undef, undef, undef, $day, $newm, $newy) = gmtime($date); + $newm += 1; # apply offset for month + $newy += 1900; # apply offset for year + + # return making sure we're in the same year & month + return $year == $newy && $month == $newm ? $day : 0; +} diff --git a/challenge-237/polettix/perl/ch-2.pl b/challenge-237/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..0e8d9b37f3 --- /dev/null +++ b/challenge-237/polettix/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +use JSON::PP; + +my ($greatness, $permutation) = great_permutation(@ARGV); +say $greatness, ' -> ', JSON::PP->new->encode($permutation); + +say greatness(@ARGV); + +sub great_permutation (@inputs) { + my @sorted_indexes = sort { $inputs[$b] <=> $inputs[$a] } 0 .. $#inputs; + + my @permutation = (undef) x @inputs; + + # first pass - set greatness! + my @pool; + my @not_great; + for my $index (@sorted_indexes) { + my $value = $inputs[$index]; + if (@pool && $pool[0] > $value) { + $permutation[$index] = shift @pool; + } + else { + push @not_great, $index; + } + push @pool, $value; + } + + my $greatness = scalar(@inputs) - scalar(@pool); + + # second pass - fill the rest + @permutation[@not_great] = @pool; + + return ($greatness, \@permutation); +} + +sub greatness (@inputs) { + my %count_for; + $count_for{$_}++ for @inputs; + my @counts = @count_for{sort { $a <=> $b } keys %count_for}; + + my $greatness = @inputs; + my $pool = 0; + for my $count (@counts) { + next if $count <= $pool; # win & accumulate the same quantity + $greatness -= $count - $pool; # not enough in pool, lose some + $pool = $count; # restart pool from this slot + } + return $greatness; +} diff --git a/challenge-237/polettix/raku/ch-1.raku b/challenge-237/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..eb759862d1 --- /dev/null +++ b/challenge-237/polettix/raku/ch-1.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (Int $year, Int $month, Int $weekday_in_month, Int $weekday) { + my $date = Date.new(year => $year, month => $month, day => 1); + + # how much should we advance to find the first occurrence of the + # target $weekday? + my $delta = ($weekday + 7 - $date.day-of-week) % 7; + + # advance that much to land on the first, then additional weeks to + # land on the target $weekday_in_month + $date += $delta + ($weekday_in_month - 1) * 7; + + # print the result making sure it's still in the same year & month + put($date.year == $year && $date.month == $month ?? $date.day !! 0); +} diff --git a/challenge-237/polettix/raku/ch-2.raku b/challenge-237/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..dc576bcaf5 --- /dev/null +++ b/challenge-237/polettix/raku/ch-2.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@args) { put greatness(@args) } + +sub greatness (@inputs) { + my %count-for; + %count-for{$_}++ for @inputs; + my @counts = %count-for{%count-for.keys.sort({$^a.Int <=> $^b.Int})}; + + my $greatness = @inputs.elems; + my $pool = 0; + for @counts -> $count { + next if $count <= $pool; # win and accumulate the same quantity + $greatness -= $count - $pool; # not enough in pool, lose some + $pool = $count; # restart pool from this slot + } + return $greatness; +} |
