Hi Some Guy, Roy
I like a challenge.
Here's my version. I left in the comments and debug output, though in the
competition I would remove them.
Are you going to post yours for review?
Regards,
Fergus
<code>
'Start time 9:50pm
'First run 10:20
'First successful completion of examples 10:27
'Finished testing 10:36
'Ready to submit 10:38
'
Public Class CircleDeck
Public Sub Test
cardsLeft ("QJT98765432AQJT98765432A")
cardsLeft ("") '0
cardsLeft ("QQQ") '0
cardsLeft ("KKKKKKKKKK") '0
cardsLeft ("T1111113") '6
cardsLeft ("111111T3") '6
cardsLeft ("KQJT987K65432AQJT987K65432AK") '0
cardsLeft ("KKKKKAQT23") '1
cardsLeft ("KKKKATQ23J") '6
cardsLeft ("AT68482AK6875QJ5K9573Q") '4
cardsLeft ("AQK262362TKKAQ6262437892KTTJA332") '24
End Sub
Public Function cardsLeft (sDeck As String) As Integer
Dim sCards As String = "23456789TJQK"
Dim NumCards = sDeck.Length
Dim I = 0
Dim J = 0
Dim StillGoing
Dim NoRemoveCount = 0
Console.WriteLine ("CARDS = {0}", sDeck)
Do Until NoRemoveCount = sDeck.Length
'Get the next index.
J = IIF (I + 1 < sDeck.Length, I + 1, 0)
Console.Write ("I = {0}, J = {1}, ", I, J)
Console.WriteLine ("c1 = {0}, c2 = {1}, v1 = {2}, v2 = {3}", _
sDeck.Substring (I, 1), _
sDeck.Substring (J, 1), _
sCards.IndexOf (sDeck.Substring (I, 1)) + 2, _
sCards.IndexOf (sDeck.Substring (J, 1)) + 2 _
)
'Get the values for the next pair of cards.
Dim v1 As Integer = sCards.IndexOf (sDeck.Substring (I, 1)) + 2
If v1 = 13 Then
Console.WriteLine ("Remove King:{0} {1}", vbCrLf, sDeck)
'Remove King.
sDeck = sDeck.Substring (0, I) & sDeck.Substring (I + 1)
'I stays same or.
If I >= sDeck.Length Then
I = 0
End If
NoRemoveCount = 0
Else
J = IIF (I + 1 < sDeck.Length, I + 1, 0)
Dim v2 As Integer = sCards.IndexOf (sDeck.Substring (J, 1)) +
2
If v1 + v2 = 13 Then
'Remove the pair.
If J = 0 Then
'Lose both ends.
Console.WriteLine ("Lose Ends:{0} {1}", vbCrLf,
sDeck)
sDeck = sDeck.Substring (1, I - 1)
Console.WriteLine (" {0}", sDeck)
'I drops by two.
I = I - 2
Else
'Lose two from middle or far end.
Console.WriteLine ("Lose Middle:{0} {1}", vbCrLf,
sDeck)
sDeck = sDeck.Substring (0, I) & sDeck.Substring (J +
1)
Console.WriteLine (" {0}", sDeck)
'I stays same or.
If I >= sDeck.Length Then
I = 0
End If
End If
NoRemoveCount = 0
Else
I = I + 1
If I >= sDeck.Length Then
I = 0
End If
NoRemoveCount = NoRemoveCount + 1
End If
End If
Loop
Console.WriteLine ("Result = {0}", sDeck.Length)
Return sDeck.Length
End Function
End Class
</code>