aboutsummaryrefslogtreecommitdiff
path: root/challenge-037/javier-luque/perl6/ch-2.p6
blob: 996aae9ccf47ae167d8f04e0b7a3023534b66c02 (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
# Test: perl6 ch2.p6
use v6.d;

# Box configurations
sub MAIN () {
    my $date1 = Date.new(2019,11,1);
    my $date2 = Date.new(2019,12,1);
    compare_lengths($date1, $date2);
}

sub compare_lengths(Date $date1, Date $date2) {
    # Months
    my @mon = (
        'Jan', 'Feb', 'Mar', 'Apr',
        'May', 'Jun', 'Jul', 'Aug',
        'Sep', 'Oct', 'Nov', 'Dec'
    );

    # Get the data from the web
    my @date1_data =
        get-data($date1);
    my @date2_data =
        get-data($date2);

    # Calculate totals
    my $date1_total = calculate-daylight-total(@date1_data);
    my $date2_total = calculate-daylight-total(@date2_data);
    my $difference = $date1_total - $date2_total;

    # Print the output
    say @mon[$date1.month - 1] ~ ' ' ~ $date1.year ~ ' has ' ~
        convert-seconds-to-string($date1_total) ~
        ' of daylight.';

    # Print the output
    say @mon[$date2.month - 1] ~ ' ' ~ $date2.year ~ ' has ' ~
        convert-seconds-to-string($date2_total) ~
        ' of daylight.';

    say "The difference is: " ~
        convert-seconds-to-string($difference) ~ '.';
}

# Calculates the total daylight hours from data
sub calculate-daylight-total(@data) {
    my $total = 0;
    for (@data) -> $daylight {
        $total += convert-time-to-seconds($daylight).Int;
    }
    return $total
}

sub convert-seconds-to-string (Int $seconds) {
    return ( $seconds.polymod(60, 60, 24) Z
             ('seconds', 'minutes', 'hours', 'days')
             ).reverse.join(", ");
}

# Convert hh::mm::ss to seconds
sub convert-time-to-seconds(Str $time_string) {
    my ($hh, $mm, $ss) = $time_string.split(':');
    return $hh * 3600 + $mm * 60 + 60;
}

# A bit of a cheat, I built the Scraper
# in perl5 so don't reinvent the wheel
# Gets the daytime date
sub get-data(Date $date) {
    my %data = (
        '2019-11-01' => (
            '9:40:44', '9:37:10', '9:33:37', '9:30:07',
            '9:26:38', '9:23:11', '9:19:45', '9:16:22',
            '9:13:01', '9:09:42', '9:06:25', '9:03:11',
            '8:59:59', '8:56:50', '8:53:44', '8:50:40',
            '8:47:39', '8:44:42', '8:41:48', '8:38:57',
            '8:36:09', '8:33:25', '8:30:45', '8:28:09',
            '8:25:36', '8:23:08', '8:20:44', '8:18:24',
            '8:16:09', '8:13:59'
        ),
        '2019-12-01' => (
            '8:11:53', '8:09:53', '8:07:57', '8:06:07',
            '8:04:22', '8:02:42', '8:01:08', '7:59:40',
            '7:58:17', '7:57:00', '7:55:50', '7:54:45',
            '7:53:46', '7:52:54', '7:52:07', '7:51:27',
            '7:50:54', '7:50:27', '7:50:06', '7:49:52',
            '7:49:44', '7:49:43', '7:49:48', '7:50:00',
            '7:50:19', '7:50:44', '7:51:15', '7:51:53',
            '7:52:37', '7:53:27', '7:54:24'
        ),
    );

    return @(%data.{$date});
}