-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday09.pl
More file actions
34 lines (32 loc) · 801 Bytes
/
day09.pl
File metadata and controls
34 lines (32 loc) · 801 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
25
26
27
28
29
30
31
32
33
34
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);
my $score = 0;
my $currentLevel = 0;
my $inGarbage=0;
my $charsInGarbage=0;
while( 0 < read($fh, my $input, 1) ) {
if( !$inGarbage ) {
if( $input eq "{" ) {
$currentLevel++;
} elsif( $input eq "}" ) {
$score+=$currentLevel--;
} elsif( $input eq "<" ) {
$inGarbage=1;
}
} else {
if( $input eq "!") {
read $fh, my $input, 1;
} elsif ( $input eq ">" ) {
$inGarbage = 0;
} else {
$charsInGarbage++;
}
}
}
print "The total score is ", colored( $score, "black on_red" ), ".\n";
print "The total amount of garbage is ", colored( $charsInGarbage, "black on_red" ), ". ( ", sprintf ("%.3f",time - $time) ," s )\n";