aboutsummaryrefslogtreecommitdiff
path: root/challenge-200/athanasius/perl/ch-2.pl
blob: 277fd2c4c337275d8bd16511820281d5c0db14a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!perl

###############################################################################
=comment

Perl Weekly Challenge 200
=========================

TASK #2
-------
*Seven Segment 200*

Submitted by: Ryan J Thompson

A seven segment display is an electronic component, usually used to display
digits. The segments are labeled 'a' through 'g' as shown:

    __a__
   |     |
  f|     |b
   |__g__|
   |     |
  e|     |c
   |__d__|


The encoding of each digit can thus be represented compactly as a truth table:

  my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>;

For example, $truth[1] = ‘bc’. The digit 1 would have segments ‘b’ and ‘c’
enabled.

Write a program that accepts any decimal number and draws that number as a
horizontal sequence of ASCII seven segment displays, similar to the following:

  -------  -------  -------
        |  |     |  |     |
        |  |     |  |     |
  -------
  |        |     |  |     |
  |        |     |  |     |
  -------  -------  -------

To qualify as a seven segment display, each segment must be drawn (or not
drawn) according to your @truth table.

The number "200" was of course chosen to celebrate our 200th week!

=cut
###############################################################################

#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#

#==============================================================================
=comment

Assumption
----------
A "decimal" number is a non-negative integer.

=cut
#==============================================================================

use strict;
use warnings;
use Const::Fast;
use Regexp::Common qw( number );

const my $SPACE            => ' ';
const my $HORIZONTAL_BAR   => '-';
const my $VERTICAL_BAR     => '|';
const my $SEVEN_SEG_HEIGHT =>  7;
const my $SEVEN_SEG_WIDTH  =>  7;
const my $SEPARATOR_WIDTH  =>  2;
const my $DIGIT_WIDTH      => $SEVEN_SEG_WIDTH + $SEPARATOR_WIDTH;
const my $SCREEN_WIDTH     => 80;
const my $MAX_LINE_WIDTH   => $SCREEN_WIDTH - ($SCREEN_WIDTH % $DIGIT_WIDTH);
const my @TRUTH_TABLE      =>
         qw( abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg );
const my $USAGE            =>
"Usage:
  perl $0 <decimal>

    <decimal>    A non-negative integer\n";

#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 200, Task #2: Seven Segment 200 (Perl)\n\n";
}

#==============================================================================
MAIN:
#==============================================================================
{
    my  $args = scalar @ARGV;

    if ($args == 1)
    {
        my $decimal =  $ARGV[ 0 ];
           $decimal =~ / ^ $RE{num}{int} $ /x
                or error( qq["$_" is not a valid integer] );

        print  "Input: $decimal\n";
        printf "Output:\n\n%s", draw_number( $decimal );
    }
    else
    {
        error( "Expected 1 argument, found $args" );
    }
}

#------------------------------------------------------------------------------
sub draw_number
#------------------------------------------------------------------------------
{
    my ($decimal) = @_;

    # 1. Draw the display

    my   @lines;
    push @lines, draw_horizontal( $decimal, 'a'      );
    push @lines, draw_vertical  ( $decimal, 'f', 'b' );
    push @lines, draw_horizontal( $decimal, 'g'      );
    push @lines, draw_vertical  ( $decimal, 'e', 'c' );
    push @lines, draw_horizontal( $decimal, 'd'      );

    # 2. Now split long display lines to fit the screen width

    my $display;

    do
    {
        $display .= substr( $lines[ $_ ], 0, $MAX_LINE_WIDTH, '' ) . "\n"
            for 0 .. $#lines;

        $display .= "\n";

    } while (length $lines[ 0 ] > 0);

    chomp  $display;

    return $display;
}

#------------------------------------------------------------------------------
sub draw_horizontal
#------------------------------------------------------------------------------
{
    my ($decimal, $seg) = @_;
    my  $line;

    for my $digit (split //, $decimal)
    {
        my $code =  $TRUTH_TABLE[ $digit ];
        my $char = ($code =~ / $seg /x) ? $HORIZONTAL_BAR : $SPACE;

        $line .= $SPACE x $SEPARATOR_WIDTH . $char x $SEVEN_SEG_WIDTH;
    }

    return $line;
}

#------------------------------------------------------------------------------
sub draw_vertical
#------------------------------------------------------------------------------
{
    my ($decimal, $l_seg, $r_seg) = @_;
    my  $line;

    for my $digit (split //, $decimal)
    {
        my $code   =  $TRUTH_TABLE[ $digit ];
        my $l_char = ($code =~ / $l_seg /x) ? $VERTICAL_BAR : $SPACE;
        my $r_char = ($code =~ / $r_seg /x) ? $VERTICAL_BAR : $SPACE;

        $line .= $SPACE x  $SEPARATOR_WIDTH      . $l_char .
                 $SPACE x ($SEVEN_SEG_WIDTH - 2) . $r_char;
    }

    return ($line, $line);
}

#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

###############################################################################