{"id":9958,"date":"2026-12-03T13:00:12","date_gmt":"2026-12-03T13:00:12","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=9958"},"modified":"2026-12-03T13:06:00","modified_gmt":"2026-12-03T13:06:00","slug":"advent-of-code-2026-day-2-in-apl","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2026\/12\/advent-of-code-2026-day-2-in-apl\/","title":{"rendered":"Advent of Code 2026: Day 2 in APL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/adventofcode.com\/\" data-type=\"link\" data-id=\"https:\/\/adventofcode.com\/\" target=\"_blank\" rel=\"noopener\">Advent of Code<\/a> is an annual challenge in which a new programming puzzle is released every day for the first 12 days of December. It\u2019s an opportunity to solve problems, and we always see some great solutions in Dyalog APL. In this post I&#8217;m going to look at some ways to solve yesterday&#8217;s problem. <strong>Spoilers ahead!<\/strong><\/p>\n\n\n\n<h2 id=\"part-1\" class=\"wp-block-heading\">Part 1<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/adventofcode.com\/2026\/day\/2\">Yesterday\u2019s problem<\/a> asks us to imagine a series of differently-coloured waypoints in a field. These waypoints are <strong>r<\/strong>ed, <strong>o<\/strong>range, <strong>y<\/strong>ellow, <strong>g<\/strong>reen, <strong>b<\/strong>lue, and <strong>v<\/strong>iolet. They\u2019re arranged in a repeating pattern in the field like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>roygbvroygbvroygbvroy..\n\u2514\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500\u2500\u2500\u2518\u2514\u2500\u2500<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019re given a sequence of waypoint colours for a robot to visit, without backtracking and without visiting the same waypoint twice. The robot starts on the initial <code>r<\/code>, so the sequence <code>obbvb<\/code> means the robot takes this path:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code> \u250c\u2192\u2500\u2510     \u250c\u2510\nroygbvroygbvroygbv..\n\u2514\u2518  \u2514\u2500\u2500\u2192\u2500\u2500\u2518\u2514\u2500\u2192\u2500\u2500\u2518\n\u2514\u2500\u2500 16 steps \u2500\u2500\u2500\u2518<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the robot travels 16 steps. How can I write an APL program to work this out? For now, I&#8217;ll take <code class=\"language-apl\">x\u2190'obbvb'<\/code> so that I can visualise what\u2019s going on. Later, I can load the full input from a file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">My first step was to prepend the inital red waypoint \u2013 the robot\u2019s starting position:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x\u2190'obbvb'\n      x\u2190'r',x<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I started by making the input numeric, so I can take the difference between elements to find how far apart they are:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>Convert  r o y g b v\nto       0 1 2 3 4 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I did this by looking up the input in <code>'roygbv'<\/code> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      \u2395IO\u21900\n      i\u2190'roygbv'\u2373x\n      i\n0 1 4 4 5 4\n      \u235d x: r o b b v b\n      \u235d i: 0 1 4 4 5 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019m using <code class=\"language-apl\">\u2395IO\u21900<\/code> so that <code class=\"language-apl\">r<\/code> corresponds to <code class=\"language-apl\">0<\/code>. Now I can find the size of the jumps from one waypoint to the next by taking pairwise differences:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      \u00af2-\/i\n1 3 0 1 \u00af1\n      \u235d r o b b v b\n      \u235d 0 1 4 4 5 4\n      \u235d  1 3 0 1 \u00af1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is close to what I need: I have the positive jumps from <code class=\"language-apl\">r<\/code> to <code class=\"language-apl\">o<\/code>, <code class=\"language-apl\">o<\/code> to <code class=\"language-apl\">b<\/code>, and <code class=\"language-apl\">b<\/code> to <code class=\"language-apl\">v<\/code>. There are two things to fix, though. I need the jump from the first <code class=\"language-apl\">b<\/code> to the next to be a whole <code class=\"language-apl\">6<\/code>, but currently it\u2019s <code class=\"language-apl\">0<\/code>, since we don\u2019t stay in place. I also need the jump from the <code class=\"language-apl\">v<\/code> to the next <code class=\"language-apl\">b<\/code> to be <code class=\"language-apl\">5<\/code>, not <code class=\"language-apl\">\u00af1<\/code>, since we\u2019re not supposed to backtrack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can take advantage of the repeating pattern to fix the negative numbers. Imagine the colours arranged in a circle:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>  r\nv   o\nb   y\n  g<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Currently, the distance from <code class=\"language-apl\">v<\/code> to <code class=\"language-apl\">b<\/code> is <code class=\"language-apl\">\u00af1<\/code>, which can be thought of as stepping one place anticlockwise around this circle. We can also get from <code class=\"language-apl\">v<\/code> to <code class=\"language-apl\">b<\/code> by stepping <code class=\"language-apl\">5<\/code> places clockwise. I can normalise our negative distances to positive ones by taking the residue mod 6, which maps any number of steps around this circle to the equivalent number of steps from <code class=\"language-apl\">0 1 2 3 4 5<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      6|\u00af2-\/i\n1 3 0 1 5\n      \u235d r o b b v b\n      \u235d 0 1 4 4 5 4\n      \u235d  1 3 0 1 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I still need to change the <code class=\"language-apl\">0<\/code> (the distance from <code class=\"language-apl\">b<\/code> to itself) to a <code class=\"language-apl\">6<\/code> (the distance from a <code class=\"language-apl\">b<\/code> to the next <code class=\"language-apl\">b<\/code>). An easy way to do this is to add <code class=\"language-apl\">6<\/code> at the places that currently contain a <code class=\"language-apl\">0<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      +d\u21906|\u00af2-\/i\n1 3 0 1 5\n      6\u00d70=d\n0 0 6 0 0\n      d+\u21906\u00d70=d\n      d\n1 3 6 1 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now that I have the correct distances from each waypoint on the robot\u2019s path to the next, I just need to sum them for the total distance. Putting it all together:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      \u2395IO\u21900\n      x\u2190'obbvb'\n      x\u2190'r',x       \u235d start at 'r'\n      i\u2190'roygbv'\u2373x  \u235d make into numbers\n      d\u21906|\u00af2-\/i     \u235d distances, non-negative\n      d+\u21906\u00d70=d      \u235d same colour \u2192 jump to next block\n      +\/d           \u235d total distance travelled\n16<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You might prefer to present this as a one-liner:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x\u2190'obbvb'\n      +\/d+6\u00d70=d\u21906|\u00af2-\/'roygbv'\u2373'r',x\n16<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This way, the name <code class=\"language-apl\">i<\/code> is not needed. If you like <a href=\"docs.dyalog.com\/21.0\/programming-reference-guide\/introduction\/trains\/\">tacit programming<\/a>, you can also avoid using the name <code class=\"language-apl\">d<\/code> to get a solution that uses no extra names:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x\u2190'obbvb'\n      +\/(\u22a2+6\u00d70=\u22a2)6|\u00af2-\/'roygbv'\u2373'r',x\n16<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So far, I&#8217;ve been using a small example input. I can save the real input saved in a file called <strong>input.txt<\/strong>, and load it:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x\u2190\u00af1\u2193\u2283\u2395NGET 'input.txt'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"language-apl\">\u2395NGET<\/code> will load the file, ensuring that there is a trail line feed character. I don\u2019t need that, so I drop it from the end.<\/p>\n\n\n\n<h2 id=\"part-2\" class=\"wp-block-heading\">Part 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The second part of yesterday\u2019s problem asks how far the robot would travel if we rearranged its target waypoints, one at a time, so that each jump is as far as possible. For instance, if the input was <code>rybryb<\/code>, we would re-arrange it to <code>rrbbyy<\/code>. This is because the first 2 jumps (initial <code>r<\/code> to <code>r<\/code>, then another <code>r<\/code> to <code>r<\/code>) are as long as possible (<code>6<\/code> each). The next jump (<code>r<\/code> to <code>b<\/code>, length <code>5<\/code>), is as long as we can make it with what we have left, and so on.<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>   \u2192  r  \u2192  r \u2192 b  \u2192  b \u2192 y  \u2192  y  (rrbbyy)\n      \u250c\u2500\u2500\u2192\u2500\u2500\u2510   \u250c\u2500\u2500\u2192\u2500\u2500\u2510   \u250c\u2500\u2500\u2192\u2500\u2500\u2510\nroygbvroygbvroygbvroygbvroygbvroygbv..\n\u2514\u2500\u2500\u2192\u2500\u2500\u2518     \u2514\u2500\u2192\u2500\u2518     \u2514\u2500\u2192\u2500\u2518\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 32 steps \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One way to solve this problem is to re-arrange the input explicitly, and then do the same distance calculation that I did in part 1.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do that, notice that when we can jump from a waypoint to another waypoint with the same colour, we\u2019re maximising the size of the jump. So we know that colours must be grouped together, always taking the opportunity to jump to a waypoint with the same colour \u2013 we already saw that above, where the best arrangement resulted from grouping the colours together (<code class=\"language-apl\">rrbbyy<\/code>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that I know that the colours are grouped, I need to know what order the groups should be in. Since I&#8217;m implicitly starting on an <code class=\"language-apl\">r<\/code> waypoint, the first group has to be all the <code class=\"language-apl\">r<\/code>s. I want each jump between groups to be as long as possible, so it should be to the latest-occuring colour that hasn\u2019t been used up yet. That is, I want to start with <code class=\"language-apl\">r<\/code>, then visit the colours in the <em>reverse<\/em> order they appear in <code>roygbv<\/code>. So, the order is <code>r\u2192v\u2192b\u2192g\u2192y\u2192o<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can sort our input by mapping each colour to a number, with larger numbers for the later colours, then using these numbers to sort the original input. This mapping can be done with an <em>index-of<\/em>, as I did earlier, mapping colours to positions. Using the examples from the problem specification:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x&#91;\u234b'rvbgyo'\u2373x\u2190'rybryb']\nrrbbyy\n      x&#91;\u234b'rvbgyo'\u2373x\u2190'vrvbvg']\nrvvvbg\n      x&#91;\u234b'rvbgyo'\u2373x\u2190'groovyyoyo']\nrvgyyyoooo\n      x&#91;\u234b'rvbgyo'\u2373x\u2190'roygbvroygbvroygbv']\nrrrvvvbbbgggyyyooo<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I can now apply the distance calculation from part 1. For the first example, the re-arranged path takes 32 steps:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      +\/(\u22a2+6\u00d70=\u22a2)6|\u00af2-\/'roygbv'\u2373'r',x&#91;\u234b'rvbgyo'\u2373x\u2190'rybryb']\n32<\/code><\/pre>\n\n\n\n<h2 id=\"part-2---another-way\" class=\"wp-block-heading\">Part 2 &#8211; Another Way<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While I <em>can<\/em> re-arrange our input explicitly, this isn\u2019t the only approach to the problem. <a href=\"www.dyalog.com\/blog\/2023\/08\/welcome-stefan-kruger\/\">Stefan Kruger<\/a> came up with a cool alternative.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that, after re-arranging our input, most of the jumps have distance <code class=\"language-apl\">6<\/code>. The only ones that don\u2019t occur when we change group:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      (\u22a2+6\u00d70=\u22a2)6|\u00af2-\/'roygbv'\u2373'rrrvvvbbbgggyyyooo'\n6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This means that the total distance will be <code class=\"language-apl\">(6\u00d7\u2262x)-a little bit<\/code>. The extra bit that needs to be subtracted is the total distance from each colour <em>back<\/em> to the colour that is being jumped to. For example, changing from a group of <code class=\"language-apl\">b<\/code>s to a group of <code class=\"language-apl\">y<\/code>s, results in a situation like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>            \u250c\u2500\u25006\u2500\u2500\u2510\n            \u2502   \u250c2\u2524\n  \u2500\u2500\u2500\u2500\u2510     \u250c\u25004\u2500\u2510     \u250c\u2500\u2500\u2500\n..roygbvroygbvroygbvroygbv..\n      \u2514\u2500\u25006\u2500\u2500\u2518   \u2514\u2500\u25006\u2500\u2500\u2518\n   \u2192  b  \u2192  b \u2192 y  \u2192  y  \u2192<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since the first <code class=\"language-apl\">y<\/code> is a distance of <code class=\"language-apl\">2<\/code> behind the next <code class=\"language-apl\">b<\/code>, it\u2019s that <code class=\"language-apl\">2<\/code> that we\u2019re subtracting from a maximum jump of <code class=\"language-apl\">6<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Eventually, we reach the last group, which means we\u2019ve subtracted enough to go from <code class=\"language-apl\">r<\/code> (which we definitely start on) to the last colour. This means that any group transitions in the middle don\u2019t actually matter! Since we know we\u2019re starting on <code class=\"language-apl\">r<\/code>, and we know what colour we end on, we know exactly how much to subtract.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the last group is a group of <code class=\"language-apl\">v<\/code>s, we only step back one place from <code class=\"language-apl\">r<\/code>, so we only subtract <code class=\"language-apl\">1<\/code>. If the last group is a group of <code class=\"language-apl\">b<\/code>s, we subtract <code class=\"language-apl\">2<\/code>, and so on. If the last group is a group of <code class=\"language-apl\">r<\/code>s, then there is only one group, and we subtract nothing.<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>Last group:  r v b g y o\nSubtract:    0 1 2 3 4 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ve ordered the colours by the order in which they will appear when re-arranged. I want to subtract the corresponding value of the last group, which will be the largest subtraction amount available. Since the subtraction amounts happen to be <code class=\"language-apl\">\u23736<\/code> (with <code class=\"language-apl\">\u2395IO\u21900<\/code>), I can find them very elegantly with a lookup:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x\u2190'rybryb'\n      'rvbgyo'\u2373x\n0 4 2 0 4 2\n      \u2308\/'rvbgyo'\u2373x\n4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This tells me that the last group (the group of <code class=\"language-apl\">y<\/code>s) ends <code class=\"language-apl\">4<\/code> steps behind where I would have ended if I took steps of size <code class=\"language-apl\">6<\/code> each time. With this, I finally know the \u2018little bit\u2019 to subtract from <code class=\"language-apl\">6\u00d7\u2262x<\/code> to obtain the total length of <code class=\"language-apl\">32<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-apl\"><code>      x\u2190'rybryb'\n      (6\u00d7\u2262x)-\u2308\/'rvbgyo'\u2373x\n32<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Isn\u2019t that beautiful! I had a lot of fun solving yesterday\u2019s problem and discussing it with others. Give the next few days of Advent of Code a try, and let us know if you come up with anything interesting!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Advent of Code is an annual challenge in which a new programming puzzle is released every day for the first 12 days of December. It\u2019s an opportunity to solve problems, and we always see some great solutions in Dyalog APL. &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2026\/12\/advent-of-code-2026-day-2-in-apl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-9958","post","type-post","status-publish","format-standard","hentry","category-recreational"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=9958"}],"version-history":[{"count":9,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9958\/revisions"}],"predecessor-version":[{"id":9971,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9958\/revisions\/9971"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=9958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=9958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=9958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}