#!/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=) { chomp($line); push @lines, $line; for(my $i=0; $inew(); 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...................................................................... ............................................................................................................................................. ......................................................................^...................................................................... ............................................................................................................................................. .....................................................................^.^..................................................................... ............................................................................................................................................. ....................................................................^.^.^.................................................................... ............................................................................................................................................. ...................................................................^...^.^................................................................... ............................................................................................................................................. ..................................................................^.^...^.^.................................................................. ............................................................................................................................................. .................................................................^...^.^.^.^................................................................. ............................................................................................................................................. ................................................................^.^.........^................................................................ ............................................................................................................................................. ...............................................................^.^...^.^.^.^.^............................................................... ............................................................................................................................................. ..............................................................^.^.^.^.^.^.....^.............................................................. ............................................................................................................................................. .............................................................^.^.....^...^.^.^.^............................................................. ............................................................................................................................................. ............................................................^...^.....^.^...^.^.^............................................................ ............................................................................................................................................. ...........................................................^.....^.^.^.^.^.^.^.^.^........................................................... ............................................................................................................................................. ..........................................................^...^...^.....^.^...^.^.^.......................................................... ............................................................................................................................................. .........................................................^.^.....^.^.....^.^.^.^.^.^......................................................... ............................................................................................................................................. ........................................................^...^.^.^.^...^.^.....^.....^........................................................ ............................................................................................................................................. .......................................................^.^.^.^.^.^.....^.....^.^.^.^.^....................................................... ............................................................................................................................................. ......................................................^.^.....^.^.^.....^.^...^...^.^.^...................................................... ............................................................................................................................................. .....................................................^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^..................................................... ............................................................................................................................................. ....................................................^.^.^...^.^.^.^.^.^.^.^...^...^.^.^.^.................................................... ............................................................................................................................................. ...................................................^.^...^.^.^.^.^.^...^.^.^.^.^...^...^.^................................................... ............................................................................................................................................. ..................................................^.^.^.^...^.^.^.^.^.^.^...^.^...^.^...^.^.................................................. ............................................................................................................................................. .................................................^.^.^.^...^.^.......^.^.^.^.....^.^.^.^.^.^................................................. ............................................................................................................................................. ................................................^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^................................................ ............................................................................................................................................. ...............................................^.....^...^.^.^.^.^...^...^.^.^.^.^.^.^.^...^.^............................................... ............................................................................................................................................. ..............................................^...^.^...^.^...^.....^.^.^.^.^.^...^.^.^.......^.............................................. ............................................................................................................................................. .............................................^.^.^.^.^...^.^.^.^.^.^...^.....^.^.....^.^.^...^.^............................................. ............................................................................................................................................. ............................................^.^.^.^.^.^.^.^.^.^.^...^.^.....^...^.^...^.^.^.^...^............................................ ............................................................................................................................................. ...........................................^.^.....^.^.....^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.....^........................................... ............................................................................................................................................. ..........................................^.^.^...^.....^.^.^.^.^...^.^.....^.^.^.^...^...^.^...^.^.......................................... ............................................................................................................................................. .........................................^.^...^.^.^.^.^.^.^.^.^.^.^.....^.....^.^.^.^.^.^.^.^.^.^.^......................................... ............................................................................................................................................. ........................................^.....^.^...^.^.^.^.....^.^.^.^...^...^.^.^.^.^...^.^...^.^.^........................................ ............................................................................................................................................. .......................................^.^.^.^.....^.^.^.......^.^.........^...^.^.^.^.....^.^.....^.^....................................... ............................................................................................................................................. ......................................^.^.....^.^.^...^.^...^.^.......^.^...^.^.^.^.^.^...^.....^.^...^...................................... ............................................................................................................................................. .....................................^.^.^...^.^.^.^.^.....^.^...^.....^.....^...^.^...^.......^.^.^.^.^..................................... ............................................................................................................................................. ....................................^.^.^...^...^.^.^...^.^...^.^.^.^.......^.....^.....^.^.......^.^.^.^.................................... ............................................................................................................................................. ...................................^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^...^.^...^.^.^.^.^...^.^.^................................... ............................................................................................................................................. ..................................^...^.^.^.^...^.......^.^.^...^.^.......^.^.^.....^...^...^.^.^...^.^...^.................................. ............................................................................................................................................. .................................^...^.......^.^.^.....^.^...^.^...^.^.^.^.^.^.....^.^...^...^.^.^...^...^.^................................. ............................................................................................................................................. ................................^.^.^...^.^.^.^.^.^.^...^...^.^.......^.^...^.^.^...^.^...^.^.^.^.^...^...^.^................................ ............................................................................................................................................. ...............................^.^.....^.^.^...^.^.^.^...^.........^.^.^.^.........^.^.^...........^.^.^.^...^............................... ............................................................................................................................................. ..............................^.^...^.^...^...^...^...^.^.^.^.^.^...^.^.^...^.^.^...^.....^.^.^...^...^.^.^...^.............................. ............................................................................................................................................. .............................^.^.^.^.^.^.^.....^.^.^.^.^.^.....^...^.^.^.......^.^.^.^.....^.........^.^.^.....^............................. ............................................................................................................................................. ............................^...^...^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^.......^.^.....^.^.^.^...^.^...^...^............................ ............................................................................................................................................. ...........................^.^.^...^.^.^.....^.....^.^.......^.^.^.....^.^.^...^.....^...^.^.^.^.....^.^.^.^.^.^.^........................... ............................................................................................................................................. ..........................^.....^.....^.^...^.^.^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.^.^.....^.^.^...^.^...^...^.......................... ............................................................................................................................................. .........................^.^...^.^.^.^...^.^...^.^.........^.......^.^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^...^......................... ............................................................................................................................................. ........................^...^.^.^.^...^.^...^...^.^...^.^.^...^.^.^.^.^.^...^.^.^...^.......^.^.^.^.^...^.....^...^.^........................ ............................................................................................................................................. .......................^.^.^...^.^.^.....^.^...^.^.^...^...^.^.^...^.^.^...^.^.....^.^.^.^.^...^.^.........^.^.^.^.^.^....................... ............................................................................................................................................. ......................^.^.^.^...^.^.^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.....^.^.....^...^.^.^.^.....^...........^.^.^...................... ............................................................................................................................................. .....................^.^.^.^.^.^...^.^.^.^.^.....^.^...^...^...^.^.^.^.^.....^.^.^.....^.^...^.......^.^.^.....^.^.^.^.^..................... ............................................................................................................................................. ....................^...^.^.......^.^.^.^.^.^.........^.^.^.^.......^.^...^.^.^.^.^.^.^.^.^...^.^.^.....^.^.^.^.^.^.^.^.^.................... ............................................................................................................................................. ...................^.^.......^.^...^.^.^.^.^.^...^.^...^.^...^.....^.^.^...^.^...^.^.^.^.^...^...^.^.....^.^...^.^.^.^.^.^................... ............................................................................................................................................. ..................^.^.........^.^.^...^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.....^...^.^.................. ............................................................................................................................................. .................^.^.^...^.^.^.^.^.......^.^.....^.^...^.^.^...^.^.^...^.^.^.^.^...^.......^.^...^.^.^.........^.^...^.^.^.^................. ............................................................................................................................................. ................^.....^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^...^.^.^.^...^.^.^.^.^.^.....^...^.....^.^................ ............................................................................................................................................. ...............^.....^.^.^.^.^.^.^.^.^.^...^.^...^...^...^...^...^.^.^.^.........^.^.^.^.^...^.....^.^.......^...^...^.^.^.^.^............... ............................................................................................................................................. ..............^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^...^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^...^.^.^.^.^...^.....^.....^.............. ............................................................................................................................................. .............^.^...^.^.....^.......^.....^.....^.^...^.^.^.^.....^.^.....^.....^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^............. ............................................................................................................................................. ............^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^...^.....^.^...^...^.........^.^...^.^...^.^.^.^.^.^.^.^...^............ ............................................................................................................................................. ...........^...^.^.^...^.^...^...^.^...^...^.^.^.^...^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^.....^.^.^.^........... ............................................................................................................................................. ..........^.^.........^.^...^.....^.^.^.^.^.^.^.^...^...^...^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^.^...^.^.^.^...^.....^...^...^.......... ............................................................................................................................................. .........^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.....^...^.^.....^.^...^...^.^...^.^.^.^...^.^...^...^.^.^.^...^...^.....^.^.^...^...^.^......... ............................................................................................................................................. ........^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.........^...^.^.^.^.....^.^...^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^...^.^.^.....^.^.....^.^.^........ ............................................................................................................................................. .......^.......^.^.^.^...^.^...^.^...^.^.^.^.^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^...^.^.^.^.^.^.^...^.^.....^.^.^...^.^.^...^.^.^....... ............................................................................................................................................. ......^...^...^.^.....^.^.^.^.^.^.^...^.^.^.^...^.^.^...^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^.^.^.^.^.^...^.^...... ............................................................................................................................................. .....^.....^.^.^.^.^...^.........^.....^.......^.^.^...^.^.......^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^...^...^.^...^.^.....^.^..... ............................................................................................................................................. ....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^.^.^.^...^.....^.^.....^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^...^...^.... ............................................................................................................................................. ...^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.....^.^.^.^.^.^...^.......^.^...^...^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^.^...^... ............................................................................................................................................. ..^.^.^.^...^.^.^.^.^...^.....^...^.^.^...^...^.^.^.^...^...^.^.^...^...........^.^.^.^.......^...^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.. ............................................................................................................................................. .^.^.^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.....^.^.......^.^.^.^.^...^...^.^.^.^.......^.....^. .............................................................................................................................................