-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13b.pl
70 lines (57 loc) · 1.54 KB
/
13b.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
use List::Util qw(min);
for (<>) {
chomp;
unless ($_) {
$sum += find_smudge();
@pattern = ();
} else {
push @pattern, [split //];
}
}
$sum += find_smudge();
print "$sum\n";
sub find_smudge {
my $original_value = find_symmetry();
for $row (0..@pattern - 1) {
for $col (0..@{$pattern[0]} - 1) {
flip_value($row, $col);
$new_value = find_symmetry($original_value);
return $new_value if $new_value;
flip_value($row, $col);
}
}
}
sub find_symmetry {
# Skip checking the original axis of symmetry when we're looking for a new one.
my $skip_value = shift;
# Look for vertical symmetry
for $pivot (1..@{$pattern[0]}-1) {
next if $pivot == $skip_value;
$possible = 1;
$width = min($pivot, @{$pattern[0]} - $pivot);
for $row (0..@pattern-1) {
for $offset (0..$width-1) {
$possible = 0 unless $pattern[$row]->[$pivot-$offset-1] eq $pattern[$row]->[$pivot+$offset];
}
last unless $possible;
}
return $pivot if $possible;
}
# Look for horizontal symmetry
for $pivot (1..@pattern-1) {
next if $pivot * 100 == $skip_value;
$possible = 1;
$width = min($pivot, @pattern - $pivot);
for $col (0..@{$pattern[0]}-1) {
for $offset (0..$width-1) {
$possible = 0 unless $pattern[$pivot-$offset-1]->[$col] eq $pattern[$pivot + $offset]->[$col];
}
last unless $possible;
}
return $pivot * 100 if $possible;
}
}
sub flip_value {
($row, $col) = @_;
$pattern[$row]->[$col] = $pattern[$row]->[$col] eq '.' ? '#' : '.';
}