/
ch-1.pl
executable file
·49 lines (38 loc) · 1.16 KB
/
ch-1.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
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use experimental 'signatures';
use List::Util 'min';
sub main (@notes) {
# Store what is in the wallet, key is the dollar value, value is the
# number of notes we have
my %wallet = ();
C: foreach my $customer_note (@notes) {
# Add the amount the person has given us
$wallet{$customer_note}++;
if ( $customer_note == 5 ) {
# No change required :)
next;
}
my $change = $customer_note - 5;
# We try the bigger notes first
foreach my $wallet_note ( sort { $b <=> $a } keys %wallet ) {
my $note_count =
min( $wallet{$wallet_note}, int( $change / $wallet_note ) );
if ( $note_count > 0 ) {
# Take the note from the wallet, and reduce the change still
# to give
$wallet{$wallet_note} -= $note_count;
$change -= $wallet_note * $note_count;
if ( $change == 0 ) {
next C;
}
}
}
say 'false';
return;
}
say 'true';
}
main(@ARGV);