luoheng
12/24/2019 - 6:45 AM

validSquare


func distance(p1 []int, p2 []int) int {
    x, y := p1[0]-p2[0], p1[1]-p2[1]
    return x * x + y * y
}

func validSquare(p1 []int, p2 []int, p3 []int, p4 []int) bool {
    x1, x2, x3, x4, x5, x6 := distance(p1, p2), distance(p1, p3), distance(p1, p4), distance(p2, p3), distance(p2, p4), distance(p3, p4)
    var m map[int]int = map[int]int{}
    m[x1]++
    m[x2]++
    m[x3]++
    m[x4]++
    m[x5]++
    m[x6]++
    if len(m) != 2 {
        return false
    }
    var s, s_value int
    for key, value := range m {
        if s != 0 {
            if key*2 == s && value == 4 && s_value == 2 || s*2 == key && value == 2 && s_value == 4 {
                return true
            }
        }
        s, s_value = key, value
    }
    return false
}