Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivanbruel/2b0bffb941da0ac23006edca883cd5d7 to your computer and use it in GitHub Desktop.
Save ivanbruel/2b0bffb941da0ac23006edca883cd5d7 to your computer and use it in GitHub Desktop.
import UIKit
struct Domino {
let left: Int
let right: Int
init(string: String) {
let split = string.componentsSeparatedByString("-")
left = split.first.flatMap { Int($0) } ?? 0
right = split.last.flatMap { Int($0) } ?? 0
}
func matches(nextDomino: Domino) -> Bool {
return right == nextDomino.left
}
}
func matchCount(dominoString: String) -> Int {
let dominoStrings = dominoString.componentsSeparatedByString(",")
let dominos = dominoStrings.map { Domino(string: $0) }
var matches = [Int]()
var match: Int = 0
for index in 0..<dominos.count {
let domino = dominos[index]
if index + 1 >= dominos.count {
break
}
let nextDomino = dominos[index + 1]
if domino.matches(nextDomino) {
match += 1
} else {
match = 1
}
matches.append(match)
}
return matches.maxElement() ?? 1
}
matchCount("1-1,3-5,5-2,2-3,2-4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment