Here is my submission for the Advanced Event 3 in the Microsoft Scripting Games 2008.
1: '*********************************************************************
2: ' Script Name: Event3.vbs
3: ' Version: 1.0
4: ' Author: Perry Harris (PHactotum)
5: ' Updated: 9:49 PM Thursday, February 21, 2008
6: ' Purpose: Solves the 2008 winter Scripting Games Advance Event 3: Instant (Runoff) Winner
7: '
8: ' Usage: cscript Event3.vbs [phone number]
9: ' Notes:
10: ' Keywords:
11: ' versioning: 1.0 Original release
12: '*********************************************************************
13: Option Explicit
14: Dim Politicians, Losers, Candidate
15: Dim Done
16: Dim objFS, objFile
17: Dim strLine
18: Dim aVote
19: Dim i
20: Dim LowestVote
21: Dim LowestName
22: Dim TotalVotes
23:
24: Const ForReading = 1
25: 'We'll be using two Dictionaries to keep track of our Politicians and our Losers.
26: set Politicians=CreateObject("Scripting.Dictionary")
27: set Losers=CreateObject("Scripting.Dictionary")
28:
29: 'Open and read our Voting File
30: Set objFS = CreateObject("Scripting.FileSystemObject")
31: 'Just to be clear, We're not done yet
32: Done = False
33:
34: Do
35: Politicians.RemoveAll
36: TotalVotes = 0
37: Set objFile = objFS.OpenTextFile("C:\Scripts\votes.txt", ForReading)
38: Do until objFile.AtEndOfStream
39: strLine = objFile.ReadLine
40: aVote = split(strLine,",")
41: i = 0
42: Do until not(Losers.Exists(aVote(i)))
43: i = i + 1
44: loop
45:
46: If Politicians.Exists(aVote(i)) then
47: Politicians.Item(aVote(i)) = Politicians.Item(aVote(i)) + 1
48: else
49: Politicians.Add aVote(i), 1
50: end if
51: TotalVotes = TotalVotes + 1
52: Loop
53: 'Debugging statement
54: 'WScript.StdOut.WriteLine "============================="
55: 'We set LowestVote to 100%, or 1.00 so that as we find lower numbers we bubble down to the lowest result for possible extra passes.
56: LowestVote = 1
57: LowestName = "" 'Just to make sure it doesn't affect the outcome in future passes
58: For each Candidate in Politicians
59: 'The following line was used for debugging purposes
60: 'Wscript.StdOut.WriteLine Candidate & ": " & FormatPercent(Politicians.Item(Candidate)/TotalVotes,1)
61: If Politicians.Item(Candidate)/TotalVotes > 0.5 then
62: WScript.StdOut.WriteLine "The winner is " & Candidate & " with " & FormatPercent(Politicians.Item(Candidate)/TotalVotes,1) & " of the vote."
63: Done = True
64: Exit For
65: else if LowestVote > Politicians.Item(Candidate)/TotalVotes then
66: LowestName = Candidate
67: LowestVote = Politicians.Item(Candidate)/TotalVotes
68: end if
69: end if
70: next
71: Losers.Add LowestName,LowestVote 'Really don't need the vote but the Dictionary add method requires an item.
72: objFile.Close
73: Loop Until Done