Skip to content

Instantly share code, notes, and snippets.

@CodaFi
Last active May 20, 2018 16:10
Show Gist options
  • Save CodaFi/1a4835dcd256504467aeba4d8400605d to your computer and use it in GitHub Desktop.
Save CodaFi/1a4835dcd256504467aeba4d8400605d to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
//: Roots of Unity Often Lie
indirect enum BTree {
case empty
case branch(BTree, BTree)
}
func encode(_ t: (BTree, BTree, BTree, BTree, BTree, BTree, BTree)) -> BTree {
switch t {
case (.empty, .empty, .empty, .empty, .empty, .empty, .empty):
return .branch(.empty, .empty)
case let (.empty, .empty, .empty, .empty, .empty, .empty, .branch(b1, b2)):
return .branch(.branch(.branch(.branch(.branch(.empty, .empty), .empty), b1), .empty), b2)
case let (.empty, .empty, .empty, .empty, .empty, .branch(b1, b2), .empty):
return .branch(.empty, .branch(b1, b2))
case let (.empty, .empty, .empty, .empty, .empty, .branch(b1, b2), .branch(b3, b4)):
return .branch(.branch(.branch(.branch(.branch(.empty, .empty), .branch(b1, b2)), b3), .empty), b4)
case (.empty, .empty, .empty, .empty, .branch(.empty, .empty), .empty, .empty):
return .empty
case let (.empty, .empty, .empty, .empty, .branch(.empty, .empty), .branch(b1, b2), .empty):
return .branch(.branch(.empty, b1), b2)
case let (.empty, .empty, .empty, .empty, .branch(.empty, .branch(b1, b2)), b3, .empty):
return .branch(.branch(.branch(.branch(.empty, b1), b2), .empty),b3)
case let (.empty, .empty, .empty, .empty, .branch(.empty, .empty), b1, .branch(b2, b3)):
return .branch(.branch(.branch(.empty, b1), b2), b3)
case let (.empty, .empty, .empty, .empty, .branch(.empty, .branch(b1, b2)), b3, .branch(b4, b5)):
return .branch(.branch(.branch(.branch(.empty, b1),b2), .branch(b3, b4)),b5)
case let (.empty, .empty, .empty, .empty, .branch(.branch(b1, b2), b3), b4, b5):
return .branch(.branch(.branch(.branch(.branch(.empty, .empty), b1), b2), .branch(b3, b4)),b5)
case let (.empty, .empty, .empty, .branch(b1, b2), b3, b4, b5):
return .branch(.branch(.branch(.branch(.branch(.empty, .branch(.empty, b1)), b2), b3), b4), b5)
case let (.empty, .empty, .branch(b1, b2), b3, b4, b5, b6):
return .branch(.branch(.branch(.branch(.branch(.empty,.branch(.branch(.empty, b1),b2)),b3),b4),b5),b6)
case let (.empty, .branch(b1, b2), b3, b4, b5, b6, b7):
return .branch(.branch(.branch(.branch(.branch(.empty,.branch(.branch(.branch(.empty, b1), b2), b3)), b4), b5), b6), b7)
case let (.branch(.empty, b1), b2, b3, b4, b5, b6, b7):
return .branch(.branch(.branch(.branch(.branch(.branch(b1, b2), b3), b4), b5), b6), b7)
case let (.branch(.branch(b1, b2),b3), b4, b5, b6, b7, b8, b9):
return .branch(.branch(.branch(.branch(.branch(.empty,.branch(.branch(.branch(.branch(b1, b2), b3), b4), b5)), b6), b7), b8), b9)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment