-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.pl
72 lines (66 loc) · 1.51 KB
/
20.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
#!/usr/bin/perl -w
use strict;
no warnings 'portable';
use Data::Dumper;
use feature 'say';
use Clipboard;
use List::Util qw/sum min max reduce any all none notall first product uniq/;
use Math::Cartesian::Product;
use Math::Complex;
use List::PriorityQueue;
use Memoize;
sub out {
my $out = shift;
if (ref($out)) {
print Dumper($out);
} else {
Clipboard->copy_to_all_selections($out);
print "$out\n";
}
}
# Read code
$_ = <>;
chomp;
my @CODE = map {$_ eq '#'} split('');
# Read initial position
my @A;
my $r=0;
while (<>) {
chomp;
my $c=0;
for my $v (split('')) {
push @A, "$r,$c" if $v eq '#';
$c++;
}
$r++;
}
# Neighbors, initialize background
my @NARR = reverse([-1, -1], [-1, 0], [-1, 1], [ 0, -1], [ 0, 0], [ 0, 1], [ 1, -1], [ 1, 0], [ 1, 1]);
my $bg = 0;
# Enchancement steps
for my $i (1..50) {
my %H;
my $nbg = $CODE[$bg?-1:0];
# Collect the relevant 3x3 binary values based on neighbors
for my $k (@A) {
my ($r,$c) = split(',', $k);
for my $n (0..$#NARR) {
my ($dx, $dy) = @{$NARR[$n]};
my ($nr, $nc) = ($r-$dx, $c-$dy);
$H{"$nr,$nc"} |= 1 << $n;
}
}
# Create the output list of non-bg values
@A=();
while (my ($k,$val) = each %H) {
# need to flip the code if the bg is reversed
$val = (~$val & 511) if $bg;
# Only set the output if it's different from the background
if ($CODE[$val] ^ $nbg) {
push @A, $k;
}
}
$bg=$nbg;
print "Number of ".($bg?"un":"")."lit pixels after iteration $i: ";
out (scalar @A);
}