Created
March 23, 2017 01:19
-
-
Save yovasx2/8fdce5453a548814d02d7ff4a782cb0d to your computer and use it in GitHub Desktop.
This file contains 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/ruby | |
# The night sky can be modeled as an infinite 2D plane. There are N stars at distinct positions on this plane, the ith of which is at coordinates (Xi, Yi). | |
# A boomerang constellation is a pair of distinct equal-length line segments which share a single endpoint, such that both endpoints of each segment coincide with a star's location. | |
# Two boomerang constellations are distinct if they're not made up of the same unordered pair of line segments. How many distinct boomerang constellations can you spot? | |
# Input Format | |
# Input begins with an integer N. Then, N lines follow, the ith of which contains the space-separated integers Xi and Yi. | |
# Constraints | |
# 1 ≤ N ≤ 2,000 | |
# -10,000 ≤ Xi, Yi ≤ 10,000 | |
# Output Format | |
# print the number of boomerang constellations in the night sky. | |
# Example 1: | |
# Input: | |
# 3 | |
# 0 0 | |
# 0 1 | |
# 0 3 | |
# Expected Output: | |
# 0 | |
# Explanation: Every pair of stars is a unique distance apart, so there are no boomerang constellations. | |
# Example 2: | |
# Input: 5 | |
# 0 0 | |
# 0 1 | |
# 0 2 | |
# 0 3 | |
# 0 4 | |
# Expected Output: 4 | |
# Explanation: There are 4 boomerang constellations. One of them consists of the line segments (0,0)-(0,2) and (0,2)-(0,4). | |
# Sample Input 0 | |
# 3 | |
# 0 0 | |
# 0 1 | |
# 0 3 | |
# Sample Output 0 | |
# 0 | |
# Sample Input 1 | |
# 5 | |
# 0 0 | |
# 0 1 | |
# 0 2 | |
# 0 3 | |
# 0 4 | |
# Sample Output 1 | |
# 4 | |
# Sample Input 2 | |
# 4 | |
# 0 0 | |
# 0 100 | |
# 100 0 | |
# 100 100 | |
# Sample Output 2 | |
# 4 | |
# Sample Input 3 | |
# 4 | |
# 0 0 | |
# -3 4 | |
# 0 5 | |
# -5 0 | |
# Sample Output 3 | |
# 3 | |
# Sample Input 4 | |
# 6 | |
# 5 6 | |
# 6 5 | |
# 7 6 | |
# 6 7 | |
# 7 8 | |
# 8 7 | |
# Sample Output 4 | |
# 12 | |
class Boomerang | |
attr_accessor :num_stars, :stars | |
def initialize | |
@stars = [] | |
@num_stars = gets.chop.to_i | |
for i in 1..@num_stars | |
coordinates = clean_params(gets, ' ') | |
@stars << Star.new(coordinates[0].to_i, coordinates[1].to_i) | |
end | |
end | |
def clean_params(str, separator = '') | |
str.chop.split(separator) | |
end | |
def dist2(i, j) | |
dx2 = (@stars[i].x - @stars[j].x) * (@stars[i].x - @stars[j].x) | |
dy2 = (@stars[i].y - @stars[j].y) * (@stars[i].y - @stars[j].y) | |
dx2 + dy2 | |
end | |
def num_constellations | |
res = 0 | |
for i in 0..@num_stars-1 do | |
distances = {} | |
for j in 0..@num_stars-1 do | |
if(i!=j) | |
d = dist2(i,j) | |
distances[d] = 0 unless distances.has_key?(d) | |
res += distances[d] | |
distances[d]+=1 | |
end | |
end | |
end | |
res | |
end | |
end | |
class Star | |
attr_accessor :x, :y | |
def initialize(x, y) | |
@x = x | |
@y = y | |
end | |
end | |
b = Boomerang.new | |
puts b.num_constellations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment