hello everyone a couple of months ago I tried to solve the Sudoku solver which is a 3Q code Wars exercise and I I was struggling a little bit so I left it alone and um a few days ago I tried again and started from scratch and I I was able to solve it finally I think it took me uh over two hours to solve it but I liked the end result because the main part of the program is in just a single line of code which I liked is array functions chained together and um after
I finished I made a small bot using a puppeteer to access our website to extract the the puzzle information into an array have the algorithm solve it then write the solution back into the website all uh automatically and I also like that result so let's I will show you my logic and I will show you a couple of examples of the bot solving the Sudoku puzzles I want to point out that this is the trick use the cool solver by pineapple clock and it's the easy one I I think there are a couple of uh
not so easy ones see so this is a the terminable meaning there's no need to guess so you will find at least one position into the puzzle where there's uh only one solution so it's not that complicated okay before we continue uh there's a spoiler alert because I will show you um pretty much all my codes so if you want to solve it on your own uh you should stop looking at the next part of the video so this is the the puzzle as you will receive it okay so my logic is is this you
receive the puzzle and while it's not solved you will find in this nested Force because you have to check I I wanted to check every position and see the possibilities of the numbers that can be placed into that position into the puzzle so if there's only one possibility I will write it down and continue again until the Apostle is solved I I included the max cycle so there's only 100 Cycles as a fail safe just in case I run into um on the terminal puzzle so let's see at what they find possibilities function actually does
so this is the important function and after thinking a little bit I realized that what I wanted to do was to Simply type all the numbers and the filter the numbers that are already there and remove those so for example let's take um x equals 3 and Y equals one so that would be this position so what we need to do is find out which numbers are in into this row this column and this mini Square and remove them from our number list so these are the possibilities which we start with and let's start with
the row because it's the easiest one so we will want to filter all these all these numbers by using this particular row and we want to check that every element into the row is different from our numbers so in this case and there's then number one and number two so those two numbers will not pass the filter and therefore will be removed okay so that was the easiest part and this took me actually a little bit but then I realized the next part it's also a filter and there's no need to start with all the
numbers again so we will filter starting from the remaining numbers of course the next part is the row so before we run our filter we want to map our puzzle and only keep the elements that have the same index as our X so that will return all these numbers we want to plot them to have a one-dimensional array and we will have the same as we did right here and by coincidence the the code is almost on top of one another then this as you can see is exactly the same function we want to check
that every element into this new array is different from the remaining elements into our number list so we have three four eight and a nine so those will be gone foreign so by this point we already already have three possibilities and we finally have to do the same filter but with the numbers [Music] listed here so this was the most challenging part because before running the final filter we need a slice of the puzzle and I realized that by dividing by 3 slurring it and multiplying by three we will get for for index 0 1
and 2 we will get zero four three four and five we will get three and for the next part we will get six so by making this slice three positions so one two and three and we will get this subarray or this slice of the array and then we will we will have to map that new sub array and filter the index so that the index is bigger than again this operation but now with x and uh less than two positions more so finally we will get this slice of the array we will want to
Flat it to get a one-dimensional array and finally we will be able to perform the same function function as before that every element into this array is different from our number list so there is one eight and a nine and they are no longer into our list so for this particle particular position we will get these three possibilities so as you saw in the in the code before we will want to find um a position where there's only one possible solution to write it down so without going into much detail a a used Puppeteer to
create this small bot to test my algorithm into our real website real Sudoku website so this is the website that I found would be easy for me to to create a boat for so I open the website I had to increase the timeout a little bit maybe my computer is slow and then I extract all the information from the Sudoku table or boards and after getting the solution with the previous algorithm I type it back and that worked well I will leave a couple of examples here so you can see and if you want to
check the code I I will leave a link into the description for my GitHub page so I hope it was useful for you goodbye [Music] thank you [Music] [Music] thank you [Music]