#!/usr/bin/perl $usage = < /dev/null -O - | tr '\r' '\n' | grep '^setCell' | sed -e "s/''/' '/" | cut -d"'" -f2 | tr -d '\n'`; } $data2 = $data; $data2 =~ s/\n//gs; @array = split(//, $data2); $order=sqrt(sqrt($#array+1)); $ordln=length($order**4); print "order: $order\n"; for my $a (0 .. $#array) { $array[$a] = &valueize($array[$a]); $charset{$array[$a]} = 1; } &printdata; # initialize possibilities array for my $a (0 .. $#array) { if ($array[$a] eq " ") { for my $m (keys %charset) { $poss[$a][$m] = 1; } } else { $poss[$a][$array[$a]] = 1; # start aware } } while (1) { &cullposs; &printposs; &findnewnums; } sub cullposs { for my $a (0 .. $#array) { if ($array[$a] eq " ") { for my $m (keys %charset) { ### check vertical columns for (my $b = $a%($order**2); $b <= $#array; $b+=($order**2)) { if ($array[$b] == $m) { $poss[$a][$m] = undef; } } ### check horizontal rows $rowstart = int($a/($order**2))*($order**2); for (my $b = $rowstart; $b < $rowstart+($order**2); $b++) { if ($array[$b] == $m) { $poss[$a][$m] = undef; } } ### check box $boxrow = int($a/($order**3))*($order**3); $boxcolumn = int(($a%($order**2))/$order)*$order; $boxstart = $boxcolumn + $boxrow; # for each row for (my $c = $boxstart; $c < $boxstart+($order**3); $c+=($order**2)) { # $order sequential columns for (my $b = $c; $b < $c+$order; $b++) { if ($array[$b] == $m) { $poss[$a][$m] = undef; } } } } } } } sub findnewnums { $newnum = 0; for my $a (0 .. $#array) { if ($array[$a] eq " ") { # this finds instances where there's only one value in the possibilities list. $found = 0; $foundval = undef; for my $m (keys %charset) { if ($poss[$a][$m]) { $found++; $foundval = $m; } } if ($found == 1) { $array[$a] = $foundval; $poss[$a] = undef; $poss[$a][$foundval] = 1; print &padval($a) . " is " . &digitize($foundval) . " by one possibility left\n"; $newnum++; } # this finds instances where a number in the possibilities list doesn't show up # in the possibilities list for any horizontal, vertical, or box-style area. for my $m (keys %charset) { if($poss[$a][$m]) { ### check vertical columns $found = 0; $foundval = undef; for (my $b = $a%($order**2); $b <= $#array; $b+=($order**2)) { if ($poss[$b][$m]) { $found++; $foundval = $m; } } if ($found == 1) { $array[$a] = $foundval; $poss[$a] = undef; $poss[$a][$foundval] = 1; # &cullposs; print &padval($a) . " is " . &digitize($foundval) . " by a possibility not possible elsewhere vertically\n"; $newnum++; } ### check horizontal rows $found = 0; $foundval = undef; $rowstart = int($a/($order**2))*($order**2); for (my $b = $rowstart; $b < $rowstart+($order**2); $b++) { if ($poss[$b][$m]) { $found++; $foundval = $m; } } if ($found == 1) { $array[$a] = $foundval; $poss[$a] = undef; $poss[$a][$foundval] = 1; # &cullposs; print &padval($a) . " is " . &digitize($foundval) . " by a possibility not possible elsewhere horizontally\n"; $newnum++; } ### check box $found = 0; $foundval = undef; $boxrow = int($a/($order**3))*($order**3); $boxcolumn = int(($a%($order**2))/$order)*$order; $boxstart = $boxcolumn + $boxrow; # for each row for (my $c = $boxstart; $c < $boxstart+($order**3); $c+=($order**2)) { # $order sequential columns for (my $b = $c; $b < $c+$order; $b++) { if ($poss[$b][$m]) { $found++; $foundval = $m; } } } if ($found == 1) { $array[$a] = $foundval; $poss[$a] = undef; $poss[$a][$foundval] = 1; # &cullposs; print &padval($a) . " is " . &digitize($foundval) . " by a possibility not possible elsewhere in the same box\n"; $newnum++; } } } } else { # just to be sure the possibilities table matches up $poss[$a] = undef; $poss[$a][$array[$a]] = 1; } } if ($newnum > 0) { &printdata; } else { exit 1;} } sub printdata { $width = 2; print "+" . scalar(("-" x $width . "+") x $order) x $order; for my $v (0 .. $order-1) { for my $w (0 .. $order-1) { print "\n|"; for my $x (0 .. $order-1) { for my $y (0 .. $order-1) { $char = digitize($array[$v*($order**3) + $w*($order**2) + $x*$order + $y]); print $char . scalar(" " x ($width - length($char))). "|"; } } } print "\n+" . scalar(("-" x $width . "+") x $order) x $order; } print "\n"; } sub printposs { $width = $order**2; print "+" . scalar(("-" x $width . "+") x $order) x $order; for my $v (0 .. $order-1) { for my $w (0 .. $order-1) { print "\n|" . scalar((" " x $width . "|") x $order) x $order; print "\n|"; for my $x (0 .. $order-1) { for my $y (0 .. $order-1) { $char = ""; $cell = $v*($order**3) + $w*($order**2) + $x*$order + $y; if ($array[$cell] eq " ") { for my $m (1 .. ($order**2)) { if ($poss[$cell][$m]) { $char .= &digitize($m); } else { $char .= " "; } } } else { if ($order > 1) { $subchar = "[" . &digitize($array[$cell]) . "]"; } else { $subchar = &digitize($array[$cell]); } $char = (" " x (int($order**2-3)/2)) . $subchar; } print $char . scalar(" " x ($width - length($char))). "|"; } } print "\n|" . scalar(("_" x $width . "|") x $order) x $order; } print "\n+" . scalar(("-" x $width . "+") x $order) x $order; } print "\n"; } sub digitize { my $val = shift; @charray = split(//,' 123456789ABCDEF0GHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#%'); return $charray[$val]; } sub valueize { my $dig = shift; $dig =~ s/0/16/; $dig =~ s/[A-F]/scalar(ord($&)-64+9)/e; $dig =~ s/[G-Z]/scalar(ord($&)-64+10)/e; $dig =~ s/[a-z]/scalar(ord($&)-96+10+26)/e; $dig =~ s/\#/63/; $dig =~ s/\%/64/; return $dig; } sub padval { my $val = shift; return "0" x ($ordln - length($val)) . $val; }