#!/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 $startx; { my $splits=0; my %positions; my %nextpositions; while(my $line=) { chomp($line); push @lines, $line; for(my $i=0; $i= @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"; } } __DATA__ ......................................................................S...................................................................... ............................................................................................................................................. ......................................................................^...................................................................... ............................................................................................................................................. .....................................................................^.^..................................................................... ............................................................................................................................................. ....................................................................^.^.^.................................................................... ............................................................................................................................................. ...................................................................^...^.^................................................................... ............................................................................................................................................. ..................................................................^.^...^.^.................................................................. ............................................................................................................................................. .................................................................^...^.^.^.^................................................................. ............................................................................................................................................. ................................................................^.^.........^................................................................ ............................................................................................................................................. ...............................................................^.^...^.^.^.^.^............................................................... ............................................................................................................................................. ..............................................................^.^.^.^.^.^.....^.............................................................. ............................................................................................................................................. .............................................................^.^.....^...^.^.^.^............................................................. ............................................................................................................................................. ............................................................^...^.....^.^...^.^.^............................................................ ............................................................................................................................................. ...........................................................^.....^.^.^.^.^.^.^.^.^........................................................... ............................................................................................................................................. ..........................................................^...^...^.....^.^...^.^.^.......................................................... ............................................................................................................................................. .........................................................^.^.....^.^.....^.^.^.^.^.^......................................................... ............................................................................................................................................. ........................................................^...^.^.^.^...^.^.....^.....^........................................................ ............................................................................................................................................. .......................................................^.^.^.^.^.^.....^.....^.^.^.^.^....................................................... ............................................................................................................................................. ......................................................^.^.....^.^.^.....^.^...^...^.^.^...................................................... ............................................................................................................................................. .....................................................^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^..................................................... ............................................................................................................................................. ....................................................^.^.^...^.^.^.^.^.^.^.^...^...^.^.^.^.................................................... ............................................................................................................................................. ...................................................^.^...^.^.^.^.^.^...^.^.^.^.^...^...^.^................................................... ............................................................................................................................................. ..................................................^.^.^.^...^.^.^.^.^.^.^...^.^...^.^...^.^.................................................. ............................................................................................................................................. .................................................^.^.^.^...^.^.......^.^.^.^.....^.^.^.^.^.^................................................. ............................................................................................................................................. ................................................^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^................................................ ............................................................................................................................................. ...............................................^.....^...^.^.^.^.^...^...^.^.^.^.^.^.^.^...^.^............................................... ............................................................................................................................................. ..............................................^...^.^...^.^...^.....^.^.^.^.^.^...^.^.^.......^.............................................. ............................................................................................................................................. .............................................^.^.^.^.^...^.^.^.^.^.^...^.....^.^.....^.^.^...^.^............................................. ............................................................................................................................................. ............................................^.^.^.^.^.^.^.^.^.^.^...^.^.....^...^.^...^.^.^.^...^............................................ ............................................................................................................................................. ...........................................^.^.....^.^.....^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.....^........................................... ............................................................................................................................................. ..........................................^.^.^...^.....^.^.^.^.^...^.^.....^.^.^.^...^...^.^...^.^.......................................... ............................................................................................................................................. .........................................^.^...^.^.^.^.^.^.^.^.^.^.^.....^.....^.^.^.^.^.^.^.^.^.^.^......................................... ............................................................................................................................................. ........................................^.....^.^...^.^.^.^.....^.^.^.^...^...^.^.^.^.^...^.^...^.^.^........................................ ............................................................................................................................................. .......................................^.^.^.^.....^.^.^.......^.^.........^...^.^.^.^.....^.^.....^.^....................................... ............................................................................................................................................. ......................................^.^.....^.^.^...^.^...^.^.......^.^...^.^.^.^.^.^...^.....^.^...^...................................... ............................................................................................................................................. .....................................^.^.^...^.^.^.^.^.....^.^...^.....^.....^...^.^...^.......^.^.^.^.^..................................... ............................................................................................................................................. ....................................^.^.^...^...^.^.^...^.^...^.^.^.^.......^.....^.....^.^.......^.^.^.^.................................... ............................................................................................................................................. ...................................^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^...^.^...^.^.^.^.^...^.^.^................................... ............................................................................................................................................. ..................................^...^.^.^.^...^.......^.^.^...^.^.......^.^.^.....^...^...^.^.^...^.^...^.................................. ............................................................................................................................................. .................................^...^.......^.^.^.....^.^...^.^...^.^.^.^.^.^.....^.^...^...^.^.^...^...^.^................................. ............................................................................................................................................. ................................^.^.^...^.^.^.^.^.^.^...^...^.^.......^.^...^.^.^...^.^...^.^.^.^.^...^...^.^................................ ............................................................................................................................................. ...............................^.^.....^.^.^...^.^.^.^...^.........^.^.^.^.........^.^.^...........^.^.^.^...^............................... ............................................................................................................................................. ..............................^.^...^.^...^...^...^...^.^.^.^.^.^...^.^.^...^.^.^...^.....^.^.^...^...^.^.^...^.............................. ............................................................................................................................................. .............................^.^.^.^.^.^.^.....^.^.^.^.^.^.....^...^.^.^.......^.^.^.^.....^.........^.^.^.....^............................. ............................................................................................................................................. ............................^...^...^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^.......^.^.....^.^.^.^...^.^...^...^............................ ............................................................................................................................................. ...........................^.^.^...^.^.^.....^.....^.^.......^.^.^.....^.^.^...^.....^...^.^.^.^.....^.^.^.^.^.^.^........................... ............................................................................................................................................. ..........................^.....^.....^.^...^.^.^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.^.^.....^.^.^...^.^...^...^.......................... ............................................................................................................................................. .........................^.^...^.^.^.^...^.^...^.^.........^.......^.^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^...^......................... ............................................................................................................................................. ........................^...^.^.^.^...^.^...^...^.^...^.^.^...^.^.^.^.^.^...^.^.^...^.......^.^.^.^.^...^.....^...^.^........................ ............................................................................................................................................. .......................^.^.^...^.^.^.....^.^...^.^.^...^...^.^.^...^.^.^...^.^.....^.^.^.^.^...^.^.........^.^.^.^.^.^....................... ............................................................................................................................................. ......................^.^.^.^...^.^.^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.....^.^.....^...^.^.^.^.....^...........^.^.^...................... ............................................................................................................................................. .....................^.^.^.^.^.^...^.^.^.^.^.....^.^...^...^...^.^.^.^.^.....^.^.^.....^.^...^.......^.^.^.....^.^.^.^.^..................... ............................................................................................................................................. ....................^...^.^.......^.^.^.^.^.^.........^.^.^.^.......^.^...^.^.^.^.^.^.^.^.^...^.^.^.....^.^.^.^.^.^.^.^.^.................... ............................................................................................................................................. ...................^.^.......^.^...^.^.^.^.^.^...^.^...^.^...^.....^.^.^...^.^...^.^.^.^.^...^...^.^.....^.^...^.^.^.^.^.^................... ............................................................................................................................................. ..................^.^.........^.^.^...^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.....^...^.^.................. ............................................................................................................................................. .................^.^.^...^.^.^.^.^.......^.^.....^.^...^.^.^...^.^.^...^.^.^.^.^...^.......^.^...^.^.^.........^.^...^.^.^.^................. ............................................................................................................................................. ................^.....^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^...^.^.^.^...^.^.^.^.^.^.....^...^.....^.^................ ............................................................................................................................................. ...............^.....^.^.^.^.^.^.^.^.^.^...^.^...^...^...^...^...^.^.^.^.........^.^.^.^.^...^.....^.^.......^...^...^.^.^.^.^............... ............................................................................................................................................. ..............^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^...^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^...^.^.^.^.^...^.....^.....^.............. ............................................................................................................................................. .............^.^...^.^.....^.......^.....^.....^.^...^.^.^.^.....^.^.....^.....^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^............. ............................................................................................................................................. ............^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^...^.....^.^...^...^.........^.^...^.^...^.^.^.^.^.^.^.^...^............ ............................................................................................................................................. ...........^...^.^.^...^.^...^...^.^...^...^.^.^.^...^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^.....^.^.^.^........... ............................................................................................................................................. ..........^.^.........^.^...^.....^.^.^.^.^.^.^.^...^...^...^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^.^...^.^.^.^...^.....^...^...^.......... ............................................................................................................................................. .........^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.....^...^.^.....^.^...^...^.^...^.^.^.^...^.^...^...^.^.^.^...^...^.....^.^.^...^...^.^......... ............................................................................................................................................. ........^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.........^...^.^.^.^.....^.^...^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^...^.^.^.....^.^.....^.^.^........ ............................................................................................................................................. .......^.......^.^.^.^...^.^...^.^...^.^.^.^.^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^...^.^.^.^.^.^.^...^.^.....^.^.^...^.^.^...^.^.^....... ............................................................................................................................................. ......^...^...^.^.....^.^.^.^.^.^.^...^.^.^.^...^.^.^...^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^.^.^.^.^.^...^.^...... ............................................................................................................................................. .....^.....^.^.^.^.^...^.........^.....^.......^.^.^...^.^.......^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^...^...^.^...^.^.....^.^..... ............................................................................................................................................. ....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^.^.^.^...^.....^.^.....^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^...^...^.... ............................................................................................................................................. ...^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.....^.^.^.^.^.^...^.......^.^...^...^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^.^...^... ............................................................................................................................................. ..^.^.^.^...^.^.^.^.^...^.....^...^.^.^...^...^.^.^.^...^...^.^.^...^...........^.^.^.^.......^...^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.. ............................................................................................................................................. .^.^.^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.....^.^.......^.^.^.^.^...^...^.^.^.^.......^.....^. .............................................................................................................................................