Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #293 ("Dominos" and "Boomerangs")

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2024-10-27 through 2024-11-02 is #293.

The tasks for challenge #293 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 293-1: Similar Dominos
Submitted by: Mohammad Sajid Anwar
You are given a list of dominos, @dominos. Write a script to
return the number of dominoes that are "similar" to any other
domino. $dominos[i] = [a, b] and $dominos[j] = [c, d] are
"similar" if either (a = c and b = d) or (a = d and b = c).

Example 1:
Input: @dominos = ([1, 3], [3, 1], [2, 4], [6, 8])
Similar Dominos are $dominos[0], $dominos[1],
so output is 2.

Example 2:
Input: @dominos = ([1, 2], [2, 1], [1, 1], [1, 2], [2, 2])
Similar Dominos are $dominos[0], $dominos[1], $dominos[3],
so output is 3.

This is just a matter of following the instructions.

Robbie Hatley's Perl Solution to The Weekly Challenge 293-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 293-2: Boomerang
Submitted by: Mohammad Sajid Anwar
You are given an array of points (x,y). Write a script to find
out if the given points are a boomerang. A boomerang is a set
of three points that are all distinct and not in a straight line.

Example 1
Input: @points = ( [1, 1], [2, 3], [3,2] )
Output: true

Example 2
Input: @points = ( [1, 1], [2, 2], [3, 3] )
Output: false

Example 3
Input: @points = ( [1, 1], [1, 2], [2, 3] )
Output: true

Example 4
Input: @points = ( [1, 1], [1, 2], [1, 3] )
Output: false

Example 5
Input: @points = ( [1, 1], [2, 1], [3, 1] )
Output: false

Example 6
Input: @points = ( [0, 0], [2, 3], [4, 5] )
Output: true

There are a number of different approaches one could take to this. Two approaches are to compare slopes or compare angles. However, I note that every three points define a (possibly-degenerate) triangle, and every triangle has a (possibly-zero) area given by Heron's Formula, which says that given a triangle with sides having lengths a,b,c, if we let s = (a+b+c)/2 then the area A = sqrt(s*(s-a)*(s-b)*(s-c)). If a triangle's area is at least 1/10000 of the maximum area a triangle of that semiperimeter can have, then I'll consider that triangle to be a "boomerang". (The max area of a triangle with semiperimeter s is when the triangle is equilateral, which gives max_area = s**2/sqrt(27).)

Robbie Hatley's Perl Solution to The Weekly Challenge 293-2

That's it for challenge 293; see you on challenge 294!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273