Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Weekly Challenges

Solve the challenge, share your solution and summit the ranks of our Community!

Also available in | Français | Português | Español | 日本語
IDEAS WANTED

Want to get involved? We're always looking for ideas and content for Weekly Challenges.

SUBMIT YOUR IDEA

Challenge #200: Sudoku Solver

patel_bm
8 - Asteroid

Here's my beginner solution.

 

Will need to spend further time to get other two solutions.

deepaprash
8 - Asteroid

Beginner was easy.

dpencheva
8 - Asteroid

Here is my solution but only for the Beginner level. I will make the rest of the task during the weekend and will update my post as well. Very challenging task! 

LiD
8 - Asteroid

Naming it as entering the death zone isn't inadequate, it was not easy, even the intermediate one.

Spoiler
LiD_0-1587934282847.png

 

AndrewS
11 - Bolide

#200 - beginner for now.

Spoiler
200_beginner.PNG

 

jgosalia
7 - Meteor

Spoiler- beginner solution

jarrod
ACE Emeritus
ACE Emeritus

This one was easy to comprehend in Alteryx (if you understand sudoku) but took quite a few tools.

on the flip side, the python solutions i've found and ultimately used can be a lot more tidy, but require a deeper understanding of data manipulation that isn't nearly as intuitive as alteryx. 

Spoiler
# List all non-standard packages to be imported by your 
# script here (only missing packages will be installed)
from ayx import Package
#Package.installPackages(['pandas','numpy'])

from ayx import Alteryx
import numpy as np
import pandas as pd
import time

# This is the Beginner Solution

dfBeginner = Alteryx.read("#1")
dfBeginner = dfBeginner['quizzes'].str.extract('(.{9})'*9)
dfBeginner = np.transpose(dfBeginner)
dfBeginner = dfBeginner[0].str.extract('(.{1})'*9)
# print(dfBeginner)
Alteryx.write(dfBeginner,1)

# This is the start of the Intermediate/Advanced Solutions

dfInput = Alteryx.read("#1")
dfInput = dfInput['quizzes'].str.extract('(.{9})'*9)
dfInput = np.transpose(dfInput)
# the .astype(int) is necessary for turning the data into an array
dfInput = dfInput[0].str.extract('(.{1})'*9).astype(int)
dfInput = dfInput.to_numpy()
container = dfInput

# This is not my code below, but i found it intuitive although i'm still digging into how it all works.

###  Defining Functions  ###
subtract_set = {1,2,3,4,5,6,7,8,9}

def check_horizontal(i,j):
    return subtract_set - set(container[i])

def check_vertical(i,j):
    ret_set = []
    for x in range(9):
        ret_set.append(container[x][j])
    return subtract_set - set(ret_set)

def check_square(i,j):
    first = [0,1,2]
    second = [3,4,5]
    third = [6,7,8]
    find_square = [first,second,third]
    for l in find_square:
        if i in l:
            row = l
        if j in l:
            col = l
    ret_set = []
    for x in row:
        for y in col:
            ret_set.append(container[x][y])
    return subtract_set - set(ret_set)

def get_poss_vals(i,j):
    poss_vals = list(check_square(i,j).intersection(check_horizontal(i,j)).intersection(check_vertical(i,j)))
    return poss_vals

def explicit_solver(container):
    stump_count = 1
    for i in range(9):
        for j in range(9):
            if container[i][j] == 0:
                poss_vals = get_poss_vals(i,j)
                if len(poss_vals) == 1:
                    container[i][j] = list(poss_vals)[0]
                    print_container(container)
                    stump_count = 0
    return container, stump_count

def implicit_solver(i,j,container):
    if container[i][j] == 0:
        poss_vals = get_poss_vals(i,j)
        
        #check row
        row_poss = []
        for y in range(9):
            if y == j:
                continue
            if container[i][y] == 0:
                for val in get_poss_vals(i,y):
                    row_poss.append(val)
        if len(set(poss_vals)-set(row_poss)) == 1:
            container[i][j] = list(set(poss_vals)-set(row_poss))[0]
            print_container(container)
        
        #check column
        col_poss = []
        for x in range(9):
            if x == i:
                continue
            if container[x][j] == 0:
                for val in get_poss_vals(x,j):
                    col_poss.append(val)
        if len(set(poss_vals)-set(col_poss)) == 1:
            container[i][j] = list(set(poss_vals)-set(col_poss))[0]
            print_container(container)
                
        #check square
        first = [0,1,2]
        second = [3,4,5]
        third = [6,7,8]
        find_square = [first,second,third]
        for l in find_square:
            if i in l:
                row = l
            if j in l:
                col = l
        square_poss = []
        for x in row:
            for y in col:
                if container[x][y] == 0:
                    for val in get_poss_vals(x,y):
                        square_poss.append(val)
        if len(set(poss_vals)-set(square_poss)) == 1:
            container[i][j] = list(set(poss_vals)-set(square_poss))[0]
            print_container(container)
    return container

def print_container(container):
    for i, row in enumerate(container):
        for j, val in enumerate(row):
            if (j)%3 == 0 and j<8 and j>0:
                print("|",end=' ')
            print(val,end=' ')
        print()
        if (i-2)%3 == 0 and i<8:
            print("_____________________", end='')
            print()
        print()
    print()
    print("||||||||||||||||||||||")
    print()

# using explicit solver
start = time.time()
zero_count = 0
for l in container:
    for v in l:
        if v == 0:
            zero_count += 1
            
print(f'There are {zero_count} moves I have to make!')
print()

print_container(container)
print()
solving = True


while solving:
    #Solver Portion
    container, stump_count = explicit_solver(container)
    
    #Loop-Breaking Portion
    zero_count = 0
    for l in container:
        for v in l:
            if v == 0:
                zero_count += 1
    if zero_count==0:
        print_container(container)
        solving=False
    if stump_count > 0:
        for i in range(9):
            for j in range(9):
                container = implicit_solver(i,j,container)
print()
print('That took', time.time()-start, 'seconds!')

#SnakingMyWayThruChallenges

mattreynolds
9 - Comet

Solution for the Advanced level challenge. Tempting to leverage Python/R. Was able to use standard Alteryx tools, and in particular the Optimization Tool to create a solver. Really great challenge.

 

Spoiler
sudoku.png

 

hanykowska
11 - Bolide

Cool one

 

Spoiler
hanykowska_0-1590087537001.png

 

TiwariVaishali
8 - Asteroid

solution for beginner