aboutsummaryrefslogtreecommitdiff
path: root/challenge-281/bob-lied/perl/ch-1.pl
blob: 74dde20c5eda8194cbc3bc95ddb84dfda288b618 (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
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2024, Bob Lied
#=============================================================================
# ch-1.pl Perl Weekly Challenge 281 Task 1 Check Color
#=============================================================================
# You are given coordinates, a string that represents the coordinates of
# a square of the chessboard as shown below:
# Write a script to return true if the square is light, and false if
# the square is dark.
# Example 1 Input: $coordinates = "d3" Output: true
# Example 2 Input: $coordinates = "g5" Output: false
# Example 3 Input: $coordinates = "e6" Output: true
#=============================================================================

use v5.40;

use Getopt::Long;
my $Verbose = false;
my $DoTest  = false;
my $Benchmark = 0;

my %Board = (
    a => [ undef, false, true, false, true, false, true, false, true ],
    b => [ undef, true, false, true, false, true, false, true, false ],
    c => [ undef, false, true, false, true, false, true, false, true ],
    d => [ undef, true, false, true, false, true, false, true, false ],
    e => [ undef, false, true, false, true, false, true, false, true ],
    f => [ undef, true, false, true, false, true, false, true, false ],
    g => [ undef, false, true, false, true, false, true, false, true ],
    h => [ undef, true, false, true, false, true, false, true, false ],
);

GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
exit(!runTest()) if $DoTest;
exit( runBenchmark($Benchmark) ) if $Benchmark;

say checkColor($_) for @ARGV;

sub checkColor($s)
{
    return $Board{substr($s,0,1)}[substr($s,1)];
}

sub runTest
{
    use Test2::V0;

    is( checkColor("d3"), true,  "Example 1");
    is( checkColor("g5"), false, "Example 2");
    is( checkColor("e6"), true,  "Example 3");

    done_testing;
}

sub runBenchmark($repeat)
{
    use Benchmark qw/cmpthese/;

    cmpthese($repeat, {
            label => sub { },
        });
}