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/perl
use 5.012;
use Test2::V0;
# Enable trace output:
my $verbose = 1;
our $level;
sub trace {
say ' ' x $level, @_ if $verbose;
}
# The task states: "[the] value at each index determines how far you are
# allowed to jump further". So I'll regard shorter jumps as valid.
# There wouldn't be much fun otherwise - and it's called a game!
# Recursive jump game. Try jumps from the maximum allowed length
# referenced by $maxjump[0] down to 1. On failure, set the value to a
# negative value preventing subsequent jumps to this position.
sub jump_game;
sub jump_game {
my @maxjump = @_;
# Convert the given numbers into references to them. This enables
# the modification of the original values through array slices.
# Transform only once.
@maxjump = map \$_, @maxjump unless ref $maxjump[0];
local $level = ($level // -1) + 1;
trace "at (@{[map $$_, @maxjump]})";
# Jump length from max down to 1.
for my $jump (reverse 1 .. ${$maxjump[0]}) {
# If we can jump beyond the end, we can hit it as well.
if ($jump > $#maxjump) {
$jump = $#maxjump
}
# Don't ride a dead horse.
elsif (${$maxjump[$jump]} <= 0) {
trace "avoid jump $jump";
next;
}
trace "jump $jump:";
trace('hit the end'), return 1 if $jump == $#maxjump;
# Recurse into the remaining numbers from the jump target
# onwards.
return 1 if jump_game @maxjump[$jump .. $#maxjump];
}
trace 'failed';
# Record current failure by setting max to a negative value. Any
# value <= 0 would do, but this visibly preserves the structure of
# the data when the trace is enabled.
${$maxjump[0]} *= -1;
0;
}
ok jump_game(1, 2, 1, 2), 'Example 1';
ok ! jump_game(2, 1, 1, 0, 2), 'Example 2';
# No solution without short jumps:
ok jump_game(2, 2, 0, 2, 9, 3, 0, 0, 0, 1), 'step back and jump precise';
ok ! jump_game(6, 5, 4, 3, 0, 0, 0, 1), 'track failures';
ok jump_game(2, 8, 2, 0, 1, 0, 5, 2, 0, 1, 0, 1), 'jump game!';
ok ! jump_game(2, 8, 2, 0, 1, 0, 4, 2, 0, 1, 0, 1), 'too short';
done_testing;
|