{"id":274,"date":"2009-05-04T17:49:56","date_gmt":"2009-05-05T00:49:56","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1281"},"modified":"2009-05-04T17:49:56","modified_gmt":"2009-05-05T00:49:56","slug":"hill-climbing","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=274","title":{"rendered":"Hill Climbing"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar target:Array = (&quot;actionsnippet&quot;).split(&quot;&quot;);\r\nvar leng:int=target.length;\r\nvar iterations:int = 0;\r\n\r\nvar alphabet:Array = (&quot;abcdefghijklmnopqrstuvwxyz&quot;).split(&quot;&quot;);\r\nvar search:Array = randomString();\r\nvar indices:Array = new Array();\r\nfor (var i:int = 0; i&lt;leng; i++) indices.push(i);\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tfor (var i:int = 0; i&lt;10; i++){\r\n\t\tif (indices.length &gt; 0){\r\n\t\t   var ii:int = int(Math.random()*indices.length);\r\n\t\t   var index:int = indices&#x5B;ii];\r\n\t\t \r\n\t\t   search&#x5B;index] = randomChar();\r\n\t\t   \r\n\t\t   if (search&#x5B;index] == target&#x5B;index]){\r\n\t\t\t   indices.splice(ii,1);   \r\n\t\t   }\r\n\t\t   trace(search);\r\n\t\t   iterations++;\r\n\t\t}else{\r\n\t\t\ttrace(&quot;found after &quot;+iterations+&quot; iterations&quot;);\r\n\t\t\tremoveEventListener(Event.ENTER_FRAME, onLoop);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n}\r\nfunction randomChar():String { return alphabet&#x5B;int(Math.random()*alphabet.length)]; };\r\nfunction randomString():Array {\r\n\tvar str:Array = new Array();\r\n\tfor (var i:int = 0; i&lt;leng; i++) {\r\n\t\tstr.push(randomChar());\r\n\t}\r\n\treturn str;\r\n}\r\n<\/pre>\n<p>A little random hill climbing&#8230;<\/p>\n<p>Here are the last few iterations&#8230; lowest number of iterations I noticed was round 250&#8230;<br \/>\n<code><br \/>\n....<br \/>\nj,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\ny,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\ns,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\nw,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\ne,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\nj,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\nz,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\nl,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\nf,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\na,c,t,i,o,n,s,n,i,p,p,e,t<br \/>\nfound after 361 iterations<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>var target:Array = (&quot;actionsnippet&quot;).split(&quot;&quot;); var leng:int=target.length; var iterations:int = 0; var alphabet:Array = (&quot;abcdefghijklmnopqrstuvwxyz&quot;).split(&quot;&quot;); var search:Array = randomString(); var indices:Array = new Array(); for (var i:int = 0; i&lt;leng; i++) indices.push(i); addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { for (var i:int = 0; i&lt;10; i++){ if (indices.length &gt; 0){ var ii:int = int(Math.random()*indices.length); var index:int = indices&#x5B;ii]; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,94,21],"tags":[3,4],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-arrays","category-misc","category-strings","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}