Even if the code compiled, it wouldn't solve a game of Sudoku. Actually, all it does is to set the 9 variables bN
to true if any of the 81 variables aPQ
are equal to N
.
And it doesn't even do this efficiently. There are 1458 (=18*81) conditions setting each of the bN
variables to true. (Simple check: each of the conditions is 3 lines; 1458 checks for each of 9 variables: 3 * 1458 * 9 = 39366, the approximate length of the file).
All of the setters of bN
are independent, and are idempotent, so they can be arbitrarily rearranged and the 17 repeated checks of the conditions can be removed.
An equivalent (and adequately efficient) version of this code - using arrays - is:
// Using 10 as array size, as OP's code is one-based;
// first element is unused.
int a[][] = new int[10][10];
// Initialize the elements of a.
boolean b[] = new boolean[10];
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (a[i][j] >= 1 && a[i][j] <= 9) {
b[a[i][j]] = true;
}
}
}
which should fit inside the maximum size of a method quite easily.
You should focus on writing correct, maintainable code, before considering how to make it efficient - this code doesn't work for its stated purpose, and I would not want to be the one working out where the bug in 40k lines of code is. The only reason I was able to analyse this much code is that it appears to be generated, as it is very uniform in its pattern.
I did the analysis above using a (very hacky) Python script.
Run using:
curl http://pastebin.com/raw/NbyTTAdX | python script.py
script.py
:
import sys
import re
with open('/dev/stdin') as fh:
lines = fh.readlines()
bequals = re.compile(r'^b\d\s*= true;$')
i = 0
bvariablesetters = {}
while i < len(lines):
if lines[i].strip().startswith('if (') and lines[i].strip().endswith('{'):
# Match the conditionals setting one of the b variables.
if lines[i+2].strip() == '}' and bequals.search(lines[i+1].strip()):
newline = ' '.join(map(str.strip, lines[i:i+3]))
spl = newline.split()
# This is the "b=" variable
bvar = spl[5]
bvariablesetters.setdefault(bvar, []).append(' '.join(newline))
i += 3
continue
else:
# Print out lines which don't match the conditional-set-b pattern, so you
# can see that there's nothing else going on.
sys.stdout.write(lines[i])
i += 1
# Print the number of conditionals setting each of the b variables.
print {(k, len(v)) for k, v in bvariablesetters.iteritems()}
# Print the number of unique conditionals setting each of the b variables.
print {(k, len(set(v))) for k, v in bvariablesetters.iteritems()}
# Print one of the lists of conditions to set a b variable.
print bvariablesetters['b1=']
# Print one of the sets of conditions to set a b variable.
print sorted(set(bvariablesetters['b1=']))