This commit is contained in:
Ryan 2025-12-14 15:17:58 -05:00
parent ff1745df3a
commit 3368e638eb
1 changed files with 27 additions and 54 deletions

View File

@ -8,6 +8,7 @@ use Thread::Queue;
my @lines;
my $startx;
{
my $splits=0;
my %positions;
@ -20,12 +21,13 @@ my @lines;
if($c eq 'S') {
$nextpositions{$i}=1;
$startx = $i;
} elsif($c eq '.' && $positions{$i}) {
$nextpositions{$i}=1;
$nextpositions{$i}=$positions{$i};
} elsif($c eq '^' && $positions{$i}) {
$splits++;
$nextpositions{$i-1}=1 unless($i == 0);
$nextpositions{$i+1}=1 unless($i == length($line)-1);
$nextpositions{$i-1} += $positions{$i};
$nextpositions{$i+1} += $positions{$i};
}
}
@ -36,60 +38,31 @@ my @lines;
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 $times = calcpos_r($startx, 0);
print "$times timelines\n";
my $ends = 0;
my $numworkers = $ARGV[0] || 16;
my $queue = Thread::Queue->new();
my @threads;
sub calcpos_r {
my($pos, $row)=@_;
$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;
if($row >= @lines) {
return 1;
}
}
}
return $ends;
}
my $char = substr($lines[$row], $pos, 1);
if($char eq '.' || $char eq 'S') {
$row++;
next;
}
if($char eq '^') {
return calcpos_r($pos-1, $row+1) + calcpos_r($pos+1, $row+1);
}
die "Unknown character '$char' at $row : $pos\n";
}
}
__DATA__