seems like a straight-forward simple question but I cannot program it no matter what I try because it's hard to compact the logic in both cases forward and backwards and ignoring starting position (using starting position as well as going forward from starting position if necessary).
Code:
1) If you start at Row 0, Column 0, Then Just
~ Print out Row 0, Column 0.
~ If Processing is required can go to any Row 1,2 or itself Row 0.
2) If you start at Row 1, Column 0, Then
~ If in Row 0, and Column `n..3` is Processed already, Go back to Row 1.
3) If you start at Row 2, Column 0, Then Print out Row 1, Column `n..3`.
~ Print Row 1, and Column `n..3` Then
~ Print Row 0, and Column `n..3`
~ If Row 1 and Row 0 are both already Processed. Go back to Row 2.
Here is the code I got.
Code:
Public Structure Result
Dim Answer As Short
Dim CurrentRow As Long
Dim CurrentColumn As Long
End Structure
Structure FoundValue
Dim Value As Short
Dim Row As Integer
Dim Column As Integer
End Structure
Public Uniques()() As Short
Public ProcessedBits()() As Byte
Function GetFirstRowUnique(UniqueArray()() As Short, CurrentRow As Integer) As Result
Dim res1 As New Result
res1.Answer = -1
If UniqueArray Is Nothing Then Return res1 'Unique Array isn't initialized.
Dim Row As Integer = 0
Dim Column As Integer = 0
Dim ValuesFoundInPath As New List(Of FoundValue)
Dim isCurrentRow As Boolean = False
Dim OriginalRow As Integer = CurrentRow
Row = CurrentRow
Dim CurrentValue As Short = -1
Dim Increase As Boolean = False
CurrentRow = OriginalRow
Row = OriginalRow
While Row >= 0
If ((Row - 1) < 0 OrElse Row > UniqueArray.Length - 1) AndAlso ValuesFoundInPath.Count > 0 Then
Exit While
ElseIf Row = OriginalRow AndAlso (Row - 1) >= 0 Then
Row -= 1 'If Original Row and can go down, then Lower the Row by 1.
End If
For Column = 0 To Uniques(Row).Length - 1 'Check each column in current row.
If ProcessedBits(Row)(Column) = 1 Then Continue For 'If the Column is Processed, Then skip it and process the next one.
Dim foundValue As New FoundValue
foundValue.Value = UniqueArray(Row)(Column)
foundValue.Row = Row
foundValue.Column = Column
ValuesFoundInPath.Add(foundValue)
Exit For
Next
If OriginalRow = 0 AndAlso Row = 0 Then
Row += 1 'If OriginalRow failed, Increase Row by +1.
ElseIf (Row - 1) = OriginalRow OrElse (((Row - 1) < 0 OrElse Row > UniqueArray.Length - 1) AndAlso ValuesFoundInPath.Count > 0) Then
Exit While
End If
End While
'TODO: Logic removed, this is how to simulate a ProcessedBits.
ProcessedBits(Row)(Column) = 1
'TODO Logic removed this is just the end of it.
Dim result As New Result
result.Answer = ValuesFoundInPath(matchingIndex).Value
result.CurrentRow = ValuesFoundInPath(matchingIndex).Row
result.CurrentColumn = ValuesFoundInPath(matchingIndex).Column
Return result 'The Next Row value which isn't Processed yet.
End Function
[1]: https://i.stack.imgur.com/BCRT3.png