wip
This commit is contained in:
parent
ff1745df3a
commit
3368e638eb
81
day07/run.pl
81
day07/run.pl
|
|
@ -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,61 +38,32 @@ 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";
|
||||
|
||||
sub calcpos_r {
|
||||
my($pos, $row)=@_;
|
||||
|
||||
|
||||
for(;;) {
|
||||
if($row >= @lines) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
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......................................................................
|
||||
|
|
|
|||
Loading…
Reference in New Issue