aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/alexander-pankoff/perl/ch-2.pl
blob: e86839a89052a3fc05acce065f2e52b981a7bb68 (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
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw'say state signatures';
no warnings qw'experimental::signatures';

# TASK #2 › Rob The House
# Submitted by: Mohammad S Anwar
#
# You are planning to rob a row of houses, always starting with the first and moving in the same direction. However, you can’t rob two adjacent houses.
#
# Write a script to find the highest possible gain that can be achieved.
# Example 1:
#
# Input: @valuables = (2, 4, 5);
# Output: 7
#
# If we rob house (index=0) we get 2 and then the only house we can rob is house (index=2) where we have 5.
# So the total valuables in this case is (2 + 5) = 7.
#
#
# Example 2:
#
# Input: @valuables = (4, 2, 3, 6, 5, 3);
# Output: 13
#
# The best choice would be to first rob house (index=0) then rob house (index=3) then finally house (index=5).
# This would give us 4 + 6 + 3 =13.

use List::Util qw(all reduce sum0);

run() unless caller();

sub run() {
    my @valuables = @ARGV;
    die "Invalid input\n" unless all { m/^-?\d+$/ } @valuables;

    say rob_house(@valuables);
}

sub rob_house (@valuables) {
    my @tours = plan_tours($#valuables);

    my $best_tour = reduce sub {
        my @tour       = @$b;
        my $tour_value = sum0 map { $valuables[$_] } @tour;
        if (   $tour_value > $a->{value}
            || $tour_value == $a->{value} && @tour < @{ $a->{tour} } )
        {
            return {
                value => $tour_value,
                tour  => [@tour],
            };
        }

        return $a;

    }, { value => 0, tour => [] }, @tours;

    return $best_tour->{value};
}

sub plan_tours ( $max, $cur = 0 ) {
    return [] if $cur > $max;
    my @paths = (
        ( map { [ $cur, @$_ ] } plan_tours( $max, $cur + 2 ) ),
        plan_tours( $max, $cur + 1 )
    );

    return @paths;
}