-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-2.pl
56 lines (45 loc) · 1.34 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
#!/usr/bin/env perl
# The Weekly Challenge 267 - By Nelo Tovar
#
# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
#
# Task 2 - Line Counts
#
use strict;
use warnings;
use v5.28;
use Data::Dump qw(dump);
my @example_lines = ("abcdefghijklmnopqrstuvwxyz", "bbbcccdddaaa");
my @example_widths =(
[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],
[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],
);
sub line_counts {
my $line = shift;
my $widths = shift;
my $len = length $line;
my $width_sum = 0;
my $line_counter = 0;
my $ord_a = ord('a');
for (my $i = 0; $i < $len; $i++) {
my $letter = ord(substr($line,$i,1)) - $ord_a;
if ($width_sum + @$widths[$letter] <= 100) {
$width_sum += @$widths[$letter]
}else {
$line_counter++;
$width_sum = @$widths[$letter];
}
}
$line_counter++ if ($width_sum);
return [$line_counter, $width_sum];
}
for (my $i = 0; $i < scalar @example_lines; $i++) {
my $line = $example_lines[$i];
my $width = $example_widths[$i];
my $lc = line_counts $line, $width;
$Data::Dump::LINEWIDTH = 100;
say 'Input : @str = ', $line;
say ' @widths = ', dump(@$width);
say 'Output : ', dump(@$lc);
say ' ';
}