-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-2.pl
executable file
·64 lines (51 loc) · 1.63 KB
/
ch-2.pl
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/perl
# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
use v5.26; # The Weekly Challenge - 2024-04-292
use utf8; # Week 267 - task 2 - Line counts
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
line_counts('abcdefghijklmnopqrstuvwxyz',
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]);
line_counts('bbbcccdddaaa',
[ 4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]);
line_counts('thequickbrownfoxjumpsoverthelazydog',
[15, 27, 12, 28, 16, 29, 33, 21, 14, 29, 36, 13, 14,
9, 18, 23, 22, 35, 27, 15, 25, 21, 19, 35, 23, 15]);
sub line_counts {
my ($str, @widths, $ch, $limit, %pixels, $lines,
$chars, $j, $line, @lines, @line_length);
$str = $_[0];
@widths = @{$_[1]};
$limit = 100;
# set $pixels{$char} to the width of $char
for $j (0 .. 25) {
$pixels{chr(ord('a') + $j)} = $widths[$j];
}
# loop over characters in $str
$line = 0;
$chars = $limit;
while ($str =~ m|([a-z])|g) {
# needs a new line
$ch = $1;
if ($chars + $pixels{$ch} > $limit) {
$line ++;
$chars = $pixels{$ch};
# fits on current line
} else {
$chars += $pixels{$ch};
}
# save for explanation
$lines[$line] .= $ch;
$line_length[$line] += $pixels{$ch};
}
# results
say qq[\nInput: \@str = '$str'];
say qq[ \$widths = (] . join(', ', @widths[0 .. 12]) . ',';
say qq[ ] . join(', ', @widths[13 .. 25]) . ')';
say qq[Output: ($line, $chars)];
for $j (1 .. @lines - 1) {
say qq[ Line $j: $lines[$j] ($line_length[$j] pixels)];
}
}