-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.pl
More file actions
24 lines (20 loc) · 748 Bytes
/
day01.pl
File metadata and controls
24 lines (20 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use strict;
use Term::ANSIColor;
use Win32::Console::ANSI;
use Time::HiRes qw/time/;
my $time = time;
my $filename = "${0}_input";
open(my $fh, $filename);
chomp (my $input = <$fh>);
my $sum;
my $sum2;
my $len = length $input;
for (my $i = 0; $i < $len; $i++) {
my $current = substr($input, $i, 1);
my $next = substr($input, ($i+1) % ($len), 1);
my $halfway = substr($input, ($i+$len/2)%($len), 1);
$sum += $current if( $current == $next );
$sum2 += $current if( $current == $halfway );
}
print "Sum of al chars that are equal to their follower is ", colored( $sum, "black on_red" ), ".\n";
print "Sum of al chars that are equal to their halfway equal is ", colored($sum2,"black on_red"), ". ( ", sprintf ("%.3f",time - $time) ," s )\n";