Yogiz Posted August 20, 2006 Share Posted August 20, 2006 So, basically I have three arrays at a given time. Let's say they are $x[], $y[] and$z[] For example if $x has values 1, 2, 3, $y has 5, 6, 7 and $z has 3, 4, 5 then I need the script to give me values 3 and 5 because they are in two arrays at the same time. I thought of using binarysearch to find if they are in any of the arrays one at a time (I need to check for numbers 1-9 only) but I had no idea where to store what and what the hell I'm doing at all. It just gets too complicated. Is there maybe a simplier way to achive what I'm trying to? Ideally in the end I want the script to return a single number from 1-9 if it's at least in two of the arrays and it's the only number that's in two arrays at the same time and maybe a 0 if none of the numbers are in two arrays at the same time or there is more then one number that's in two arrays. Link to comment Share on other sites More sharing options...
flyingboz Posted August 20, 2006 Share Posted August 20, 2006 (edited) So, basically I have three arrays at a given time. Let's say they are $x[], $y[] and$z[] Depends on how many possible solutions you could have, or if there will only be one solution. A big portion of your complication is you don't seem to be able to articulate exactly what you want. If you can't describe it clearly, it's really hard to code it. If you can only have one solution, then: build an array with the number of times you found a 1-9. iterate through each array, adding 1 to the appropriate element as you find it. test the new value after its addition - if it equals two , you're done. Edited August 20, 2006 by flyingboz Reading the help file before you post... Not only will it make you look smarter, it will make you smarter. Link to comment Share on other sites More sharing options...
Yogiz Posted August 20, 2006 Author Share Posted August 20, 2006 For clarification then, I'm making a sudoku solver. For each of the 81 squares (that have a value from 1-9, 0 if blank) I have three arrays. 9x1 cell that goes from left to right. 1x9 cell that goes from top to bottom and a 3x3 cell that goes left to right and then top to bottom. So for each square I need to see if there is only one possible value for it, if there is, the value of the square will be changed into that. Link to comment Share on other sites More sharing options...
flyingboz Posted August 20, 2006 Share Posted August 20, 2006 For clarification then, I'm making a sudoku solver. For each of the 81 squares (that have a value from 1-9, 0 if blank) I have three arrays. 9x1 cell that goes from left to right. 1x9 cell that goes from top to bottom and a 3x3 cell that goes left to right and then top to bottom. So for each square I need to see if there is only one possible value for it, if there is, the value of the square will be changed into that.I think you're going to need to rethink your approach. What you need is a mechanism to determine which elements you should be testing. Most sudoku solver approaches I have seen involve starting each square w/ all possible options, and then eliminating the duplicates... In your case 0 = 1-9, which is fine, but when you eliminate an option, how are you going to store the fact that a given cell can be 1-5,7,9 if you only populate solved cells? Much easier, IMO to start your "blank" cells with the string "123456789", and then eliminate based on the sudoku ruleset w/ a StringReplace() . When StringLen($array[$x][$y]) = 1 then you have solved that cell. This way , you have one 9x9 array, and you develop the functions for calling a given row/line (trivial) and each of the 9 subsquares (either do the math, or create a squares array that contains the xy coords of each cell in the square). Of course, this is only one approach, and relying on the String... functions will cost speed. Reading the help file before you post... Not only will it make you look smarter, it will make you smarter. Link to comment Share on other sites More sharing options...
Yogiz Posted August 20, 2006 Author Share Posted August 20, 2006 I think you're going to need to rethink your approach. What you need is a mechanism to determine which elements you should be testing. Most sudoku solver approaches I have seen involve starting each square w/ all possible options, and then eliminating the duplicates... In your case 0 = 1-9, which is fine, but when you eliminate an option, how are you going to store the fact that a given cell can be 1-5,7,9 if you only populate solved cells? Much easier, IMO to start your "blank" cells with the string "123456789", and then eliminate based on the sudoku ruleset w/ a StringReplace() . When StringLen($array[$x][$y]) = 1 then you have solved that cell. This way , you have one 9x9 array, and you develop the functions for calling a given row/line (trivial) and each of the 9 subsquares (either do the math, or create a squares array that contains the xy coords of each cell in the square). Of course, this is only one approach, and relying on the String... functions will cost speed. I may be missing something, but this alternate solution will still not help me with the current problem. How do I know that the current square can be declared solved? Link to comment Share on other sites More sharing options...
Confuzzled Posted August 20, 2006 Share Posted August 20, 2006 What is the rule that defines a current square as being solved? Test for that. Link to comment Share on other sites More sharing options...
Yogiz Posted August 20, 2006 Author Share Posted August 20, 2006 What is the rule that defines a current square as being solved? Test for that.Well, that's what the three arrays are for and that's why I have a problem : ). Link to comment Share on other sites More sharing options...
Confuzzled Posted August 20, 2006 Share Posted August 20, 2006 (edited) Ideally in the end I want the script to Test 1: return a single number from 1-9 if it's at least in two of the arraysandTest 2: it's the only number that's in two arrays at the same timeandTest 3: maybe a 0 if none of the numbers are in two arrays at the same timeor Test 4: there is more then one number that's in two arrays.Is this what you want to test for? You just want a series of nested loops, don't you? Edited August 20, 2006 by Confuzzled Link to comment Share on other sites More sharing options...
Yogiz Posted August 20, 2006 Author Share Posted August 20, 2006 Is this what you want to test for? You just want a series of nested loops, don't you?Probably so, It's the first test I'm having a problem with. Link to comment Share on other sites More sharing options...
GaryFrost Posted August 20, 2006 Share Posted August 20, 2006 Need to re-create what's been done a few times?If not:http://www.autoitscript.com/forum/index.ph...st&p=171580just search for sudoku SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
Yogiz Posted August 20, 2006 Author Share Posted August 20, 2006 Need to re-create what's been done a few times?Actually yes.It's more of a learning-making yourself thing then actually needing the program. Link to comment Share on other sites More sharing options...
GaryFrost Posted August 20, 2006 Share Posted August 20, 2006 (edited) I think this is what you were asking in the original post: expandcollapse popup#include <Array.au3> Dim $a1[3] = [1, 2, 3], $a2[5] = [5, 6, 7, 8, 9], $a3[2] = [3, 5] MsgBox(0, "Dupes?", _HasDupe($a1, $a2, $a3)) Func _HasDupe($a_array01, $a_array02, $a_array03, $i_ReturnType = 0) Local $Dupes[1] = [0] Local $found = False For $i = 0 To UBound($a_array01) - 1 For $j = 0 To UBound($a_array02) - 1 If $a_array01[$i] = $a_array02[$j] Then $found = False For $x = 1 To $Dupes[0] If $Dupes[$x] = $a_array01[$i] Then $found = True ExitLoop EndIf Next If Not $found Then $Dupes[0] += 1 ReDim $Dupes[$Dupes[0] + 1] $Dupes[$Dupes[0]] = $a_array01[$x] EndIf EndIf For $k = 0 To UBound($a_array03) - 1 If $a_array01[$i] = $a_array03[$k]Then $found = False For $x = 1 To $Dupes[0] If $Dupes[$x] = $a_array01[$i] Then $found = True ExitLoop EndIf Next If Not $found Then $Dupes[0] += 1 ReDim $Dupes[$Dupes[0] + 1] $Dupes[$Dupes[0]] = $a_array01[$i] EndIf EndIf If $a_array02[$j] = $a_array03[$k]Then $found = False For $x = 1 To $Dupes[0] If $Dupes[$x] = $a_array02[$j] Then $found = True ExitLoop EndIf Next If Not $found Then $Dupes[0] += 1 ReDim $Dupes[$Dupes[0] + 1] $Dupes[$Dupes[0]] = $a_array02[$j] EndIf EndIf Next Next Next If $Dupes[0] = 0 Then Return '' If Not $i_ReturnType Then Local $s_return = '' For $x = 1 To $Dupes[0] $s_return &= $Dupes[$x] & ',' Next Return StringTrimRight($s_return, 1) Else Return $Dupes EndIf EndFunc ;==>_HasDupe Edited August 20, 2006 by gafrost SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
Yogiz Posted August 20, 2006 Author Share Posted August 20, 2006 (edited) I think this is what you were asking in the original post:I think it is, thanks. Edited August 20, 2006 by Yogiz Link to comment Share on other sites More sharing options...
GaryFrost Posted August 20, 2006 Share Posted August 20, 2006 (edited) I think it is, thanks. Just playing around, but this will let you use any number of arrays: expandcollapse popup#include <Array.au3> Dim $a1[3] = [1, 2, 3], $a2[5] = [5, 6, 7, 8, 9], $a3[2] = [3, 5] Dim $a_a[4] = [3, $a1, $a2, $a3] MsgBox(0, "Dupes?", _SearchForDupes($a_a)) Func _SearchForDupes($a_arrays, $i_ReturnType = 0) Local $Dupes[1] = [0] If $a_arrays[0] < 2 Then Return '' For $i = 1 To $a_a[0] - 1 For $j = $i + 1 To $a_a[0] _FindDupe($a_a[$i], $a_a[$j], $Dupes) Next Next If $Dupes[0] = 0 Then Return '' _ArraySort($Dupes) If Not $i_ReturnType Then Local $s_return = '' For $x = 1 To $Dupes[0] $s_return &= $Dupes[$x] & ',' Next Return StringTrimRight($s_return, 1) Else Return $Dupes EndIf EndFunc ;==>_SearchForDupes Func _FindDupe(ByRef $a_array01, ByRef $a_array02, ByRef $Dupes) Local $found = False For $i = 0 To UBound($a_array01) - 1 For $j = 0 To UBound($a_array02) - 1 If $a_array01[$i] = $a_array02[$j] Then $found = False For $x = 1 To $Dupes[0] If $Dupes[$x] = $a_array01[$i] Then $found = True ExitLoop EndIf Next If Not $found Then $Dupes[0] += 1 ReDim $Dupes[$Dupes[0] + 1] $Dupes[$Dupes[0]] = $a_array01[$i] EndIf EndIf Next Next EndFunc ;==>_FindDupe Edited August 20, 2006 by gafrost SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now