/
ch-2.pl
executable file
·47 lines (34 loc) · 945 Bytes
/
ch-2.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
our $first = 1;
sub _make_move {
my ( $size, $x, $y, $this_path ) = @_;
if ( $x == $size and $y == $size ) {
# We have a solution
print $first ? "Output: $this_path" : ", $this_path";
$first = 0;
return 1;
}
my $count = 0;
if ( $y < $size ) {
# Move diagonally left
$count += _make_move( $size, $x, $y + 1, "${this_path}L" );
# Move diagonally right
$count += _make_move( $size, $x + 1, $y + 1, "${this_path}R" );
}
if ( $x < $y ) {
# Move right
$count += _make_move( $size, $x + 1, $y, "${this_path}H" );
}
return $count;
}
sub main {
my $lines = shift;
die "Lines must be a positive integer" if $lines !~ /^[1-9][0-9]*$/;
my $count = _make_move( $lines, 0, 0, '', 0 );
say '';
say "A total of $count paths found";
}
main(@ARGV);