Created
October 24, 2018 00:27
-
-
Save jay18001/92771171af1d77d3035ab39ebc6721f3 to your computer and use it in GitHub Desktop.
Mega Millions Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array { | |
func choose(_ n: Int) -> Array { return Array(shuffled().prefix(n)) } | |
} | |
struct Ticket { | |
let numbers: [Int] | |
let bonus: Int | |
} | |
func randomTicket() -> Ticket { | |
let numbers = Array(1...70).choose(5).sorted() | |
let bonus = Array(1...25).randomElement()! | |
return Ticket(numbers: numbers, bonus: bonus) | |
} | |
func randomTickets(number: Int = 5) -> [Ticket] { | |
var tickets = [Ticket]() | |
for _ in 0..<number { | |
tickets.append(randomTicket()) | |
} | |
return tickets | |
} | |
let ticket1 = randomTickets() | |
let ticket2 = randomTickets() | |
let ticket3 = randomTickets() | |
let ticket4 = randomTickets() | |
let allTickets = ticket1 + ticket2 + ticket3 + ticket4 | |
let calledTicket = randomTicket()// Ticket(numbers: [15,23,53,65,70], bonus: 7) | |
var matchingTickets: [(Int, Ticket)] = [] | |
for ticket in allTickets { | |
var matchCont = 0 | |
for number in calledTicket.numbers { | |
if ticket.numbers.contains(number) { | |
matchCont += 1 | |
} | |
} | |
if calledTicket.bonus == ticket.bonus { | |
matchCont += 1 | |
} | |
if matchCont > 0 { | |
matchingTickets.append((matchCont, ticket)) | |
} | |
} | |
matchingTickets.sort { (first, other) -> Bool in | |
return first.0 > other.0 | |
} | |
let winingTickets = matchingTickets.filter { (matchCount, ticket) -> Bool in | |
return matchCount >= 3 || ticket.bonus == calledTicket.bonus | |
} | |
if winingTickets.isEmpty { | |
print("No winners :(") | |
} | |
winingTickets.forEach { (matchCount, ticket) in | |
if matchCount == 6 { | |
print("Jackpot!!!!!") | |
} else if matchCount == 5 && ticket.bonus != calledTicket.bonus { | |
print("$1,000,000") | |
} else if matchCount == 5 && ticket.bonus == calledTicket.bonus { | |
print("$10,000") | |
} else if matchCount == 4 && ticket.bonus != calledTicket.bonus { | |
print("$500") | |
} else if matchCount == 4 && ticket.bonus == calledTicket.bonus { | |
print("$200") | |
} else if matchCount == 3 { | |
print("$10") | |
} else if matchCount == 2 && ticket.bonus == calledTicket.bonus { | |
print("$4") | |
} else if matchCount == 1 && ticket.bonus == calledTicket.bonus { | |
print("$2") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment