237 lines
22 KiB
Perl
Executable File
237 lines
22 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
use strict;
|
|
use Config;
|
|
$Config{useithreads} or
|
|
die('Recompile Perl with threads to run this program.');
|
|
use threads;
|
|
use Thread::Queue;
|
|
|
|
|
|
my @lines;
|
|
{
|
|
my $splits=0;
|
|
my %positions;
|
|
my %nextpositions;
|
|
while(my $line=<DATA>) {
|
|
chomp($line);
|
|
push @lines, $line;
|
|
for(my $i=0; $i<length $line; $i++) {
|
|
my $c = substr($line, $i, 1);
|
|
|
|
if($c eq 'S') {
|
|
$nextpositions{$i}=1;
|
|
} elsif($c eq '.' && $positions{$i}) {
|
|
$nextpositions{$i}=1;
|
|
} elsif($c eq '^' && $positions{$i}) {
|
|
$splits++;
|
|
$nextpositions{$i-1}=1 unless($i == 0);
|
|
$nextpositions{$i+1}=1 unless($i == length($line)-1);
|
|
}
|
|
}
|
|
|
|
%positions=%nextpositions;
|
|
%nextpositions=();
|
|
}
|
|
|
|
print "Saw $splits splits\n";
|
|
}
|
|
|
|
my @rows;
|
|
my $startpos;
|
|
for(my $y=0; $y<@lines; $y++) {
|
|
for(my $x=0; $x<length $lines[0]; $x++) {
|
|
$rows[$x]=[] if($y==0);
|
|
$rows[$x][$y] = substr($lines[$y], $x, 1);
|
|
if($rows[$x][$y] eq 'S') {
|
|
$startpos=[$x,$y];
|
|
}
|
|
}
|
|
}
|
|
|
|
my $ends = 0;
|
|
my $numworkers = $ARGV[0] || 16;
|
|
my $queue = Thread::Queue->new();
|
|
my @threads;
|
|
|
|
$queue->enqueue($startpos);
|
|
for(my $i=0; $i<$numworkers; $i++) {
|
|
push @threads, threads->create(\&process, $i);
|
|
}
|
|
|
|
foreach my $thread (@threads) {
|
|
my $count=$thread->join();
|
|
print "Joined thread with $count ends\n";
|
|
$ends+=$count;
|
|
}
|
|
print "$ends total ends\n";
|
|
|
|
sub process {
|
|
my $threadid=shift(@_);
|
|
my $ends = 0;
|
|
while(my $pos=$queue->dequeue_timed(5)) {
|
|
if($threadid == 0) {
|
|
print $queue->pending() . " pending\n";
|
|
}
|
|
my($x,$y)=@$pos;
|
|
|
|
for(;;) {
|
|
$y++;
|
|
if($y == @{ $rows[$x] }) {
|
|
$ends++;
|
|
last;
|
|
} elsif($rows[$x][$y] eq '^') {
|
|
$queue->enqueue([$x-1, $y]);
|
|
$x++;
|
|
#$queue->enqueue([$x+1, $y]);
|
|
#last;
|
|
}
|
|
}
|
|
}
|
|
return $ends;
|
|
}
|
|
|
|
|
|
|
|
__DATA__
|
|
......................................................................S......................................................................
|
|
.............................................................................................................................................
|
|
......................................................................^......................................................................
|
|
.............................................................................................................................................
|
|
.....................................................................^.^.....................................................................
|
|
.............................................................................................................................................
|
|
....................................................................^.^.^....................................................................
|
|
.............................................................................................................................................
|
|
...................................................................^...^.^...................................................................
|
|
.............................................................................................................................................
|
|
..................................................................^.^...^.^..................................................................
|
|
.............................................................................................................................................
|
|
.................................................................^...^.^.^.^.................................................................
|
|
.............................................................................................................................................
|
|
................................................................^.^.........^................................................................
|
|
.............................................................................................................................................
|
|
...............................................................^.^...^.^.^.^.^...............................................................
|
|
.............................................................................................................................................
|
|
..............................................................^.^.^.^.^.^.....^..............................................................
|
|
.............................................................................................................................................
|
|
.............................................................^.^.....^...^.^.^.^.............................................................
|
|
.............................................................................................................................................
|
|
............................................................^...^.....^.^...^.^.^............................................................
|
|
.............................................................................................................................................
|
|
...........................................................^.....^.^.^.^.^.^.^.^.^...........................................................
|
|
.............................................................................................................................................
|
|
..........................................................^...^...^.....^.^...^.^.^..........................................................
|
|
.............................................................................................................................................
|
|
.........................................................^.^.....^.^.....^.^.^.^.^.^.........................................................
|
|
.............................................................................................................................................
|
|
........................................................^...^.^.^.^...^.^.....^.....^........................................................
|
|
.............................................................................................................................................
|
|
.......................................................^.^.^.^.^.^.....^.....^.^.^.^.^.......................................................
|
|
.............................................................................................................................................
|
|
......................................................^.^.....^.^.^.....^.^...^...^.^.^......................................................
|
|
.............................................................................................................................................
|
|
.....................................................^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.....................................................
|
|
.............................................................................................................................................
|
|
....................................................^.^.^...^.^.^.^.^.^.^.^...^...^.^.^.^....................................................
|
|
.............................................................................................................................................
|
|
...................................................^.^...^.^.^.^.^.^...^.^.^.^.^...^...^.^...................................................
|
|
.............................................................................................................................................
|
|
..................................................^.^.^.^...^.^.^.^.^.^.^...^.^...^.^...^.^..................................................
|
|
.............................................................................................................................................
|
|
.................................................^.^.^.^...^.^.......^.^.^.^.....^.^.^.^.^.^.................................................
|
|
.............................................................................................................................................
|
|
................................................^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^................................................
|
|
.............................................................................................................................................
|
|
...............................................^.....^...^.^.^.^.^...^...^.^.^.^.^.^.^.^...^.^...............................................
|
|
.............................................................................................................................................
|
|
..............................................^...^.^...^.^...^.....^.^.^.^.^.^...^.^.^.......^..............................................
|
|
.............................................................................................................................................
|
|
.............................................^.^.^.^.^...^.^.^.^.^.^...^.....^.^.....^.^.^...^.^.............................................
|
|
.............................................................................................................................................
|
|
............................................^.^.^.^.^.^.^.^.^.^.^...^.^.....^...^.^...^.^.^.^...^............................................
|
|
.............................................................................................................................................
|
|
...........................................^.^.....^.^.....^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.....^...........................................
|
|
.............................................................................................................................................
|
|
..........................................^.^.^...^.....^.^.^.^.^...^.^.....^.^.^.^...^...^.^...^.^..........................................
|
|
.............................................................................................................................................
|
|
.........................................^.^...^.^.^.^.^.^.^.^.^.^.^.....^.....^.^.^.^.^.^.^.^.^.^.^.........................................
|
|
.............................................................................................................................................
|
|
........................................^.....^.^...^.^.^.^.....^.^.^.^...^...^.^.^.^.^...^.^...^.^.^........................................
|
|
.............................................................................................................................................
|
|
.......................................^.^.^.^.....^.^.^.......^.^.........^...^.^.^.^.....^.^.....^.^.......................................
|
|
.............................................................................................................................................
|
|
......................................^.^.....^.^.^...^.^...^.^.......^.^...^.^.^.^.^.^...^.....^.^...^......................................
|
|
.............................................................................................................................................
|
|
.....................................^.^.^...^.^.^.^.^.....^.^...^.....^.....^...^.^...^.......^.^.^.^.^.....................................
|
|
.............................................................................................................................................
|
|
....................................^.^.^...^...^.^.^...^.^...^.^.^.^.......^.....^.....^.^.......^.^.^.^....................................
|
|
.............................................................................................................................................
|
|
...................................^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^...^.^...^.^.^.^.^...^.^.^...................................
|
|
.............................................................................................................................................
|
|
..................................^...^.^.^.^...^.......^.^.^...^.^.......^.^.^.....^...^...^.^.^...^.^...^..................................
|
|
.............................................................................................................................................
|
|
.................................^...^.......^.^.^.....^.^...^.^...^.^.^.^.^.^.....^.^...^...^.^.^...^...^.^.................................
|
|
.............................................................................................................................................
|
|
................................^.^.^...^.^.^.^.^.^.^...^...^.^.......^.^...^.^.^...^.^...^.^.^.^.^...^...^.^................................
|
|
.............................................................................................................................................
|
|
...............................^.^.....^.^.^...^.^.^.^...^.........^.^.^.^.........^.^.^...........^.^.^.^...^...............................
|
|
.............................................................................................................................................
|
|
..............................^.^...^.^...^...^...^...^.^.^.^.^.^...^.^.^...^.^.^...^.....^.^.^...^...^.^.^...^..............................
|
|
.............................................................................................................................................
|
|
.............................^.^.^.^.^.^.^.....^.^.^.^.^.^.....^...^.^.^.......^.^.^.^.....^.........^.^.^.....^.............................
|
|
.............................................................................................................................................
|
|
............................^...^...^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^.......^.^.....^.^.^.^...^.^...^...^............................
|
|
.............................................................................................................................................
|
|
...........................^.^.^...^.^.^.....^.....^.^.......^.^.^.....^.^.^...^.....^...^.^.^.^.....^.^.^.^.^.^.^...........................
|
|
.............................................................................................................................................
|
|
..........................^.....^.....^.^...^.^.^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.^.^.....^.^.^...^.^...^...^..........................
|
|
.............................................................................................................................................
|
|
.........................^.^...^.^.^.^...^.^...^.^.........^.......^.^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^...^.........................
|
|
.............................................................................................................................................
|
|
........................^...^.^.^.^...^.^...^...^.^...^.^.^...^.^.^.^.^.^...^.^.^...^.......^.^.^.^.^...^.....^...^.^........................
|
|
.............................................................................................................................................
|
|
.......................^.^.^...^.^.^.....^.^...^.^.^...^...^.^.^...^.^.^...^.^.....^.^.^.^.^...^.^.........^.^.^.^.^.^.......................
|
|
.............................................................................................................................................
|
|
......................^.^.^.^...^.^.^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.....^.^.....^...^.^.^.^.....^...........^.^.^......................
|
|
.............................................................................................................................................
|
|
.....................^.^.^.^.^.^...^.^.^.^.^.....^.^...^...^...^.^.^.^.^.....^.^.^.....^.^...^.......^.^.^.....^.^.^.^.^.....................
|
|
.............................................................................................................................................
|
|
....................^...^.^.......^.^.^.^.^.^.........^.^.^.^.......^.^...^.^.^.^.^.^.^.^.^...^.^.^.....^.^.^.^.^.^.^.^.^....................
|
|
.............................................................................................................................................
|
|
...................^.^.......^.^...^.^.^.^.^.^...^.^...^.^...^.....^.^.^...^.^...^.^.^.^.^...^...^.^.....^.^...^.^.^.^.^.^...................
|
|
.............................................................................................................................................
|
|
..................^.^.........^.^.^...^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.....^...^.^..................
|
|
.............................................................................................................................................
|
|
.................^.^.^...^.^.^.^.^.......^.^.....^.^...^.^.^...^.^.^...^.^.^.^.^...^.......^.^...^.^.^.........^.^...^.^.^.^.................
|
|
.............................................................................................................................................
|
|
................^.....^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^...^.^.^.^...^.^.^.^.^.^.....^...^.....^.^................
|
|
.............................................................................................................................................
|
|
...............^.....^.^.^.^.^.^.^.^.^.^...^.^...^...^...^...^...^.^.^.^.........^.^.^.^.^...^.....^.^.......^...^...^.^.^.^.^...............
|
|
.............................................................................................................................................
|
|
..............^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^...^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^...^.^.^.^.^...^.....^.....^..............
|
|
.............................................................................................................................................
|
|
.............^.^...^.^.....^.......^.....^.....^.^...^.^.^.^.....^.^.....^.....^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.............
|
|
.............................................................................................................................................
|
|
............^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^...^.....^.^...^...^.........^.^...^.^...^.^.^.^.^.^.^.^...^............
|
|
.............................................................................................................................................
|
|
...........^...^.^.^...^.^...^...^.^...^...^.^.^.^...^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^.....^.^.^.^...........
|
|
.............................................................................................................................................
|
|
..........^.^.........^.^...^.....^.^.^.^.^.^.^.^...^...^...^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^.^...^.^.^.^...^.....^...^...^..........
|
|
.............................................................................................................................................
|
|
.........^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.....^...^.^.....^.^...^...^.^...^.^.^.^...^.^...^...^.^.^.^...^...^.....^.^.^...^...^.^.........
|
|
.............................................................................................................................................
|
|
........^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.........^...^.^.^.^.....^.^...^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^...^.^.^.....^.^.....^.^.^........
|
|
.............................................................................................................................................
|
|
.......^.......^.^.^.^...^.^...^.^...^.^.^.^.^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^...^.^.^.^.^.^.^...^.^.....^.^.^...^.^.^...^.^.^.......
|
|
.............................................................................................................................................
|
|
......^...^...^.^.....^.^.^.^.^.^.^...^.^.^.^...^.^.^...^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^.^.^.^.^.^...^.^......
|
|
.............................................................................................................................................
|
|
.....^.....^.^.^.^.^...^.........^.....^.......^.^.^...^.^.......^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^...^...^.^...^.^.....^.^.....
|
|
.............................................................................................................................................
|
|
....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^.^.^.^...^.....^.^.....^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^...^...^....
|
|
.............................................................................................................................................
|
|
...^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.....^.^.^.^.^.^...^.......^.^...^...^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^.^...^...
|
|
.............................................................................................................................................
|
|
..^.^.^.^...^.^.^.^.^...^.....^...^.^.^...^...^.^.^.^...^...^.^.^...^...........^.^.^.^.......^...^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^..
|
|
.............................................................................................................................................
|
|
.^.^.^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.....^.^.......^.^.^.^.^...^...^.^.^.^.......^.....^.
|
|
............................................................................................................................................. |