-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-12-star-1.pl
executable file
·67 lines (52 loc) · 1.49 KB
/
day-12-star-1.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
#!/usr/bin/perl
use warnings;
use 5.036;
use List::Util qw(sum);
my $sum;
while(<>) {
chomp;
(my $springs, my $groups) = split(/ /);
my @groups = split(/,/, $groups);
my $totalSprings = sum @groups;
# say "Total springs: $totalSprings";
my @queue = ($springs);
while (@queue) {
my $current = shift @queue;
my $brokenSprings = $current =~ tr/#//;
my $springsLeft = $totalSprings - $brokenSprings;
my $slotsLeft = $current =~ tr/?//;
# my $slotsLeft = ($#slotsLeft + 1);
# say "Slots: $slotsLeft, springs: $springsLeft";
# check if legal
if ($springsLeft < 0 || $slotsLeft < $springsLeft) {
next;
}
# next unless &isLegal($current, \@groups);
# if legal, check if leaf
if ($slotsLeft == 0 && $springsLeft == 0) {
$sum += &isGoodLeaf($current, \@groups);
next;
}
# if not leaf, add two new ones
my $left = $current =~ s/\?/\#/r;
my $right = $current =~ s/\?/\./r;
# say $left;
# say $right;
push(@queue, $left, $right);
}
# say @queue;
}
say $sum;
sub isGoodLeaf($springs, $groupsRef) {
my @springs = $springs =~ /(\#+)/g;
my @springGroups = map { length($_) } @springs;
if ($#springGroups != $#{$groupsRef}) {
return 0;
}
for my $i (0..$#springs) {
if ($springGroups[$i] != ${$groupsRef}[$i]) {
return 0;
}
}
return 1;
}