diff options
| author | bagheera-sands <git@sandsscouts.org.uk> | 2019-10-14 12:13:41 +0100 |
|---|---|---|
| committer | bagheera-sands <git@sandsscouts.org.uk> | 2019-10-14 12:13:41 +0100 |
| commit | 8772eb01ec4177b81a1e394eb9a278297bb5328e (patch) | |
| tree | 1dd353e3227f3330eece555074e679d74f7df39b /challenge-030 | |
| parent | 5a7333b54780e76bfa5682236ad79fc91fa05998 (diff) | |
| download | perlweeklychallenge-club-8772eb01ec4177b81a1e394eb9a278297bb5328e.tar.gz perlweeklychallenge-club-8772eb01ec4177b81a1e394eb9a278297bb5328e.tar.bz2 perlweeklychallenge-club-8772eb01ec4177b81a1e394eb9a278297bb5328e.zip | |
challenge 30 solutions - one liners for each of these - golf style! - 131 bytes total for the shortest pair of solutions
Diffstat (limited to 'challenge-030')
| -rw-r--r-- | challenge-030/james-smith/README | 1 | ||||
| -rw-r--r-- | challenge-030/james-smith/README.md | 45 | ||||
| -rwxr-xr-x | challenge-030/james-smith/perl5/ch1-duplicates.sh | 1 | ||||
| -rwxr-xr-x | challenge-030/james-smith/perl5/ch1.sh | 1 | ||||
| -rwxr-xr-x | challenge-030/james-smith/perl5/ch2-better.sh | 1 | ||||
| -rwxr-xr-x | challenge-030/james-smith/perl5/ch2.sh | 1 |
6 files changed, 49 insertions, 1 deletions
diff --git a/challenge-030/james-smith/README b/challenge-030/james-smith/README deleted file mode 100644 index 3b736748ea..0000000000 --- a/challenge-030/james-smith/README +++ /dev/null @@ -1 +0,0 @@ -Solutions by James Smith. diff --git a/challenge-030/james-smith/README.md b/challenge-030/james-smith/README.md new file mode 100644 index 0000000000..b94c4c9cb8 --- /dev/null +++ b/challenge-030/james-smith/README.md @@ -0,0 +1,45 @@ +Solutions by James Smith. + +Sorry these are such nice golf questions they had to be golfed.. + +# Challenge 1 - sum to 12 + +I've included two solutions to this as the question was slighlty ambiguous + +The first solution is for a strictly increasing sequence of numbers {no dupes} +``` +perl -E 'say map{$a=$_;map{join","," $a",$_,12-$a-$_}$a+1..5.5-$a/2}1..3' +``` + +The second is for a series of sequences which can allow duplicates +``` +perl -E 'say map{$a=$_;map{join","," $a",$_,12-$a-$_}$a..6-$a/2}1..4' +``` + +In both cases we have used shortest loops - possible... + +# Challenge 2 - Christmas on a Sunday? + +Solving the actual problem - we need to work out which days have 5*year/4 mod 7 to be zero, so just use a simple grep, to keep the code sort we use "@{[ ]}" which at 7 bytes is one byte shorter than join" ", + +``` +perl -E 'say"@{[grep{!(int(5*$_/4)%7)}2019..2099]}"' +``` + +We can extend this to every year (since the Gregorian calendar was introduced) by subtracting +int year/100 & adding int year/400... + +``` +perl -E 'say"@{[grep{6==(int(5*$_/4)-int($_/100)+int$_/400)%7}2019..3e3]}"' +``` + +## Lengths of scripts: +All four scripts come in at: 269 bytes - the shortest two solutions come in at 131 bytes +``` +| script | Bytes | +| --- | ---: | +| ch1.sh | 73 | +| ch1-duplicates.sh | 69 | +| ch2.sh | 52 | +| ch2-better.sh | 75 | +``` diff --git a/challenge-030/james-smith/perl5/ch1-duplicates.sh b/challenge-030/james-smith/perl5/ch1-duplicates.sh new file mode 100755 index 0000000000..52182deae0 --- /dev/null +++ b/challenge-030/james-smith/perl5/ch1-duplicates.sh @@ -0,0 +1 @@ +perl -E'say map{$a=$_;map{join","," $a",$_,12-$a-$_}$a..6-$a/2}1..4' diff --git a/challenge-030/james-smith/perl5/ch1.sh b/challenge-030/james-smith/perl5/ch1.sh new file mode 100755 index 0000000000..e39bd383d4 --- /dev/null +++ b/challenge-030/james-smith/perl5/ch1.sh @@ -0,0 +1 @@ +perl -E'say map{$a=$_;map{join","," $a",$_,12-$a-$_}$a+1..5.5-$a/2}1..3' diff --git a/challenge-030/james-smith/perl5/ch2-better.sh b/challenge-030/james-smith/perl5/ch2-better.sh new file mode 100755 index 0000000000..aef3dac8d2 --- /dev/null +++ b/challenge-030/james-smith/perl5/ch2-better.sh @@ -0,0 +1 @@ +perl -E'say"@{[grep{6==(int(5*$_/4)-int($_/100)+int$_/400)%7}2019..3e3]}"' diff --git a/challenge-030/james-smith/perl5/ch2.sh b/challenge-030/james-smith/perl5/ch2.sh new file mode 100755 index 0000000000..334483a152 --- /dev/null +++ b/challenge-030/james-smith/perl5/ch2.sh @@ -0,0 +1 @@ +perl -E'say"@{[grep{!(int(5*$_/4)%7)}2019..2099]}"' |
