-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.pl
89 lines (74 loc) · 1.72 KB
/
solver.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
use warnings;
open(inputFile, '<', "input.txt") or die $!;
my @state = [];
while (<inputFile>){
my $fileline = $_;
my @spl = split '', $fileline;
for (@spl) {
if ($_ =~ /[0-9]/) {
push(@state, ($_ + 0));
}
}
}
my $SIZE = 10;
my $flash_count = 0;
sub increase_squid {
my $state = $_[0];
my $y = $_[1];
my $x = $_[2];
if ($y < 0 || $y >= $SIZE) {return;}
if ($x <= 0 || $x > $SIZE) {return;}
if($state[$y * $SIZE + $x] == 9) {
$state[$y * $SIZE + $x]++;
flashes($state, $y, $x);
} else {
$state[$y * $SIZE + $x]++;
}
}
sub flashes {
$flash_count++;
my $state = $_[0];
my $y = $_[1];
my $x = $_[2];
for (my $dy = -1; $dy <= 1; $dy++) {
for (my $dx = -1; $dx <= 1; $dx++) {
if (!($dx == $dy && $dx == 0)) {
increase_squid($state, $y + $dy, $x + $dx);
}
}
}
}
sub next_state {
my $state = $_[0];
for(my $y = 0; $y < $SIZE; $y++)
{
for (my $x = 1; $x <= $SIZE; $x++) {
increase_squid($state, $y, $x);
}
}
for(my $y = 0; $y < $SIZE; $y++)
{
for (my $x = 1; $x <= $SIZE; $x++) {
if($state[$y * $SIZE + $x] > 9) {
$state[$y * $SIZE + $x] = 0;
}
}
}
return $state;
}
for (my $i = 0; $i < 300; $i++) {
next_state(@state);
if($i == 100) {
print "Part 1 : ",$flash_count, "\n";
}
my $all_flashes = 1;
for (my $j = 1; $j <= $SIZE * $SIZE; $j++) {
if ($state[$j] != 0) {
$all_flashes = 0;
}
}
if ($all_flashes == 1) {
print "Part 2 : ", $i + 1, "\n";
exit;
}
}