# RogerBW's Blog

 Perl Weekly Challenge 44: goal-seeking 13 February 2020 I’ve been doing the Perl Weekly Challenges. The latest was about arranging numbers and arithmetical operations. For the first part, I visualised eight slots between the numbers, each of which could contain +, - or nothing at all. 3^8 is only 6,561, which should present no difficulty in an exhaustive search. ``````my @base=(1..9); my @sv=('','-','+'); my \$maxdepth=8; my @si=(0) x \$maxdepth; while (1) { `````` For each possible combination, we evaluate it and list it if it's a success. `````` my \$str=join('',map {\$base[\$_].\$sv[\$si[\$_]]} (0..\$maxdepth-1)).\$base[\$maxdepth]; my \$tot=eval(\$str); if (\$tot == 100) { print "\$str\n"; } `````` Then do a standard nested loop increment: start with slot 0 and carry forward when it overflows. `````` my \$i=0; while (\$i < \$maxdepth) { \$si[\$i]++; if (\$si[\$i] <= \$#sv) { last; } \$si[\$i]=0; \$i++; } if (\$i >= \$maxdepth) { last; } } `````` Perl6 is the same, but eval has been sort-of removed from the language; there's an EVAL function (surrounded with dire warnings), which is desperately slow. On my desktop box, my code above runs in 0.07 seconds; on the same machine, the Perl6 version, which is basically identical apart from syntax details, takes 51. For the second problem, I decided to do this the amusing way. So I build a list of (working total, operation chain) pairs, starting with 1 and an empty list. ``````my @seq=([1,[]]); my \$goal=200; while (1) { `````` Each time I pull an item off the top of the list, I see if it's the goal (in which case it's a shortest path to the goal, so we print the path and exit). `````` my \$s=shift @seq; if (\$s->[0] == \$goal) { print join(', ',map {['double','add 1']->[\$_]} @{\$s->[1]}),"\n"; last; } `````` Otherwise, we create two more list entries, one with each possible path from this point (doubling or adding). `````` push @seq,[\$s->[0]*2,[@{\$s->[1]},0]]; push @seq,[\$s->[0]+1,[@{\$s->[1]},1]]; } `````` On the other hand this is basically just a binary decomposition, if we regard the "double" operation as "add a zero at the end of the number" and the "add 1" operation as "turn the trailing zero to a 1". Thus the Perl6 solution: ``````my \$goal=200; my @stack; for sprintf('%b',\$goal).split('',:skip-empty) -> \$op { push @stack,'double'; if (\$op == 1) { push @stack,'add 1'; } } @stack.shift; @stack.shift; say join(', ',@stack); `````` Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one. Search 10 per page 20 per page 50 per page 100 per page Archive Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult Special All book reviews, All film reviews
Produced by aikakirja v0.1