#!/usr/bin/perl -w # # Copyright (C) Nov, 2003 by Clifford Wolf # Free under the terms of GPL. # use strict; my $value=0; my @primes = (); $| = 1; $_=1; (1 x $_) !~ /^(11+)\1+$/ && push @primes,$_ while $_++ < 300; sub add_sub($$) { my @alternatives = (); my ($a, $n, $p, $d); my ($op, $num) = @_; my $max = $num-2 < 10 ? $num-2 : 10; return $op x $num if $num < 5; for $a (0 .. $max) { my @dividers = (); $alternatives[$a] = $op x $a; $n = $num - $a; foreach $p (@primes) { while ( $n != 1 && $n % $p == 0 ) { $n /= $p; if ( $#dividers >= 0 ) { $d = $dividers[$#dividers]; if ( $d * $p < 10 ) { $p *= $d; pop @dividers; } } push @dividers, $p; } } push @dividers, $n if $n != 1; $n = pop @dividers; if ( not defined $n ) { print "\n"; print "$num, $a\n"; exit; } $alternatives[$a] .= ">" x ($#dividers+1); foreach $d (@dividers) { $alternatives[$a].=("+" x $d)."[<"; } $alternatives[$a] .= $op x $n; foreach $d (reverse @dividers) { $alternatives[$a] .= ">-]"; } $alternatives[$a] .= "<" x ($#dividers+1); } $n = 0; $p = length $alternatives[0]; for $a (1 .. $max) { if ($p > length $alternatives[$a]) { $n = $a; $p = length $alternatives[$a]; } } return $alternatives[$n]; } sub make_text($) { my ($ch, $i); my $ret = ""; foreach $ch (split //, $_[0]) { $ret .= add_sub("+", ord($ch) - $value) if ord($ch) > $value; $ret .= add_sub("-", $value - ord($ch)) if ord($ch) < $value; $ret .= "."; $value = ord($ch); } return $ret; } my @prg = split //, make_text "Chaostreff !\n"; my $txt = << "EOT"; ##### #### #### #### ##### ##### ##### ##### ##### #### # # # # # # # # # # # # # # # # # # # # ## # ##### ## #### #### # # # ###### # # ## # # # ### # # #### # # # # # # # # # # # # # ##### # # #### #### # # # ##### # # ## EOT print "\n"; foreach (split //, $txt) { if ( $_ eq '#' ) { print defined $prg[0] ? shift @prg : '+'; } else { print; } } print "\n";