aboutsummaryrefslogtreecommitdiff
path: root/challenge-044/lubos-kolouch/perl/ch-2.pl
blob: 75a6d5429b32255a8e4ed44aab721569dff235d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use strict;
use warnings;

my $goal    = 200;    # Target amount
my $current = 1;      # Current amount
my $moves   = 0;      # Number of moves

while ( $current < $goal ) {

    # Check if doubling the current amount gets us closer to the goal
    if ( $current * 2 <= $goal - $current - 1 ) {
        $current *= 2;
    }
    else {
        $current += 1;
    }
    $moves += 1;
}

print "To reach $goal, you need $moves moves.\n";