Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Boxes and pebbles exploration

View boxes-and-pebbles.p6
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
class Conf {
has @.boxes where *.all >= 0;
method gist { "[$.boxes]" }
}
 
sub conf(@boxes) { Conf.new(:@boxes) }
sub n(Conf $c) { $c.boxes.elems }
 
sub REPL($line) {
say "> $line";
my $result = EVAL $line;
say $result if defined $result;
}
 
REPL 'n(conf [2, 0])';
 
sub but(@list, &act) { my @new = @list; &act(@new); @new }
sub add($c, $to, $count) { conf $c.boxes.&but(*.[$to] += $count) }
sub remove($c, $from, $count) { conf $c.boxes.&but(*.[$from] -= $count) }
 
sub move($c, $from, $to) { $c.&remove($from, 2).&add($to, 1) }
my $OPTIMIZATION_1 = False;
sub moves-from($c, $from) {
sub to_hostage($t) { $OPTIMIZATION_1 ?? $c.boxes[$t] == 0 !! True }
(move($c, $from, $_) if to_hostage($_) for ^n($c))
}
my $OPTIMIZATION_2 = False;
sub moves($c) {
sub lower_limit { $OPTIMIZATION_2 ?? 3 !! 2 }
(moves-from($c, $_) if $c.boxes[$_] >= lower_limit for ^n($c))
}
 
REPL 'moves(conf [2, 0])';
 
sub has-empty-box($c) { so any($c.boxes) == 0 }
 
REPL 'has-empty-box(conf [2, 2, 2, 0])';
REPL 'has-empty-box(conf [2, 2, 2, 1])';
 
sub is-solvable($c) { !has-empty-box($c) || so is-solvable any moves $c }
 
REPL 'is-solvable(conf [2, 0])';
REPL 'is-solvable(conf [3, 0])';
 
sub tree-search($c, $indent = 0) {
say "\x20" x 4 * $indent, $c;
for moves($c) {
tree-search($_, $indent + 1);
}
}
 
REPL 'tree-search(conf [4, 0, 0])';
 
sub add-pebble($c, $to) { conf $c.boxes.&but(*.[$to] += 1) }
sub add-pebble-anywhere($c) { (add-pebble($c, $_) for ^n($c)) }
 
REPL 'add-pebble-anywhere(conf [2, 0])';
 
sub is-answer($c) { !is-solvable($c) && so is-solvable all add-pebble-anywhere($c) }
 
REPL 'is-answer(conf [2, 0])';
REPL 'is-answer(conf [4, 0, 0])';
 
sub hostages($c) { +$c.boxes.grep(0) }
 
REPL 'hostages(conf [2, 0])';
REPL 'hostages(conf [3, 0, 0])';
 
sub heroes($c) { [+] ($c.boxes »-» 1) »div» 2 »max» 0 }
 
REPL 'heroes(conf [2, 0])';
REPL 'heroes(conf [3, 3, 0])';
 
$OPTIMIZATION_1 = True;
REPL 'tree-search(conf [4, 0, 0])';
 
$OPTIMIZATION_2 = True;
REPL 'tree-search(conf [4, 0, 0])';
 
sub one-more-hostage-than-heroes($c) { hostages($c) == heroes($c) + 1 }
 
REPL 'one-more-hostage-than-heroes(conf [2, 0])';
REPL 'one-more-hostage-than-heroes(conf [3, 1, 0])';
 
sub random-box { Bool.pick ?? 0 !! (1..5).pick }
 
sub random-conf {
my $n = (0..5).pick;
conf [random-box() xx $n];
}
 
sub quickcheck(&prop, $N = 1000) {
for ^$N {
print "." if $_ %% 20;
my $c = random-conf;
return "Counterexample: $c.gist()" unless &prop($c);
}
return "All $N cases passed.";
}
 
sub infix:«⇒»($premise, $conclusion) { !$premise || $conclusion }
 
sub if-answer-then-one-more-hostage($c) {
is-answer($c) ⇒ one-more-hostage-than-heroes($c);
}
 
REPL 'quickcheck &if-answer-then-one-more-hostage';
 
sub if-one-more-hostage-then-answer($c) {
one-more-hostage-than-heroes($c) ⇒ is-answer($c);
}
 
REPL 'quickcheck &if-one-more-hostage-then-answer';
 
sub all-hostages-or-everyday-heroes($c) { so $c.boxes.all %% 2 }
sub if-one-more-hostage-and-all-hostages-or-everyday-heroes-then-answer($c) {
(one-more-hostage-than-heroes($c)
&& all-hostages-or-everyday-heroes($c))
is-answer($c)
}
 
REPL 'quickcheck &if-one-more-hostage-and-all-hostages-or-everyday-heroes-then-answer';
 
sub one-more-hostage-and-all-hostages-or-everyday-heroes-means-answer($c) {
(one-more-hostage-than-heroes($c)
&& all-hostages-or-everyday-heroes($c))
== is-answer($c)
}
 
REPL 'quickcheck &one-more-hostage-and-all-hostages-or-everyday-heroes-means-answer';
 
sub pebbles-are-twice-boxes-minus-two-and-all-boxes-even-means-answer($c) {
([+]($c.boxes) == 2 * n($c) - 2 && so($c.boxes.all %% 2))
== is-answer($c)
}
 
REPL 'quickcheck &pebbles-are-twice-boxes-minus-two-and-all-boxes-even-means-answer';
 
sub partitions($n) {
uniq :as(*.Str), gather {
take [$n];
for 1..^$n -> $x {
take ([($x, .list).sort] for partitions($n - $x));
}
}
}
 
sub double(@list) { @list »*» 2 }
sub pad(@list, $size) { [@list, 0 xx ($size - @list.elems)] }
sub all-answers($n) { (.reverse.&double.&pad($n) for partitions($n - 1)) }
 
sub array-cmp(@xs, @ys) { [||] @xs Z<=> @ys }
 
for 1..* -> $n {
my @answers = all-answers($n).sort(&array-cmp);
say "{@answers.elems} answers of size $n:";
say " ", .&conf for @answers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.