Reputation: 919
Can somebody explain why this doesn't work? (meaning the variable colorChange stays the same throughout function)
Each array (leftRing and rightRing have integer values of numbers ranging from 1 through 4)
Private Sub Solve_Click(sender As System.Object, e As System.EventArgs) Handles Solve.Click
countColorChangesInRings(colorChanges)
RotateLeftRingClockwise()
countColorChangesInRings(colorChanges)
End Sub
Sub countColorChangesInRings(ByRef var As Integer)
Dim colorChangesLeft As Integer = 0
Dim colorChangesRight As Integer = 0
Dim totalChanges As Integer = 0
' Count Color Changes in Left Ring
For i = 1 To 20
If (i = 20) Then
If (leftRing(i) <> leftRing(1)) Then
colorChangesLeft += 1
End If
Else
If (leftRing(i) <> leftRing(i + 1)) Then
colorChangesLeft += 1
End If
End If
Next
' Count Color Changes in Right Ring
For i = 1 To 20
If (i = 20) Then
If (rightRing(i) <> rightRing(1)) Then
colorChangesRight += 1
End If
Else
If (rightRing(i) <> rightRing(i + 1)) Then
colorChangesRight += 1
End If
End If
Next
totalChanges = colorChangesLeft + colorChangesRight
var = totalChanges
End Sub
Sub RotateLeftRingClockwise()
Dim temp As Integer = leftRing(1)
' Rotates Rings clockwise direction
For i = 1 To 20
If (i = 20) Then
leftRing(i) = temp
Else
leftRing(i) = leftRing(i + 1)
End If
Next
End Sub
Upvotes: 0
Views: 110
Reputation: 882586
From what I can see, it will stay the same.
You basically have a circular buffer of values (due to your special handle when i = 20
) and you count the number of times the value changes from index to index.
That number is not going to change if you simply rotate the values through the circular buffer.
Take for example, the array {1, 2, 2, 2, 3, 4}
. There are four transitions there, 1-2
, 2-3
, 3-4
and 4-1
.
If you then rotate the list clockwise, you get {4, 1, 2, 2, 2, 3}
and the transitions are 4-1
, 1-2
, 2-3
and 3-4
.
In other words, the order of the transitions may change, but the quantity does not.
Based on your addition of the RotateLeftRingClockwise
code, this is where your problem lies.
Because it simply rotates the left ring, you're not handling the intersection points of the Hungarian rings correctly. Rotating the left ring would, as well as moving the values around the left ring, change two of the array elements in the right ring (and vice versa), as per the diagram below:
array element 1 (in a 32-element array)
|
+---+---+
| |
V V
LLL RRR
LL LL RR RR
L * R <- shared cell is left(29)/right(5)
L R L R
L R L R
L R L R
L R L R
L R L R
L R L R
L R L R
L * R <- shared cell is left(21)/right(13)
LL LL RR RR
\ LLL RRR /
\ /
\----->>>-----/
Direction of
increasing
array index
Because your rotate code does not do this, the transition count of both rings will remain identical and hence the sum of them will not change.
For example, say the top-middle cells are array element 1 (as shown) and it increases in a anti-clockwise direction (as your code seems to indicate from the fact that a clockwise rotation shifts from ring[i+1]
to ring[i]
).
What your code needs to do is first rotate the left ring and then force the new values for the *
cells into the right ring, something like:
Sub RotateLeftRingClockwise()
Dim temp As Integer = leftRing(1)
For i = 1 To 19
leftRing(i) = leftRing(i + 1)
Next
leftRing(20) = temp
rightRing( 5) = leftRing(29)
rightRing(13) = leftRing(21)
End Sub
Those array indexes in the last two lines are specific to my diagram above (32 cells rather than 20), you'll need to adjust them for your own puzzle.
If you make those changes (and similar ones to your other three rotation functions), you should find that the function starts returning different values. Specifically, rotating the left ring will leave the left value unchanged but should change the right value as different balls come into the intersection point.
Upvotes: 1