aboutsummaryrefslogtreecommitdiff
path: root/challenge-028/steven-wilson/perl5/ch-2.pl
blob: 676243c6a38c1f0094e137b6ba1c81839778ade1 (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
#!/usr/bin/env perl
# Author: Steven Wilson
# Date: 2019-10-01
# Week: 028

# Task #2
# Write a script to display Digital Clock. Feel free to be as creative
# as you can when displaying digits. We expect bare minimum something
# like “14:10:11”.

# Usage: % perl ch-2.pl
# ouput:     _   _  _   _  _
#          |  |. _| _|. _||_|
#          |  |.|_ |_ . _| _|

use strict;
use warnings;
use feature qw/ say /;
use DateTime;

my $dt = DateTime->now;
my $hour   = $dt->hour;
my $minute = $dt->minute;
my $second = $dt->second;
my $digital = sprintf( "%02d:%02d:%02d", $hour, $minute, $second );
my @colon = ( " ", ".", "." );
my @digits;
$digits[0] = [ " _ ", "| |", "|_|" ];
$digits[1] = [ "   ", "  |", "  |" ];
$digits[2] = [ " _ ", " _|", "|_ " ];
$digits[3] = [ " _ ", " _|", " _|" ];
$digits[4] = [ "   ", "|_|", "  |" ];
$digits[5] = [ " _ ", "|_ ", " _|" ];
$digits[6] = [ " _ ", "|_ ", "|_|" ];
$digits[7] = [ " _ ", "  |", "  |" ];
$digits[8] = [ " _ ", "|_|", "|_|" ];
$digits[9] = [ " _ ", "|_|", " _|" ];

print_digital($digital);

sub print_digital {
    my $digital_string = shift;
    my @digit_to_print = split //, $digital_string;
    for ( my $i = 0; $i < 3; $i++ ) {
        for my $d (@digit_to_print) {
            if ( $d eq ":" ) {
                print $colon[$i];
            }
            else {
                print $digits[$d][$i];
            }
        }
        print "\n";
    }
}