-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.pl
98 lines (81 loc) · 1.72 KB
/
day11.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl -l
use warnings;
use strict;
use List::Util qw(min);
use List::Util qw(max);
sub expand {
my $arr = $_[0];
my $i = $_[1];
my $j = $_[2];
for my $ii (max(0,$i-1)..min(9,$i+1)) {
for my $jj (max(0,$j-1)..min(9,$j+1)) {
if ( $arr->[$ii][$jj] == 9 ) {
$arr->[$ii][$jj] += 1;
expand($arr, $ii, $jj);
} elsif ( $arr->[$ii][$jj] < 9 ) {
$arr->[$ii][$jj] += 1;
}
}
}
}
sub increment {
my $arr = $_[0];
for my $i (0..9) {
for my $j (0..9) {
if ( $arr->[$i][$j] == 9 ) {
$arr->[$i][$j] += 1;
expand($arr, $i, $j);
} elsif ( $arr->[$i][$j] < 9 ) {
$arr->[$i][$j] += 1;
}
}
}
}
sub clean {
my $arr = $_[0];
my $flashes = 0;
for my $i (0..9) {
for my $j (0..9) {
if ( $arr->[$i][$j] == 10 ) {
$arr->[$i][$j] = 0;
$flashes += 1;
}
}
}
return $flashes;
}
sub show {
my $arr = $_[0];
$"="";
for my $i (0..9) {
print( @{$arr->[$i]} );
}
print("- - - - -");
}
open(FH, '<', 'input.txt') or die $!;
my @arr = ();
my $i = 0;
while(<FH>){
chomp($_);
for my $j (0..9) {
$arr[$i][$j] = int(substr($_, $j, 1));
}
$i += 1;
}
show(\@arr);
my $flashes = 0;
my $flashing = 0;
for my $i (1..500) {
increment(\@arr);
my $flashing = clean(\@arr);
if ($flashing == 100) {
print("Day 11, part1: ", $flashes);
print("Day 11, part2: ", $i);
last;
}
if ($i <= 100) {
$flashes += $flashing;
}
show(\@arr);
}
close(FH);