aboutsummaryrefslogtreecommitdiff
path: root/challenge-200
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2023-01-22 18:47:44 +0000
committerGitHub <noreply@github.com>2023-01-22 18:47:44 +0000
commit4cd1c92bf54b17c2af1044266e1779bb071e8828 (patch)
tree690b6904063691feb5ffa5ed9ea54383db9abc45 /challenge-200
parentdcea4c7ce34c7edc7d0940172e8ffa798f5a509e (diff)
downloadperlweeklychallenge-club-4cd1c92bf54b17c2af1044266e1779bb071e8828.tar.gz
perlweeklychallenge-club-4cd1c92bf54b17c2af1044266e1779bb071e8828.tar.bz2
perlweeklychallenge-club-4cd1c92bf54b17c2af1044266e1779bb071e8828.zip
Create ch-2.pl
Diffstat (limited to 'challenge-200')
-rw-r--r--challenge-200/james-smith/perl/ch-2.pl36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-200/james-smith/perl/ch-2.pl b/challenge-200/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..4edf36dff1
--- /dev/null
+++ b/challenge-200/james-smith/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/local/bin/perl
+
+use strict;
+use warnings;
+use feature qw(say);
+
+## Our provided truth table...
+my @truth = qw(abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg);
+my @blank = map { ' ' } 0..6;
+
+## A mapper for segment to what to draw... The rows are represented by pairs of bits 0->blank, 1->LHS, 2->RHS, 3->bar
+my %parts = ( 'a' => 0x0003, 'b' => 0x0028, 'c' => 0x0a00, 'd' => 0x3000, 'e' => 0x0500, 'f' => 0x0014, 'g' => 0x00c0, );
+## The four "pictures"!
+my @parts = ( ' ', '| ', ' |', ' -- ' );
+
+sub display {
+ ## Grab number & initialise output as empty.
+ my( $n, @out ) = ( shift, map {''} @blank );
+ while( $n ) {
+ ## Get the last digit & set the display for the digit to blank.
+ my( $d, @dig ) = ( $n%10, @blank );
+ ## Use our truth table along with part mapping to generate values for each number
+ for my $bar ( map { $parts{$_} } split //, $truth[$d] ) {
+ $dig[ $_ ] |= $parts[ $bar&3 ], $bar >>= 2 for 0 .. $#blank
+ }
+ ## Add to the display...
+ $out[ $_ ] = $dig[ $_ ].' '.$out[ $_ ] for 0..$#blank;
+ ## Update N...
+ $n = int( $n / 10 );
+ }
+ ## Render!
+ say for @out;
+}
+
+display(200);
+display(314159265358979);