I got 7.5/9, but took closer to 1.5 hours rather than 35 mins.
Here is python for 4
def four():
arr = [
['', '', 'a1', ''],
['', '', '', ''],
['b2', '', '', ''],
['', 'c3', '', 'b1']
]
possible = [x + y for x in ['a', 'b', 'c', 'd'] for y in ['1', '2', '3', '4']]
def validate(mat):
# validate rows
for row in mat:
all_letters = [x[0] for x in row if len(x) == 2]
if len(all_letters) > len(set(all_letters)):
return False
all_numbers = [x[1] for x in row if len(x) == 2]
if len(all_numbers) > len(set(all_numbers)):
return False
# validate columns
for i in range(4):
col = [row[i] for row in mat]
all_letters = [x[0] for x in col if len(x) == 2]
if len(all_letters) > len(set(all_letters)):
return False
all_numbers = [x[1] for x in col if len(x) == 2]
if len(all_numbers) > len(set(all_numbers)):
return False
all_placed = [x for row in arr for x in row if len(x) == 2]
return len(all_placed) == len(set(all_placed))
def f(i, j):
if i == 4:
return True
if j == 3:
next_i = i + 1
next_j = 0
else:
next_i = i
next_j = j + 1
if len(arr[i][j]) == 2:
ans = f(next_i, next_j)
return ans
for val in possible:
arr[i][j] = val
if validate(arr):
ans = f(next_i, next_j)
if ans:
return True
arr[i][j] = ''
return False
f(0, 0)
for row in arr:
print(row)
return arr[0][0]