Created
September 26, 2021 17:32
-
-
Save billypchan/9ffc8d5f16ab748bf96870f41928ba5e to your computer and use it in GitHub Desktop.
Find the chance when rolling 3 dices the the reminder of divid the sum by 4.
This file contains 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 | |
func diceChance() { | |
var sums = [Int: Int]() | |
for i in 1...6 { | |
for j in 1...6 { | |
for k in 1...6 { | |
sums[i+j+k, default: 0] += 1 | |
} | |
} | |
} | |
print(sums.sorted{a,b in | |
a.key < b.key | |
}) | |
let dealer = sums.reduce(into: [:]) { modFour, element in | |
modFour[element.key % 4, default: 0] += element.value | |
} | |
print(dealer.sorted{a,b in | |
a.key < b.key | |
}) | |
} | |
diceChance() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
[(key: 3, value: 1), (key: 4, value: 3), (key: 5, value: 6), (key: 6, value: 10), (key: 7, value: 15), (key: 8, value: 21), (key: 9, value: 25), (key: 10, value: 27), (key: 11, value: 27), (key: 12, value: 25), (key: 13, value: 21), (key: 14, value: 15), (key: 15, value: 10), (key: 16, value: 6), (key: 17, value: 3), (key: 18, value: 1)]
[(key: 0, value: 55), (key: 1, value: 55), (key: 2, value: 53), (key: 3, value: 53)]