これがテキストによる実装です。私は実際にそれが正しいかどうかをチェックしなかったので、そこにバグが潜んでいる可能性がありますが、一度に1行ずつ入力を処理する方法を説明する必要があります。
#!/usr/bin/env perl
use strict; use warnings;
use constant ONE_MINUTE => 60;
use constant ONE_HOUR => 60 * ONE_MINUTE;
use constant ONE_DAY => 24 * ONE_HOUR;
use constant BLOCK_SIZE => ONE_MINUTE * 20;
use constant DAY_LENGTH => ONE_DAY/BLOCK_SIZE;
my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
my $remainder = 0;
while (my $line = <DATA>) {
next unless $line =~ m{
\A
( [0-6] ) \s+
( [0-9]+ ) \s+
( [0-9]+ ) \s+
\z
}x;
my ($daynum, $start, $duration) = ($1, $2, $3);
my $start_block = seconds_to_blocks($start);
my $duration_block = seconds_to_blocks($duration);
my ($dayrow, $hang) = make_dayrow(
$remainder,
$start_block,
$duration_block,
);
printf "%3s: %s\n", $days[$daynum], $dayrow;
$remainder = $hang;
}
sub seconds_to_blocks {
my ($seconds) = @_;
return int($seconds / BLOCK_SIZE);
}
sub make_dayrow {
my ($remainder, $start, $duration) = @_;
if ($remainder > DAY_LENGTH) {
my $hang = $remainder - DAY_LENGTH;
return ('#' x DAY_LENGTH, $hang);
}
my $hang = $start + $duration > DAY_LENGTH
? $duration - (DAY_LENGTH - $start)
: 0
;
my $dayrow = '#' x $remainder;
$dayrow .= ' ' x ($start - $remainder);
$dayrow .= '#' x ($duration - $hang);
$dayrow .= ' ' x (DAY_LENGTH - length $dayrow);
return ($dayrow, $hang);
}
__DATA__
0 24000 97200
1 52200 95400
2 0 0
3 37800 180000
4 0 0
5 48000 95400
6 0 0
出力:
太陽: ################################################ ####
月:############################# ################### ##########
火:################################################ ##
結婚した: #########################################
木:################################################ ########################
金:##################################### ########### #####################
土:###############################################
アップデート
パーセンテージだけが必要な場合は、それも簡単です。
#!/usr/bin/env perl
use strict; use warnings;
use YAML;
use constant ONE_MINUTE => 60;
use constant ONE_HOUR => 60 * ONE_MINUTE;
use constant ONE_DAY => 24 * ONE_HOUR;
my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
my $remainder = 0;
my @rows;
while (my $line = <DATA>) {
next unless $line =~ m{
\A
( [0-6] ) \s+
( [0-9]+ ) \s+
( [0-9]+ ) \s+
\z
}x;
my ($daynum, $start, $duration) = ($1, $2, $3);
my $dayrow = make_dayrow($remainder, $start, $duration);
push @rows, $dayrow->[0];
$remainder = $dayrow->[1];
}
for my $row (@rows) {
print join("\t", map sprintf('%.0f%%', $_ * 100), @$row), "\n";
}
sub make_dayrow {
my ($remainder, $start, $duration) = @_;
return [[1, 0, 0], $remainder - ONE_DAY] if $remainder > ONE_DAY;
my $hang = $start + $duration > ONE_DAY
? $duration - (ONE_DAY - $start)
: 0
;
return [
[
$remainder / ONE_DAY,
$start / ONE_DAY,
($duration - $hang) / ONE_DAY
],
$hang
];
}
__DATA__
0 24000 97200
1 52200 95400
2 0 0
3 37800 180000
4 0 0
5 48000 95400
6 0 0
出力:
0%28%72%
40%60%40%
71%0%0%
0%44%56%
10000%
52%56%44%
66%0%0%