Skip to content

Instantly share code, notes, and snippets.

@cslowik
Created December 16, 2015 16:30
Show Gist options
  • Save cslowik/55905a2c08804b28ee43 to your computer and use it in GitHub Desktop.
Save cslowik/55905a2c08804b28ee43 to your computer and use it in GitHub Desktop.
Bloc Weekly Code Puzzle - Dreidel Dreidel (12/14/15)
// DREIDEL
// Chris Slowik
// 12/15/15
//
// (0) Nun - nothing happens
// (1) Gimel - you take the pot!
// (2) Hei - you take half the pot
// (3) Shin - you put a piece into the pot
//
// Create a function that given an array of dreidel rolls ordered
// from first to last (so rolls[0] is the first roll and rolls[rolls.length - 1]
// is the last roll), the number of coins in your account, and
// the number of coins in the pot, returns the number of coins left
// in your account after the last roll.
import Foundation
let myRolls = ["Nun", "Nun", "Shin", "Gimel", "Shin"]
func playDreidel(rolls: [String], var coins: Int, var pot: Int) -> Int {
for (_, roll) in rolls.enumerate() {
switch roll {
case "Gimel":
coins += pot
pot = 0
case "Hei":
pot = pot / 2
coins += pot
case "Shin":
pot += 1
coins -= 1
default:
break
}
}
return coins
}
playDreidel(myRolls, coins: 10, pot: 20) //result: 29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment