Solution for Ch 25 Bronze, Gold, and Platinum Challenge in Xcode 9.3

Bronze, and Platinum Challenge

struct Point: Comparable {
  let x: Int
  let y: Int

  // Bronze Challenge
  static func + (lhs: Point, rhs: Point) -> Point {
    return Point(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
  }

  // Platinum Challenge
  static let origin = Point(x: 0, y: 0)

  func euclideanDistance(from point: Point) -> Double {
    let dx = Double(point.x - x)
    let dy = Double(point.y - y)
    return (dx * dx + dy * dy).squareRoot()
  }

  static func == (lhs: Point, rhs: Point) -> Bool {
    return lhs.euclideanDistance(from: origin) == rhs.euclideanDistance(from: origin)
  }

  static func < (lhs: Point, rhs: Point) -> Bool {
    return lhs.euclideanDistance(from: origin) < rhs.euclideanDistance(from: origin)
  }
}

let c = Point(x: 3, y: 4)
let d = Point(x: 2, y: 5)

// Bronze Challenge
print(c + d)        // Point(x: 5, y: 9)

// Platinum Challenge
print(c.euclideanDistance(from: Point.origin))    // 5.0
print(d.euclideanDistance(from: Point.origin))    // 5.3851648071345
print(c > d)    // false
print(c < d)    // true
print(c == d)   // false
print(c >= d)   // false
print(c <= d)   // true

Gold Challenge

class Person {
  let name: String
  let age: Int

  init(name: String, age: Int) {
    self.name = name
    self.age = age
  }
}

extension Person: Equatable {
  static func == (lhs: Person, rhs: Person) -> Bool {
    return lhs === rhs    // Compare addresses, not values
  }
}

let hostage = Person(name: "Jack", age: 30)
let suspect = Person(name: "Jack", age: 30)
let victim = Person(name: "Jack", age: 30)
let peopleInBank = [hostage, suspect, victim]

if let targetPosition = peopleInBank.index(of: suspect) {
  print("Aim at", targetPosition)      // Aim at 1
}