-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02a.pl
executable file
·64 lines (48 loc) · 1.14 KB
/
day02a.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
#!/usr/bin/perl
#
# $Id: pl.template,v 1.2 2014/07/18 15:01:38 caran Exp $
#
use strict;
use warnings;
use Path::Tiny;
{ package Box_id;
sub count_dupes {
my ($self) = @_;
for my $let (keys %{ $self->{ letter_cnt } }) {
my $count = $self->{ letter_cnt }{ $let };
$self->{ dupe_cnt }{ $count }++;
}
}
sub count_letters {
my ($self) = @_;
for my $let (split( '', $self->{ id } )) {
$self->{ letter_cnt }{ $let }++;
}
}
sub num_dupes {
my ($self, $num) = @_;
return $self->{ dupe_cnt }{ $num } || 0;
}
sub new {
my $class = shift;
my $id = shift;
my $self = {
id => $id,
letter_cnt => {},
dupe_cnt => {},
};
bless $self, $class;
$self->count_letters();
$self->count_dupes();
return $self;
}
};
my $input_file = $ARGV[0] || 'input02.txt';
my @boxes = path( $input_file )->lines_utf8( { chomp => 1 } );
my ($has_two, $has_three) = (0, 0);
for my $box (@boxes) {
my $box_id = Box_id->new( $box );
$has_two++ if ($box_id->num_dupes( 2 ));
$has_three++ if ($box_id->num_dupes( 3 ));
}
print "The checksum is ", $has_two * $has_three, "\n";