Facebook Hacker Cup 2016: Qualification Round 1.1 (SOLVED)

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Facebook Hacker Cup 2016: Qualification Round 1.1 (SOLVED)

Post by MaxV » Mon Jan 11, 2016 3:50 pm

Boomerang Constellations

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
Input begins with an integer T, the number of nights on which you look out at the sky. For each night, there is first a line containing the integer N. Then, N lines follow, the ith of which contains the space-separated integers Xi and Yi.

Output
For the ith night, print a line containing "Case #i: " followed by the number of boomerang constellations in the night sky.

Constraints
1 ≤ T ≤ 50
1 ≤ N ≤ 2,000
-10,000 ≤ Xi, Yi ≤ 10,000

Explanation of Sample
On the first night, every pair of stars is a unique distance apart, so there are no boomerang constellations. On the second night, there are 4 boomerang constellations. One of them consists of the line segments (0,0)-(0,2) and (0,2)-(0,4).
Example input

Code: Select all

5
3
0 0
0 1
0 3
5
0 0
0 1
0 2
0 3
0 4
4
0 0
0 100
100 0
100 100
4
0 0
-3 4
0 5
-5 0
6
5 6
6 5
7 6
6 7
7 8
8 7
Example output:

Code: Select all

Case #1: 0
Case #2: 4
Case #3: 4
Case #4: 3
Case #5: 12
Last edited by MaxV on Mon Feb 01, 2016 3:57 pm, edited 3 times in total.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Facebook Hacker Cup 2016: Qualification Round 1.1

Post by MaxV » Mon Jan 11, 2016 5:59 pm

this is my solution, but it isn't optimized:

########CODE#######
local ciclo, notti

on mouseUp
put empty into field 2
put 0 into ciclo
put line 1 of field 1 into notti
esegui2
end mouseUp

on esegui inizio
add 1 to ciclo
put "Case #" & ciclo & ": 0" after field 2
put line inizio of field 1 into fine
put inizio + fine into fine
put 1 + inizio into inizio
put line inizioto fine of field 1 into stelle
#answer stelle
combinazioni stelle
#finito
if ciclo < notti then
put CR after field 2
esegui (fine + 1)
end if
end esegui

on combinazioni stelle
repeat with i=1 to (the number of lines of stelle - 2)
repeat with j=(i+1) to (the number of lines of stelle - 1)
repeat with k=(j+1) to the number of lines of stelle
put line i of stelle & comma & line j of stelle & comma & line k of stelle & RETURNafter tComb
end repeat
end repeat
end repeat
filter lines of tComb without empty
#answer number of lines of stelle , the number of lines of tComb
verificaBoomerang tComb
end combinazioni

on verificaBoomerang tComb
repeat for each line tLine in tComb
put (word 1 of item 1 of tLine - word 1 of item 2 of tLine)^2 into dX
put (word 2 of item 1 of tLine - word 2 of item 2 of tLine)^2 into dY
put sqrt(dX + dY) into d1
put (word 1 of item 1 of tLine - word 1 of item 3 of tLine)^2 into dX
put (word 2 of item 1 of tLine - word 2 of item 3 of tLine)^2 into dY
put sqrt(dX + dY) into d2
put (word 1 of item 2 of tLine - word 1 of item 3 of tLine)^2 into dX
put (word 2 of item 2 of tLine - word 2 of item 3 of tLine)^2 into dY
put sqrt(dX + dY) into d3
if d1 = d2 then
add 1 to the last word of field 2
next repeat
END IF
if d1 = d3 then
add 1 to the last word of field 2
next repeat
END IF
if d3 = d2 then
add 1 to the last word of field 2
next repeat
END IF
end repeat
end verificaBoomerang
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply