Originally posted by Rajesh V
Imagine a row 1 2 3 4 5 6 6 9 9
All you have to do is increase one number from a row, and decrease another from same row and bingo you have sum 45.
### sudotest.pl ### #reading the lines in the file# open(SUDOKU,"sudoku.txt") or die "No such file !"; for($i=0;$i<9;$i++){ $myline[$i]=<SUDOKU>; chomp $myline[$i]; #this cuts away the newline char } close(SUDOKU); # print $myline[0]; #this was for debugging # and now let's check a line check($myline[3]); sub check { $line=$_[0]; # this is cryptic for non-perl-users: # perl passes the parameters to the functions # as an array called @_; $_[0] is the first # and in this case the one and only item of @_ #let us split the line into the array @digit @digit = split(//,$line); #and now start with digit-by-digit check foreach $digit(@digit){ $flag=0; foreach $item(@digit){ if($digit==$item){$flag++;} #print $flag; #this for debugging only if($flag>1){die "Invalid Sudoku!"}; } } }
Comment