{"id":8994,"date":"2025-12-10T10:41:18","date_gmt":"2025-12-10T10:41:18","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=8994"},"modified":"2025-12-10T12:03:55","modified_gmt":"2025-12-10T12:03:55","slug":"advent-of-code-2025-day-1-in-apl","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2025\/12\/advent-of-code-2025-day-1-in-apl\/","title":{"rendered":"Advent of Code 2025 \u2013 Day 1 in APL"},"content":{"rendered":"<p><a href=\"https:\/\/adventofcode.com\/2025\" 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. We always see some great solutions in Dyalog APL. I&#8217;m going to look at the first problem, drawing on my own solution and some of those that have been shared across various message boards and in the <a href=\"https:\/\/aplwiki.com\/wiki\/Advent_of_Code#2020%E2%80%93\" target=\"_blank\" rel=\"noopener\">APL Wiki<\/a>.<\/p>\n<p><strong>Parsing<\/strong><\/p>\n<p><a href=\"https:\/\/adventofcode.com\/2025\/day\/1\" target=\"_blank\" rel=\"noopener\">Day 1&#8217;s problem<\/a> tasked us with uncovering the password to enter the North Pole by counting how often a dial points to zero in a sequence of rotations. Before we can solve the problem, we have to parse the input file we&#8217;re given. If you&#8217;re not interested in the parsing, you can move straight to <strong>Part One<\/strong>, but you&#8217;ll miss a cool new way to use the <code class=\"language-apl\">\u2395<\/code>-functions!<\/p>\n<p>The standard way to read a text file is to use the <a href=\"https:\/\/docs.dyalog.com\/20.0\/language-reference-guide\/system-functions\/nget\/\" target=\"_blank\" rel=\"noopener\"><code class=\"language-apl\">\u2395NGET<\/code><\/a> (<strong>N<\/strong>ative file <strong>GET<\/strong>) system function. A little known addition to <code class=\"language-apl\">\u2395NGET<\/code>, available in the recent <a href=\"https:\/\/www.dyalog.com\/dyalog\/dyalog-versions\/200.htm\">Dyalog v20.0<\/a> release, is the ability to load a file directly into a matrix. This is faster than loading each line as a character vector and mixing them together, and is very handy for Advent of Code problems, which often have rectangular input files.<\/p>\n<p>To parse the numbers given in the input file, it&#8217;s easy to use <code class=\"language-apl\">\u234e<\/code> to execute the text for each number as APL code, yielding the number as a result. For a fun exercise like Advent of Code, this is fine, but in production code, this can be very dangerous. If you&#8217;re <code class=\"language-apl\">\u234e<\/code>-ing text provided by a user, they could provide a malicious input like <code class=\"language-apl\">\u2395OFF<\/code>, or worse! This is why the system function <a href=\"https:\/\/docs.dyalog.com\/20.0\/language-reference-guide\/system-functions\/vfi\/\" target=\"_blank\" rel=\"noopener\"><code class=\"language-apl\">\u2395VFI<\/code><\/a> (<strong>V<\/strong>erify <strong>F<\/strong>ix <strong>I<\/strong>nput) exists, to parse numbers and <em>only<\/em> numbers. <code class=\"language-apl\">\u2395VFI<\/code> also allows you to configure the separators between numbers. For instance, you could use <code class=\"language-apl\">','\u2395VFI<\/code> to parse numbers separated by commas.<\/p>\n<p>Using <code class=\"language-apl\">\u2395NGET<\/code> and <code class=\"language-apl\">\u2395VFI<\/code> together gives a very neat way to parse the input, abusing the <code class=\"language-apl\">L<\/code>s and <code class=\"language-apl\">R<\/code>s as delimiters for the numbers.<\/p>\n<pre><code class=\"language-apl\">      \u2395IO\u21900\r\n      input\u2190\u2283\u2395NGET 'example.txt' 2 \u235d provide the flag 2, to load the file as a matrix\r\n      input\r\nL68\r\nL30\r\nR48\r\nL5\r\nR60\r\nL55\r\nL1\r\nL99\r\nR14\r\nL82\r\n      1\u2193,input \u235d our input numbers are delimited by 'L's and 'R's\r\n68L30R48L5 R60L55L1 L99R14L82\r\n      size\u21901\u2283'LR'\u2395VFI 1\u2193,input \u235d use \u2395VFI, telling it that 'L' and 'R' are the separators\r\n      size\r\n68 30 48 5 60 55 1 99 14 82\r\n<\/code><\/pre>\n<p>This is much faster than loading the input into separate lines, and executing the number on each one.<\/p>\n<pre><code class=\"language-apl\">      ]Runtime -c \"1\u2283'LR'\u2395VFI 1\u2193,\u2283\u2395NGET 'input.txt' 2\" \"(\u234e1\u2193\u22a2)\u00a8\u2283\u2395NGET 'input.txt' 1\"\r\n\r\n1\u2283'LR'\u2395VFI 1\u2193,\u2283\u2395NGET 'input.txt' 2 \u2192 6.9E\u00af4 |    0% \u2395\u2395\u2395\u2395\u2395\r\n(\u234e1\u2193\u22a2)\u00a8\u2283\u2395NGET 'input.txt' 1 \u2192 5.9E\u00af3        | +754% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n<\/code><\/pre>\n<p>In addition to the sizes, we also need to parse the direction of each rotation. This is a simple matter of comparing the first column of the input against <code class=\"language-apl\">'L'<\/code> or <code class=\"language-apl\">'R'<\/code>, choosing a <code class=\"language-apl\">1<\/code> for each <code class=\"language-apl\">'R'<\/code> (as a right turn is considered positive), and a <code class=\"language-apl\">\u00af1<\/code> for each <code class=\"language-apl\">'L'<\/code> (as a left turn is considered negative). The classic way to do this is with <code class=\"language-apl\">\u00af1*<\/code>.<\/p>\n<pre><code class=\"language-apl\">      input[;0]\r\nLLRLRLLLRL<code>\r\n      direction\u2190\u00af1*'L'=input[;0]\r\n      direction\r\n\u00af1 \u00af1 1 \u00af1 1 \u00af1 \u00af1 \u00af1 1 \u00af1\r\n<\/code><\/code><\/pre>\n<p>As an alternative, Reddit user u\/light_switchy <a href=\"https:\/\/reddit.com\/r\/adventofcode\/comments\/1pb3y8p\/2025_day_1_solutions\/nrsmkka\/\" target=\"_blank\" rel=\"noopener\">found the very nice train<\/a> <code class=\"language-apl\">'R'(=-\u2260)<\/code> for this.<\/p>\n<pre><code class=\"language-apl\">\u00a0     'R'(=-\u2260)input[;0]\r\n\u00af1 \u00af1 1 \u00af1 1 \u00af1 \u00af1 \u00af1 1 \u00af1\r\n<\/code><\/pre>\n<p>Tacit programming can be tricky to read. For the benefit of those of us who prefer explicit code, <code class=\"language-apl\">'R'(=-\u2260)x<\/code> is evaluated as <code class=\"language-apl\">('R'=x)-('R'\u2260x)<\/code>, which is the same as <code class=\"language-apl\">('R'=x)-('L'=x)<\/code>, since <code class=\"language-apl\">x<\/code> is made up of only <code class=\"language-apl\">'R'<\/code>s and <code class=\"language-apl\">'L'<\/code>s.<\/p>\n<p><strong>Part One<\/strong><\/p>\n<p>Many APLers have solved this year&#8217;s first problem, and the approach is so natural that everybody&#8217;s solution had a similar shape. By multiplying the size and direction of each rotation, we can find the difference in position that each rotation makes:<\/p>\n<pre><code class=\"language-apl\">      size\u00d7direction\r\n\u00af68 \u00af30 48 \u00af5 60 \u00af55 \u00af1 \u00af99 14 \u00af82\r\n<\/code><\/pre>\n<p>We can then use a sum scan to find the cumulative position of the dial after each rotation, remembering to include the starting position (<code class=\"language-apl\">50<\/code>):<\/p>\n<pre><code class=\"language-apl\">      +\\50,size\u00d7direction\r\n50 \u00af18 \u00af48 0 \u00af5 55 0 \u00af1 \u00af100 \u00af86 \u00af168\r\n<\/code><\/pre>\n<p>We are working with a circular dial, so instead of going above <code class=\"language-apl\">99<\/code>, or below <code class=\"language-apl\">0<\/code>, we want to wrap around the numbers on the dial. We can use <em>residue<\/em> (<code class=\"language-apl\">|<\/code>) to fix this:<\/p>\n<pre><code class=\"language-apl\">      100|+\\50,size\u00d7direction\r\n50 82 52 0 95 55 0 99 0 14 32\r\n<\/code><\/pre>\n<p>Finally, we are required to find the number of times the dial is left pointing at zero. Counting this is straightforward, the hard work has already been done:<\/p>\n<pre><code class=\"language-apl\">      +\/0=100|+\\50,size\u00d7direction\r\n3\r\n<\/code><\/pre>\n<p><strong>Part Two<\/strong><\/p>\n<p>For part two of today&#8217;s problem, we are asked to find not only the number of times the dial finished a rotation on zero, but also the number of times <em>any click<\/em> of the dial points it to zero.<\/p>\n<p>For the size of input we are given today, generating the position of the dial after every single click is an acceptable solution. You could do this using <code class=\"language-apl\">\u2373<\/code> and some transformations, but several people found a nice trick to transform the code from part one into exactly what we need.<\/p>\n<p>In part one, we represented the rotation <code class=\"language-apl\">L68<\/code> by the number <code class=\"language-apl\">\u00af68<\/code>, meaning 68 clicks in the negative direction. For this part, we want to represent each of those clicks independently, which we can do by replicating the direction by the size of the rotation, rather than multiplying it:<\/p>\n<pre><code class=\"language-apl\">      size\/direction\r\n\u00af1 \u00af1 \u00af1 \u00af1 \u00af1 \u00af1 \u00af1 \u00af1 \u00af1 \u00af1 ...\r\n<\/code><\/pre>\n<p>Using the same method as in part one, we can find the position of the dial after every single click, and count the number of times we see a zero:<\/p>\n<pre><code class=\"language-apl\">      +\/0=100|+\\50,size\/direction\r\n6\r\n<\/code><\/pre>\n<p>This is a change of just one character from our part one solution, very elegant! Therefore, our solution for day one is:<\/p>\n<pre><code class=\"language-apl\">      input\u2190\u2283\u2395NGET 'input.txt' 2   \u235d load the input as a matrix\r\n      size\u21901\u2283'LR'\u2395VFI 1\u2193,input     \u235d parse the size of each rotation\r\n      direction\u2190\u00af1*'L'=input[;0]   \u235d find the direction of each rotation\r\n      +\/0=100|+\\50,size\u00d7direction  \u235d solve part 1\r\n      +\/0=100|+\\50,size\/direction  \u235d solve part 2<\/code><\/pre>\n<p>If you&#8217;re interested in a more performant solution, there is a way to avoid generating all the intermediate positions of the dial. Handling the edge cases is tricky, but it runs much faster. This blog post is long enough already, so I&#8217;ll leave determining how it works to you!<\/p>\n<pre><code class=\"language-apl\">      ]Runtime -c \"+\/0=100|+\\50,size\/direction\" \"SecretFastSolution\"\r\n\r\n+\/0=100|+\\50,size\/direction \u2192 1.2E\u00af3 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\nSecretFastSolution \u2192 3.2E\u00af5          | -98% \u2395\r\n<\/code><\/pre>\n<p>Advent of Code 2025 is open for submissions until 12 December 2025, so it&#8217;s not too late to get involved! Trying the problems yourself, and then reading the approaches by others, is a fantastic way to learn new programming techniques. Happy APLing!<\/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. We always see some great solutions in Dyalog APL. I&#8217;m going to look at the first &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2025\/12\/advent-of-code-2025-day-1-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-8994","post","type-post","status-publish","format-standard","hentry","category-recreational"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/8994","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=8994"}],"version-history":[{"count":18,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/8994\/revisions"}],"predecessor-version":[{"id":9026,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/8994\/revisions\/9026"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=8994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=8994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=8994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}