diff --git a/day07/run.pl b/day07/run.pl index 5cadc81..4f97292 100755 --- a/day07/run.pl +++ b/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= @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......................................................................