Skip to content

Instantly share code, notes, and snippets.

@kanetai
kanetai / colorTest.swift
Last active April 24, 2016 14:21
[Swift]text coloring by escape code
#!/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" }
@kanetai
kanetai / bound.rb
Created April 24, 2016 13:17
cpp-like lower_bound, upper_bound in Ruby
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
@kanetai
kanetai / Point+Competitive.swift
Created April 24, 2016 10:27
[Swift]Simplified point type for competitive programming
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)
]
@kanetai
kanetai / Character+Competitive.swift
Last active April 24, 2016 10:21
[Swift]Character extension for competitive programming
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 }
}
@kanetai
kanetai / String+Competitive.swift
Last active August 3, 2017 15:34
[Swift]String extension for competitive programming
//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)]
}
}