{"id":24,"date":"2026-10-17T10:09:48","date_gmt":"2026-10-16T23:09:48","guid":{"rendered":"https:\/\/davidshoon.com\/?p=24"},"modified":"2026-10-17T10:09:48","modified_gmt":"2026-10-16T23:09:48","slug":"corewars-program","status":"publish","type":"post","link":"https:\/\/davidshoon.com\/?p=24","title":{"rendered":"Corewars program&#8230;"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The following corewars program is a statistically optimal program, which attacks a midpoint of a program, given the number of programs, coresize, average program size, and then their standard deviation. It&#8217;s just using pre-defined numbers, generated from a shell script&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>r: data 0\n\ncoresize \/ nprogs = 273\n\navg prog size: 35.65\n\nstd dev: 31.49\n\nthus, desired search points:\n\n&#91; |1dev| (midpoint) |1dev| ]\n\nmake next midpoint here: ---------------------> ^^^^^^\n\nmidpoint = interval \/ 2 = 136\n\nmain: move 0, &#91;r] # midpoint\nadd -31, r\nmove 0, &#91;r] # left 1dev\nadd 62, r\nmove 0, &#91;r] # right 1dev\nadd 74, r\n\njump main<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the corresponding shell script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\n\n# this calculates statistics from any list of numbers.\n\n# in particular, it's used to calculate the number of lines for each\n# corewars program.\n\n# wc -l *.cw ... which is then parsed by cut -d ' ' -f &#91;1,2,3,4,5,6]\n# to get all the tokens, and then \"grep -v\" to remove the text,\n# since the output of wc is not cut-friendly.\n\n# expavg is actually the average from a previous calculation, but\n# since this requires 2 passes, i couldn't be bothered. just\n# alter it yourself for the 2nd pass.\n\n\nr=0\nsum=0\navg=0\nnumprogs=0\nexpavg=35\n\nwhile read r ; do\n\n\tnumprogs=`expr $numprogs '+' 1`\n\n\tavg=`expr $r '+' $avg`\n\n\tr=`expr $r '-' $expavg`\n\n\ts=`expr $r '*' $r`\n\n\tsum=`expr $sum '+' $s`\n\ndone\n\necho \"numprogs = $numprogs\"\necho \"avg = $avg \/ numprogs\"\necho \"dev = sqrt ( $sum \/ numprogs )\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following corewars program is a statistically optimal program, which attacks a midpoint of a program, given the number of programs, coresize, average program size, and then their standard deviation. It&#8217;s just using pre-defined numbers, generated from a shell script&#8230; Here is the corresponding shell script:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":1,"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":25,"href":"https:\/\/davidshoon.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/25"}],"wp:attachment":[{"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidshoon.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}