diff options
Diffstat (limited to '2011/qual/d/d.py')
| -rw-r--r-- | 2011/qual/d/d.py | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/2011/qual/d/d.py b/2011/qual/d/d.py index 0bdf8dd..d978a3c 100644 --- a/2011/qual/d/d.py +++ b/2011/qual/d/d.py @@ -6,31 +6,28 @@ def solve(a): # stop if solved while a != sol: print "%s is not %s" % (a, sol) - + flipped = False # first flip all pairs for i in range(len(a)): if a[i] != i+1 and a[a[i]-1] == i+1: print 'flipping %d and %d' % (a[a[i]-1], a[i]) - t = a[i] - a[i] = a[a[i]-1] - print 'flipping %d and %d' % (a[a[i]-1], a[i]) - a[a[i]-1] = t - print 'flipping %d and %d' % (a[a[i]-1], a[i]) - break + t = a[a[i]-1] + a[a[i]-1] = a[i] + a[i] = t steps += 2 - - # did that suffice? - if a == sol: - print "%s is not %s" % (a, sol) - break + flipped = True + + if flipped: + continue # now find the first mixed index to fix for i in range(len(a)): if a[i] != i+1: print 'flipping %d and %d' % (a[a[i]-1], a[i]) - a[i], a[a[i]-1] = a[a[i]-1], a[i] + t = a[a[i]-1] + a[a[i]-1] = a[i] + a[i] = t steps += 2 - sleep(1) return steps |
