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
#!/usr/bin/swift | |
import Foundation | |
public enum ESCCode : UInt { | |
case Reset = 0, Bold = 1, Faint, UnderLine = 4, BlinkSlow, Negative = 7 | |
case Black = 30, Red, Green, Yellow, Blue, Magenda, Cyan, White | |
case BackgroundBlack = 40, BackgroundRed, BackgroundGreen, BackgroundYellow, BackgroundBlue, BackgroundMagenda, BackgroundCyan, BackgroundWhite | |
static let backGroundColorOffset: UInt = 10 | |
static func escapeCode(value: UInt) -> String { return "\u{1b}[\(value)m" } |
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
def lowerBound(a, key) | |
lb = -1; ub = a.length | |
while ub - lb > 1 | |
mid = (lb + ub) / 2 | |
if a[mid] >= key | |
ub = mid | |
else | |
lb = mid | |
end | |
end |
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
typealias Point = (x: Int, y: Int) | |
func ==(lhs: Point, rhs: Point) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } | |
func !=(lhs: Point, rhs: Point) -> Bool { return !(lhs == rhs) } | |
func +(lhs: Point, rhs: Point) -> Point { return Point(lhs.x + rhs.x, lhs.y + rhs.y) } | |
func +=(inout lhs: Point, rhs: Point) { lhs.x += rhs.x; lhs.y += rhs.y } | |
func -(lhs: Point, rhs: Point) -> Point { return Point(lhs.x - rhs.x, lhs.y - rhs.y) } | |
func -=(inout lhs: Point, rhs: Point) { lhs.x -= rhs.x; lhs.y -= rhs.y } | |
let neighborhood8: [Point] = [ | |
(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1) | |
] |
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
extension Character { | |
var ord: UInt32 { | |
let u = String(self).unicodeScalars | |
return u[u.startIndex].value | |
} | |
var isDigit: Bool { return "0"..."9" ~= self } | |
var isAlpha: Bool { return "a"..."z" ~= self || "A"..."Z" ~= self } | |
} |
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
//Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81) | |
extension String: CollectionType { | |
func at(idx: Int) -> Character { | |
return self[self.startIndex.advancedBy(idx)] | |
} | |
func subString(s: Int, _ e: Int) -> String { | |
return self[self.startIndex.advancedBy(s)..<self.startIndex.advancedBy(e)] | |
} | |
} |