aboutsummaryrefslogtreecommitdiff
path: root/challenge-333/bob-lied/perl/ch-1.pl
blob: b049e22a70b0d4b17a9eeeaf8ba64bc558d93535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2025, Bob Lied
#=============================================================================
# ch-1.pl Perl Weekly Challenge 333 Task 1  Straight Line
#=============================================================================
# You are given a list of co-ordinates. Write a script to find out if the
# given points make a straight line.
# Example 1 Input: @list = ([2, 1], [2, 3], [2, 5])
#           Output: true
# Example 2 Input: @list = ([1, 4], [3, 4], [10, 4])
#           Output: true
# Example 3 Input: @list = ([0, 0], [1, 1], [2, 3])
#           Output: false
# Example 4 Input: @list = ([1, 1], [1, 1], [1, 1])
#           Output: true
# Example 5 Input: @list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000])
#           Output: true
#=============================================================================

use v5.42;


use Getopt::Long;
my $DoTest  = false;

GetOptions("test" => \$DoTest);
#=============================================================================

exit(!runTest()) if $DoTest;

# Use as ch-1.pl 1,1 2,2  3,3

my @point = map {  [split ',', $_] } @ARGV;
die "Need three points" unless @point == 3;

say isLine(@point) ? "true" : "false";

#=============================================================================
## https://math.stackexchange.com/questions/701862/how-to-find-if-the-points-fall-in-a-straight-line-or-not
sub isLine(@point)
{
    my @x = map { $_->[0] } @point;
    my @y = map { $_->[1] } @point;

    return ($y[1] - $y[0]) * ( $x[2] - $x[0] )  == ( $y[2] - $y[0] ) * ( $x[1] - $x[0] )
}

sub runTest
{
    use Test2::V0;

    is( isLine( [2,1],[2,3],[ 2,5] ),  true, "Example 1");
    is( isLine( [1,4],[3,4],[10,4] ),  true, "Example 2");
    is( isLine( [0,0],[1,1],[ 2,3] ), false, "Example 3");
    is( isLine( [1,1],[1,1],[ 1,1] ),  true, "Example 4");
    is( isLine( [1000000, 1000000], [2000000, 2000000], [3000000, 3000000] ), true, "Example 5");

    done_testing;
}