/
ch-2.pl
executable file
·77 lines (58 loc) · 1.82 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
use v5.16; # The Weekly Challenge - 2023-07-03
use utf8; # Week 224 task 2 - Additive number
use strict; # Peter Campbell Smith
use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
my ($solution, @string, $j);
additive_number('12354782');
additive_number('199100199299498');
additive_number('0030047');
additive_number('314159');
# create a long one
@string = (28,74);;
for $j (0 .. 19) {
push @string, $string[-1] + $string[-2];
}
additive_number(join('', @string));
sub additive_number {
my $string;
# initialise
$solution = '';
$string = shift @_;
# start recursion
test([], $string);
# results
say qq[\nInput: \$string = '$string'];
say qq[Output: ] . ($solution ? $solution : 'false');
}
sub test {
# test 1 further number (maybe several digits) along $string
my (@numbers, $string, $count, $length, $j, @new_numbers, $new_string, $keep_going);
# we can stop if a solution has been found
return if $solution;
# initialise
@numbers = @{$_[0]};
$string = $_[1];
$count = scalar @numbers;
# if we have only 1 or 2 numbers they must be valid and we don't need to test them
if ($count < 3) {
$keep_going = 1;
# we check that the last 3 numbers are additive, and if the string is now empty we have a solution
} else {
$keep_going = $numbers[-3] + $numbers[-2] == $numbers[-1];
if ($keep_going and $string eq '') {
$solution = join(', ', @numbers);
return;
}
}
# $keep_going is true if the sequence of @numbers is good but incomplete
return unless $keep_going;
# try using the next 1, 2, 3 ... digits from $string
$length = length($string);
for $j (1 .. $length) {
@new_numbers = (@numbers, substr($string, 0, $j));
$new_string = $j == $length ? '' : substr($string, $j);
# and recurse with those
test(\@new_numbers, $new_string);
}
}