Skip to content

Instantly share code, notes, and snippets.

@kanetai
Created April 24, 2016 10:27
Show Gist options
  • Save kanetai/aa622643994ec0b8d193eb98f6f887f8 to your computer and use it in GitHub Desktop.
Save kanetai/aa622643994ec0b8d193eb98f6f887f8 to your computer and use it in GitHub Desktop.
[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)
]
let neighborhood4: [Point] = [
(0, -1), (1, 0), (0, 1), (-1, 0)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment