1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#!/usr/local/bin/python3.8

from random import randrange, seed
from copy import deepcopy
from itertools import combinations 
import datetime

Same_all_day = False

def shuffle(a):
	 i = len(a)
	 while i > 1:
		 i -= 1
		 j = randrange(i + 1) # Fisher-Yates, not Sattolo
		 a[j], a[i] = a[i], a[j]

def add_2_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if i == gd_sz - 1:
			continue
		if gd[i][j]  + gd[i + 1][j] == a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0:
				kk[i][j] = kk[i + 1][j] = '  v{0:02d}+  '.format(a)
				s.append([(i, j), (i + 1, j)])
				k += 1
				if k == count:
					break

def add_2_horz_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if j == gd_sz - 1:
			continue
		if gd[i][j] + gd[i][j + 1] == a:
			if kk[i][j] == 0 and kk[i][j + 1] == 0:
				kk[i][j] = kk[i][j + 1] = '  h{0:d}+   '.format(a)
				s.append([(i, j), (i, j + 1)])
				k += 1
				if k == count:
					break

def add_3_horz_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if j >= gd_sz - 2:
			continue
		if gd[i][j] + gd[i][j + 1] + gd[i][j + 2] == a:
			if kk[i][j] == 0 and kk[i][j + 1] == 0 and kk[i][j + 2] == 0:
				kk[i][j] = kk[i][j + 1] = kk[i][j + 2] = '  3h{0:02d}+ '.format(a)
				s.append([(i, j), (i, j + 1)])
				s.append([(i, j), (i, j + 2)])
				s.append([(i, j + 1), (i, j + 2)])
				k += 1
				if k == count:
					break

def add_3_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if i >= gd_sz - 2:
			continue
		if gd[i][j] + gd[i + 1][j] + gd[i + 2][j] == a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i + 2][j] == 0:
				kk[i][j] = kk[i + 1][j] = kk[i + 2][j] = '  3v{0:02d}+ '.format(a)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i + 2, j)])
				s.append([(i + 1, j), (i + 2, j)])
				k += 1
				if k == count:
					break

def add_2_horz_3_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i > gd_sz - 3) or (j == gd_sz - 1):
			continue
		if gd[i][j]  + gd[i + 1][j] + gd[i + 1][j + 1] + gd[i + 2][j + 1] == a:
				if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i + 1][j + 1] == 0 and kk[i + 2][j + 1] == 0:
					kk[i][j] = kk[i + 1][j] = kk[i + 1][j + 1] = kk[i + 2][j + 1] = ' hv{0:03d}+ '.format(a)
					s.append([(i, j), (i + 1, j)])
					s.append([(i, j), (i + 1, j + 1)])
					s.append([(i, j), (i + 2, j + 1)])
					s.append([(i + 1, j), (i + 1, j + 1)])
					s.append([(i + 1, j), (i + 2, j + 1)])
					s.append([(i + 1, j + 1), (i + 2, j + 1)])
					k += 1
					if k == count:
						break

# Note that a is an array of allowed products of four integers
def multiply_2_square_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]
	
	for pr in rand_mt:
		i, j = pr
		if (i == gd_sz - 1) or (j == gd_sz - 1):
			continue
		if gd[i][j] * gd[i + 1][j] * gd[i][j + 1] * gd[i + 1][j + 1] in a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i][j + 1] == 0 and kk[i + 1][j + 1] == 0:
				temp = gd[i][j] * gd[i + 1][j] * gd[i][j + 1] * gd[i + 1][j + 1]
				kk[i][j] = kk[i + 1][j] = kk[i][j + 1] = kk[i + 1][j + 1] ='  sq{0:02d}x '.format(temp)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i, j + 1)])
				s.append([(i, j), (i + 1, j + 1)])
				s.append([(i + 1, j), (i, j + 1)])
				s.append([(i + 1, j), (i + 1, j + 1)])
				s.append([(i, j + 1), (i + 1, j + 1)])
				k += 1
				if k == count:
					break

def show(puzzle):
	for a_row in puzzle:
		for el in a_row:
			print(el, end = '  ')
		print()

def legit_row(arr):
	global gd_sz, digs
	result = True
	
	for k in digs:
		counted = False
		for el in arr:
			if el == k:
				if counted:
					result = False
					break
				counted = True
		if not counted:
			result = False
		if not result:
			break
	return result

def legit_col(gd, j):
	global gd_sz
	arr = []

	for row in gd:
		arr.append(row[j])

	return legit_row(arr)

def legit_gd(gd):
	global gd_sz
	result = True

	for row in gd:
		if not legit_row(row):
			result = False
			break
	if result:
		for j in range(gd_sz):
			if not legit_col(gd, j):
				result = False
				break
	return result

def subtract_2_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if i == gd_sz - 1:
			continue
		if abs(gd[i][j] - gd[i + 1][j]) == a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0:
				kk[i][j] = kk[i + 1][j] = '  v{0:d}-   '.format(a)
				s.append([(i, j), (i + 1, j)])
				k += 1
				if k == count:
					break

def subtract_2_horz_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if j == gd_sz - 1:
			continue
		if abs(gd[i][j] - gd[i][j + 1]) == a:
			if kk[i][j] == 0 and kk[i][j + 1] == 0:
				kk[i][j] = kk[i][j + 1] = '  h{0:d}-   '.format(a)
				s.append([(i, j), (i, j + 1)])
				k += 1
				if k == count:
					break
					
# Note that a is an array of allowed products of 2 integers
def multiply_2_horz_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if j == gd_sz - 1:
			continue
		if gd[i][j] * gd[i][j + 1] in a:
			if kk[i][j] == 0 and kk[i][j + 1] == 0:
				kk[i][j] = kk[i][j + 1] = '  h{0:02d}x  '.format(gd[i][j] * gd[i][j + 1])
				s.append([(i, j), (i, j + 1)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 2 integers
def multiply_2_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if i == gd_sz - 1:
			continue
		if gd[i][j] * gd[i + 1][j] in a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0:
				kk[i][j] = kk[i + 1][j] = '  v{0:02d}x  '.format(gd[i][j] * gd[i + 1][j])
				s.append([(i, j), (i + 1, j)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 3 integers
def multiply_3_horz_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if j >= gd_sz - 2:
			continue
		if gd[i][j] * gd[i][j + 1] * gd[i][j + 2] in a:
			if kk[i][j] == 0 and kk[i][j + 1] == 0 and kk [i][j + 2] == 0:
				temp = gd[i][j] * gd[i][j + 1] * gd[i][j + 2]
				kk[i][j] = kk[i][j + 1] = kk[i][j + 2] = '  3h{0:02d}x '.format(temp)
				s.append([(i, j), (i, j + 1)])
				s.append([(i, j), (i, j + 2)])
				s.append([(i, j + 1), (i, j + 2)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 3 integers
def multiply_3_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if i >= gd_sz - 2:
			continue
		if gd[i][j] * gd[i + 1][j] * gd[i + 2][j] in a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i + 2][j]== 0:
				temp = gd[i][j] * gd[i + 1][j] * gd[i + 2][j]
				kk[i][j] = kk[i + 1][j] = kk[i + 2][j] = '  3v{0:02d}x '.format(temp)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i + 2, j)])
				s.append([(i + 1, j), (i + 2, j)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 3 integers
def multiply_ll_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == 0) or (j == gd_sz - 1):
			continue
		if gd[i][j] * gd[i - 1][j] * gd[i][j + 1] in a:
			if kk[i][j] == 0 and kk[i - 1][j] == 0 and kk[i][j + 1] == 0:
				temp = gd[i][j] * gd[i - 1][j] * gd[i][j + 1]
				kk[i][j] = kk[i - 1][j] = kk[i][j + 1] = ' ll{0:02d}x  '.format(temp)
				s.append([(i, j), (i - 1, j)])
				s.append([(i, j), (i, j + 1)])
				s.append([(i - 1, j), (i, j + 1)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 3 integers
def multiply_ul_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == gd_sz - 1) or (j == gd_sz - 1):
			continue
		if gd[i][j] * gd[i + 1][j] * gd[i][j + 1] in a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i][j + 1] == 0:
				temp = gd[i][j] * gd[i + 1][j] * gd[i][j + 1]
				kk[i][j] = kk[i + 1][j] = kk[i][j + 1] = ' ul{0:02d}x  '.format(temp)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i, j + 1)])
				s.append([(i + 1, j), (i, j + 1)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 3 integers
def multiply_ur_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == gd_sz - 1) or (j == 0):
			continue
		if gd[i][j] * gd[i][j - 1] * gd[i + 1][j] in a:
			if kk[i][j] == 0 and kk[i][j - 1] == 0 and kk[i + 1][j] == 0:
				temp = gd[i][j] * gd[i][j - 1] * gd[i + 1][j]
				kk[i][j] = kk[i][j - 1] = kk[i + 1][j] = ' ur{0:02d}x  '.format(temp)
				s.append([(i, j), (i, j - 1)])
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j - 1), (i + 1, j)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of 3 integers
def multiply_lr_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == 0) or (j == 0):
			continue
		if gd[i][j] * gd[i][j - 1] * gd[i - 1][j] in a:
			if kk[i][j] == 0 and kk[i][j - 1] == 0 and kk[i - 1][j] == 0:
				temp = gd[i][j] * gd[i][j - 1] * gd[i - 1][j]
				kk[i][j] = kk[i][j - 1] = kk[i - 1][j] = ' lr{0:03d}x '.format(temp)
				s.append([(i, j), (i, j - 1)])
				s.append([(i, j), (i - 1, j)])
				s.append([(i, j - 1), (i - 1, j)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed products of four integers
def multiply_3_vert_2_horz(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]
	
	for pr in rand_mt:
		i, j = pr
		if (i > gd_sz - 3) or (j == gd_sz - 1):
			continue
		if gd[i][j] * gd[i + 1][j] * gd[i + 2][j] * gd[i + 2][j + 1] in a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i + 2][j] == 0 and kk[i + 2][j + 1] == 0:
				temp = gd[i][j] * gd[i + 1][j] * gd[i + 2][j] * gd[i + 2][j + 1]
				kk[i][j] = kk[i + 1][j] = kk[i + 2][j] = kk[i + 2][j + 1] ='  vh{0:03d}x'.format(temp)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i + 2, j)])
				s.append([(i, j), (i + 2, j + 1)])
				s.append([(i + 1, j), (i + 2, j)])
				s.append([(i + 1, j), (i + 2, j + 1)])
				s.append([(i + 2, j), (i + 2, j + 1)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed ratios of two integers
def divide_2_horz_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if j == gd_sz - 1:
			continue
		rat = max(gd[i][j], gd[i][j + 1]) / min(gd[i][j], gd[i][j + 1])
		if (rat in a) and (kk[i][j] == 0 and kk[i][j + 1] == 0):
			kk[i][j] = kk[i][j + 1] = '  h{0:1.0f}\u00F7   '.format(rat)
			s.append([(i, j), (i, j + 1)])
			k += 1
			if k == count:
				break

def divide_2_vert_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]
	
	for pr in rand_mt:
		i, j = pr
		if i == gd_sz - 1:
			continue
		rat = max(gd[i][j], gd[i + 1][j]) / min(gd[i][j], gd[i + 1][j])
		if (rat in a) and (kk[i][j] == 0 and kk[i + 1][j] == 0):
			kk[i][j] = kk[i + 1][j] = '  v{0:1.0f}\u00F7   '.format(rat)
			s.append([(i, j), (i + 1, j)])
			k += 1
			if k == count:
				break

def add_ur_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == gd_sz - 1) or (j == 0):
			continue
		if gd[i][j] + gd[i][j - 1] + gd[i + 1][j] == a:
			if kk[i][j] == 0 and kk[i][j - 1] == 0 and kk[i + 1][j] == 0:
				kk[i][j] = kk[i][j - 1] = kk[i + 1][j] = ' ur{0:02d}+  '.format(a)
				s.append([(i, j), (i, j - 1)])
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j - 1), (i + 1, j)])
				k += 1
				if k == count:
					break

def add_ul_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == gd_sz - 1) or (j == gd_sz - 1):
			continue
		if gd[i][j] + gd[i + 1][j] + gd[i][j + 1] == a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i][j + 1] == 0:
				kk[i][j] = kk[i + 1][j] = kk[i][j + 1] = ' ul{0:02d}+  '.format(a)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i, j + 1)])
				s.append([(i + 1, j), (i, j + 1)])
				k += 1
				if k == count:
					break

def add_ll_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == 0) or (j == gd_sz - 1):
			continue
		if gd[i][j] + gd[i - 1][j] + gd[i][j + 1] == a:
			if kk[i][j] == 0 and kk[i - 1][j] == 0 and kk[i][j + 1] == 0:
				kk[i][j] = kk[i - 1][j] = kk[i][j + 1] = ' ll{0:02d}+  '.format(a)
				s.append([(i, j), (i - 1, j)])
				s.append([(i, j), (i, j + 1)])
				s.append([(i - 1, j), (i, j + 1)])
				k += 1
				if k == count:
					break

def add_lr_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]

	for pr in rand_mt:
		i, j = pr
		if (i == 0) or (j == 0):
			continue
		if gd[i][j] + gd[i][j - 1] + gd[i - 1][j] == a:
			if kk[i][j] == 0 and kk[i][j - 1] == 0 and kk[i - 1][j] == 0:
				kk[i][j] = kk[i][j - 1] = kk[i - 1][j] = ' lr{0:02d}+  '.format(a)
				s.append([(i, j), (i, j - 1)])
				s.append([(i, j), (i - 1, j)])
				s.append([(i, j - 1), (i - 1, j)])
				k += 1
				if k == count:
					break

# Note that a is an array of allowed sums of four integers
def add_2_square_target(count, indexer, gd, kk, s, a):
	global gd_sz
	k = 0

	shuffle(indexer)
	rand_mt = [divmod(el, gd_sz) for el in indexer]
	
	for pr in rand_mt:
		i, j = pr
		if (i == gd_sz - 1) or (j == gd_sz - 1):
			continue
		if gd[i][j] + gd[i + 1][j] + gd[i][j + 1] + gd[i + 1][j + 1] in a:
			if kk[i][j] == 0 and kk[i + 1][j] == 0 and kk[i][j + 1] == 0 and kk[i + 1][j + 1] == 0:
				temp = gd[i][j] + gd[i + 1][j] + gd[i][j + 1] + gd[i + 1][j + 1]
				kk[i][j] = kk[i + 1][j] = kk[i][j + 1] = kk[i + 1][j + 1] ='  sq{0:02d}+ '.format(temp)
				s.append([(i, j), (i + 1, j)])
				s.append([(i, j), (i, j + 1)])
				s.append([(i, j), (i + 1, j + 1)])
				s.append([(i + 1, j), (i, j + 1)])
				s.append([(i + 1, j), (i + 1, j + 1)])
				s.append([(i, j + 1), (i + 1, j + 1)])
				k += 1
				if k == count:
					break

def gimmes(gd, kk):
	count = 0
	for i in range(gd_sz):
		for j in range(gd_sz):
			if kk[i][j] == 0:
				count += 1
				kk[i][j] = '   {0:d}    '.format(gd[i][j])
	return count

# Begin program

# If program repeatability is needed, seed the generator with the Julian day.
if Same_all_day:
	dt = datetime.datetime.now()
	seed(int(dt.strftime("%j")))

gd_sz = 6
row_col_sum = (gd_sz * (gd_sz + 1)) // 2
digs = [k for k in range(1, gd_sz + 1)]
a_row = [i for i in range(1, gd_sz + 1)]
shuffle(a_row)
build_count = 0
indexer = [i for i in range(gd_sz * gd_sz)] # For accessing random gd elements

while True:
	build_count += 1
	print('Starting puzzle build number {0:d}.\n'.format(build_count))
	gd, row_count = [a_row], 1

	while row_count < gd_sz - 1:
		next_row = deepcopy(a_row)
		shuffle(next_row)
		result = True
		for a_row in gd:
			for i in range(gd_sz):
				if next_row[i] == a_row[i]:
					result = False
					break
			if result == False:
				break
		if result == True:
			gd.append(next_row)
			row_count += 1

	# Now the last row. Just choose the integers that make the sum for each
	# column correct.
	a_row = []
	for j in range(gd_sz):
		acc = 0
		for i in range(gd_sz - 1):
			acc += gd[i][j]
		a_row.append(row_col_sum - acc)

	gd.append(a_row)
	show(gd)
	print()
	found_a_dup = False

	# Set up a KenKen grid and fill it with sentinels.
	kk = []
	for _ in range(gd_sz):
		row = [0 for _ in range(gd_sz)] # Unused square sentinel
		kk.append(row)

	s = []
	multiply_3_vert_2_horz(1, indexer, gd, kk, s, [24, 72])
#	multiply_2_square_target(1, indexer, gd, kk, s, [24, 72])
	multiply_lr_target(1, indexer, gd, kk, s, [25, 50, 75, 100, 125, 150])
	multiply_3_vert_target(1, indexer, gd, kk, s, [40, 48])
	add_lr_target(1, indexer, gd, kk, s, 16)
	subtract_2_horz_target(2, indexer, gd, kk, s, 5)
	add_2_horz_3_vert_target(1, indexer, gd, kk, s, 14)
	add_3_horz_target(1, indexer, gd, kk, s, 12)
	add_3_vert_target(1, indexer, gd, kk, s, 13)
	subtract_2_vert_target(1, indexer, gd, kk, s, 5)
	subtract_2_vert_target(1, indexer, gd, kk, s, 4)
	subtract_2_horz_target(1, indexer, gd, kk, s, 3)
	subtract_2_horz_target(1, indexer, gd, kk, s, 1)
	subtract_2_vert_target(1, indexer, gd, kk, s, 2)
	subtract_2_horz_target(1, indexer, gd, kk, s, 2)
	subtract_2_vert_target(2, indexer, gd, kk, s, 1)
	multiply_2_horz_target(1, indexer, gd, kk, s, [6, 30])
	multiply_2_vert_target(1, indexer, gd, kk, s, [6, 20])
	multiply_3_horz_target(1, indexer, gd, kk, s, [10, 24, 40])
	divide_2_horz_target(2, indexer, gd, kk, s, [2, 3])
	divide_2_vert_target(2, indexer, gd, kk, s, [2, 3])
	add_2_vert_target(1, indexer, gd, kk, s, 7)
	add_2_horz_target(1, indexer, gd, kk, s, 5)
	add_ur_target(1, indexer, gd, kk, s, 9)
	divide_2_vert_target(1, indexer, gd, kk, s, [4, 5, 6])
	divide_2_horz_target(1, indexer, gd, kk, s, [4, 5, 6])
	# print(s)
	lst = [i for i in range(len(s))]
	comb = combinations(lst, 2)
	c = (list(comb))
	print('There are {0:d} combinations of swaps taken 2 at a time.'.format(len(c)))
	for i in c:
		(prs) = s[i[0]], s[i[1]]
		g2 = deepcopy(gd)
		for p in prs:
			temp = g2[p[0][0]][p[0][1]]
			g2[p[0][0]][p[0][1]] = g2[p[1][0]][p[1][1]]
			g2[p[1][0]][p[1][1]] = temp
		found_a_dup = legit_gd(g2)
		if found_a_dup:
			print('Found a duplicate with combinations of 2 pairs.\n')
			break
	if found_a_dup:
		continue
	comb = combinations(lst, 3)
	c = (list(comb))
	print('There are {0:d} combinations of swaps taken 3 at a time.'.format(len(c)))
	for i in c:
		(prs) = s[i[0]], s[i[1]], s[i[2]]
		g2 = deepcopy(gd)
		for p in prs:
			temp = g2[p[0][0]][p[0][1]]
			g2[p[0][0]][p[0][1]] = g2[p[1][0]][p[1][1]]
			g2[p[1][0]][p[1][1]] = temp
		found_a_dup = legit_gd(g2)
		if found_a_dup:
			print('Found a duplicate with combinations of 3 pairs.\n')
			break
	if found_a_dup:
		continue
	comb = combinations(lst, 4)
	c = (list(comb))
	print('There are {0:d} combinations of swaps taken 4 at a time.\n'.format(len(c)))
	for i in c:
		(prs) = s[i[0]], s[i[1]], s[i[2]], s[i[3]]
		g2 = deepcopy(gd)
		for p in prs:
			temp = g2[p[0][0]][p[0][1]]
			g2[p[0][0]][p[0][1]] = g2[p[1][0]][p[1][1]]
			g2[p[1][0]][p[1][1]] = temp
		found_a_dup = legit_gd(g2)
		if found_a_dup:
			print('Found a duplicate with combinations of 4 pairs.\n')
			break
	if not found_a_dup:
		break;

gimme_count = gimmes(gd, kk)
show(kk)
if gimme_count == 1:
	s_g = ''
else:
	s_g = 's'
print('\n{0:d} gimme{1:s}'.format(gimme_count, s_g))
print('\nGood to go.')