{"id":6788,"date":"2017-12-02T15:57:00","date_gmt":"2017-12-02T20:57:00","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=6788"},"modified":"2017-12-02T15:57:38","modified_gmt":"2017-12-02T20:57:38","slug":"zamanskys-advent-of-code-1","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=6788","title":{"rendered":"Zamansky&#8217;s Advent of Code #1"},"content":{"rendered":"<p> I don&rsquo;t have anything else interesting to say today so let&rsquo;s take a look at Mike Zamansky&rsquo;s <a href=\"http:\/\/cestlaz.github.io\/\">post on the Advent of Code 2017 Day 1<\/a> problem. Zamansky has a nice discussion of solving this with Python and the steps you might go through to arrive at a clean solution. You should read his post to see the problem statement. When I saw the problem, the first thing I thought of was using one of the Lisp mapping functions to step through the digits. Here&rsquo;s my solution in Emacs Lisp: <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-emacs-lisp\">(<span style=\"color: #a020f0;\">defun<\/span> <span style=\"color: #0000ff;\">cksum<\/span> (captcha)\n  (<span style=\"color: #a020f0;\">interactive<\/span>)                         <span style=\"color: #b22222;\">;<\/span><span style=\"color: #b22222;\">for interactive testing<\/span>\n  (<span style=\"color: #a020f0;\">let<\/span> ((checksum 0) (digits (mapcar (<span style=\"color: #a020f0;\">lambda<\/span> (a) (- a 48)) captcha)))\n    (cl-mapc (<span style=\"color: #a020f0;\">lambda<\/span> (a b) (<span style=\"color: #a020f0;\">when<\/span> (= a b) (<span style=\"color: #a020f0;\">cl-incf<\/span> checksum a)))\n             digits (-rotate -1 digits))\n    checksum))\n\n(cksum <span style=\"color: #8b2252;\">\"234445662\"<\/span>) &#8594; 16\n<\/pre>\n<\/div>\n<p> The first thing you notice is that it&rsquo;s really a Common Lisp solution because it makes use of <code>cl-mapc<\/code> and <code>cl-incf<\/code>. You could do without the <code>cl-incf<\/code> but the Elisp <code>mapc<\/code> operates on only one sequence. You&rsquo;ll also need Magnar Sveen&rsquo;s Dash library but if you have any packages at all installed, it&rsquo;s probably already been pulled in; otherwise you&rsquo;ll need a <code>(require dash)<\/code>. <\/p>\n<p> We need two sequences because the logic depends on <code>seq1[i+1] = seq2[i]<\/code>. Notice that this handles the &ldquo;wraparound case&rdquo; as well. The second half of the problem is the same except the two lists should be the first and second half of the digits. One nice thing about the Emacs mapping functions is that they operate on any sequence, not just lists. That&rsquo;s convenient in this problem. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&rsquo;t have anything else interesting to say today so let&rsquo;s take a look at Mike Zamansky&rsquo;s post on the Advent of Code 2017 Day 1 problem. Zamansky has a nice discussion of solving this with Python and the steps &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=6788\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[26,18],"class_list":["post-6788","post","type-post","status-publish","format-standard","hentry","category-general","tag-elisp","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6788"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6788\/revisions"}],"predecessor-version":[{"id":6789,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6788\/revisions\/6789"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}