Coffee Break
AUTHOR
Nelo Onyiah
Itās Monday morning, at least half the people in the building had to work through the weekend, everyoneās a little tired and thereās still a ton of work to do. So the kindly group assistant has decided to go get everyone coffee. Each person can choose between a Latte, an Espresso, and a Cappuccino. The assistant wanders from one office to the next taking orders. (There are several people in each office.) He writes down the office number, then the number of Lattes, Espressos, and cappuccinos for each. His list looks like this:
Office 100
Espresso 3
Latte 1
Cappuccino 1
Office 200
Cappucino 2
Latte 2
Espresso 1
Office 300
ā¦
And so on. As soon as heās done, however, he realizes he has one problem: he doesnāt want to place an order for 3 espressos and 1 espresso and 2 espressos, etc. He needs to place an order for the total number of each type of drink. Youāll successfully complete this event if you read from the text file containing the order (Coffee.txt in the Scripting Games 2008 Competitorās Pack) and correctly output a tally of the number of each type of drink the assistant needs to order, something like this:
Espresso: 15
Latte: 10
Cappuccino: 14
Make sure that you put the file Coffee.txt in the C:\Scripts folder. If you reference any path other than C:\Scripts\Coffee.txt your script will probably fail when we test it.
use v6;
our %count_for;
sub MAIN(Str $orders = $*SPEC.catdir($*PROGRAM-NAME.IO.dirname, 'coffee.txt')) {
# get the data
my @contents = $orders.IO.lines;
# count the orders
for @contents -> $line {
unless $line ~~ /^ (Office \s+ \d+) $/ {
my ($drink, $count) = $line.split(/\s+/);
%count_for{$drink} += $count;
}
}
# print to screen
for %count_for.kv -> $drink, $total {
say $drink ~ ": " ~ $total;
}
}
# vim: expandtab shiftwidth=4 ft=perl6