{
  "id": 614092,
  "title": "1st place write-up: Code Golf International",
  "content": "<p>First and foremost, we are very grateful to the organizers of the competition for such a unique opportunity to engage in competitive code golf with the broader Kaggle community. The last three months have been a thrilling experience for all of us, and finishing in first is a tremendous honor.</p>\n<h1>Overview</h1>\n<h2>Solutions</h2>\n<p>Our code repository is <a href=\"https://github.com/Seek64/NeurIPS-Code-Golf-2025\" target=\"_blank\">here</a>. The final contest submission was from <a href=\"https://github.com/Seek64/NeurIPS-Code-Golf-2025/tree/9a5d156eabdc35732688751c091bf57277f70c21\" target=\"_blank\">this commit</a>. To reproduce our entry, you can download the repo and simply run <code>python auto_zip.py</code>, which will create the <code>submission.zip</code> file.</p>\n<h2>Approach</h2>\n<p>Our team consisted of 5 experienced code golfers. Our approach to this competition was very manual, with all solutions authored by hand. The only important automation was zlib-based compression, which applied to 21 of the 400 tasks (more on that below).</p>\n<h2>Results</h2>\n<p>Our good results in the competition can be mainly attributed to expertise in golfing and a large investment of time. To help understand the time investment, the average task might take a few hours to hand-optimize to a high level (including time of both the original author and people who reviewed), plus many tasks needed to be revisited as we discovered new or improved techniques. Multiplying by 400 tasks and it becomes quite an undertaking, even for a team of 5 splitting the work.</p>\n<p>The competition was extremely close among the top 3 teams, and there is no particular technique or strategy we possessed that gave a decisive advantage over 2nd or 3rd. Indeed, reviewing others' solutions post-competition, we missed some useful techniques that the other teams had. </p>\n<h1>Techniques</h1>\n<p>Because all the problems are grid-based, there were many recurring techniques in our solutions. In this section, we outline some of the highlights, focusing on techniques that were unique to this contest rather than standard Python golf knowledge. However, this is in no way a complete list: every problem was hand-optimized and so there are many unique tricks.</p>\n<h2>Short grid operations</h2>\n<p>Because these operations are quite short, they were essential building blocks of solutions.</p>\n<p><strong>Grid operations</strong></p>\n<ul>\n<li><code>zip(*g)</code> transposes the grid</li>\n<li><code>g[::-1]</code> vertically mirrors the grid</li>\n<li><code>zip(*g[::-1])</code> rotates 90 degrees</li>\n<li><code>sum(g,[])</code> flattens the 2-d grid to a 1-d list. Note that in many cases <code>sum(g,g)</code> could be used to save a byte.</li>\n<li><code>filter(any,g)</code> removes empty rows</li>\n</ul>\n<p><strong>Row operations</strong></p>\n<ul>\n<li><code>min</code>, <code>max</code>, <code>any</code>, <code>all</code>, and <code>sorted</code> built-in functions were used ubiquitously. For <code>min</code>, <code>max</code>, and <code>sorted</code>, the <code>key</code> argument was often utilized, e.g., to get the least/most common color.</li>\n<li><code>{}.fromkeys(r)</code> will get the unique colors, sorted in order of first appearance. Alternatively, a dict comprehension could be used: <code>{v:0for v in r}</code></li>\n<li><code>filter(int,r)</code> removes black cells</li>\n</ul>\n<h2>Recursion and loops</h2>\n<p>Another fundamental building block was using recursion or loops to repeat operations. Many solutions are of the form \"do operation X, rotate 90 degrees, repeat 3 more times\".</p>\n<p>The baseline recursion template is <code>p=lambda g,n=3:-n*g or F(p(g,n-1))</code>, taking advantage that <code>-n*g</code> is falsy for <code>n=0...3</code>. This is more efficient than writing out a loop such as <code>for n in range(4)</code>. In special cases, there are shorter methods or useful alternatives:</p>\n<ul>\n<li>If the recursion is just depth 2, then one can write <code>p=lambda g,h=0:F(h or p(g,g))</code>, saving 4 bytes in most cases. The variation <code>p=lambda g,*h:F(*h or p(*g))</code> saves another 2 bytes but removes the first row.</li>\n<li>Depth 2 recursion with transposition could sometimes be done as <code>p=lambda g:[*map(F:=lambda*r:...,*map(F,*g))]</code>, saving 1 more byte.</li>\n<li>If the grid is fixed height, then there's a \"slice recursion\" technique, where you double the grid with each iteration and use a slice to stop the recursion when the desired depth is reached: <code>p=lambda g:F(g[70:]or p(g*2))</code> will iterate 4 times on a 10x10 grid. This saves bytes because it eliminates extra arguments to <code>p</code>. For example, <a href=\"https://arcprize.org/play?task=4093f84a\" target=\"_blank\">task 093</a> uses <code>p=lambda g:g[57330:]or ...</code> to iterate 12 times over a 14x14 grid, because 57330 = 14 * (2^12 - 1).</li>\n<li>As an alternative to recursion, there's a trick to make short loops: <code>p=lambda g:[g:=F(g)for _ in g][3]</code>. This is useful when <code>g</code> needs to be used multiple times inside <code>F</code>.</li>\n<li><code>exec/eval</code> can be used with string multiplication to repeat code, e.g., <code>exec(\"F(x);\"*9)</code>. It's very situational whether it's useful, but it can lead to small byte saves in a number of cases. An example is <a href=\"https://arcprize.org/play?task=05269061\" target=\"_blank\">task 007</a>:</li>\n</ul>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F5bb82570bc9fa51ff63d349c678194dc%2Ftask007.png?generation=1762352161257321&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g:(*)\n</code></pre>\n<p>In this task, there is a pattern that repeats with length 3, and we must fill in the missing cells. To do so, we take the maximum element of the flattened grid after slicing by steps of 3, and we mutate the grid so that we can always use the same slice, avoiding any need for loop variables. Because the grid is always 7x7, we can use string multiplication with <code>eval</code> to do each cell operation 7 times and each row operation 7 times.</p>\n<h2>List comprehensions</h2>\n<p>The most common structure of our solutions was to use a list comprehension to transform the grid. The most basic form of the code will look like this:</p>\n<pre><code>p= g:[[F(x) x  r] r  g]\n</code></pre>\n<p>This operates on single cells only, which is quite limiting. To do more complex operations that require looking beyond a single cell, state variables can be introduced with the walrus <code>:=</code> operator. For example, to include the cumulative sum of the row, one can write</p>\n<pre><code>p= g:[(s:=)[F(x,s:=s+x) x  r] r  g]\n</code></pre>\n<p>Initializing the state variable could sometimes be done more efficiently using <code>map</code> and <code>lambda</code>:</p>\n<pre><code>[(a:=)[F(x,a:=...) x  r)*r, (*g)]\n\n[*(*r,a=:[F(x,a:=...) x  r],*g)]\n</code></pre>\n<p>Another technique is mutating the list while operating on it. In particular, <code>list.pop()</code> was a source of multiple tricks. For example, it could facilitate \"lookahead\" in a list comprehension, such as testing whether a certain color appears later in the row:</p>\n<pre><code>\np= g:[[F(x, r[i:]) i,x  (r)] r  g]\n\np= g:[[F(r.pop(), r) _  r*] r  g] \n</code></pre>\n<p><code>list.pop()</code> can also implicitly reverse a list when popping from the back instead of front:</p>\n<pre><code>p= g:[[F(r.pop()) _  r*][::-] r  g] \n\np= g:[[F(r.pop()) _  r*] r  g] \n</code></pre>\n<h2>Regular expressions</h2>\n<p>Regular expressions, in particular <code>re.sub</code>, are extremely powerful for concisely editing strings. Therefore, it is often shorter to convert the given input matrix into a string, perform the substitution, and convert back to a matrix using <code>eval</code>. Our final submission used regex in a total of 46 solutions.</p>\n<p>A simple example of regex can be seen for <a href=\"https://arcprize.org/play?task=bb43febb\" target=\"_blank\">Task 294</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F8099f0e927e62d2227bae03158cf7da1%2Ftask294.png?generation=1762352542313904&amp;alt=media\" alt=\"\"></p>\n<pre><code> re\np= g:(re.sub(,,(g)))\n</code></pre>\n<p>The above substitution replaces all gray pixels with red if they are in the middle of a diagonal of three gray pixels. Since the grid size is fixed (10x10), we know that diagonally connected pixels are 34 characters apart.</p>\n<p>For more complex drawing tasks, a <em>rotate-and-replace</em> strategy was often used:</p>\n<pre><code>p= g:[g:=(re.sub(,,)) _  g][]\n</code></pre>\n<p>We employed different variations of this approach (list comprehension, recursion, or loops) depending on the task. \nAn interesting trick we found to save one byte is that f-strings allow unpacking, i.e., <code>f\"{*zip(*g[::-1]),}\"</code> vs <code>str([*zip(*g[::-1])])</code>.</p>\n<p>In some of the most complex tasks, we utilized multiple substitutions with different expressions. One way of doing so is:</p>\n<pre><code>p= g:[g:=(re.sub(*s,)) s [(,)]*+[(,)]*][-]\n</code></pre>\n<p>Because the result after <code>re.sub</code> is <code>eval</code>ed, the replacement could be any Python expression. This was a source for some interesting tricks. For example, in <a href=\"https://arcprize.org/play?task=6855a6e4\" target=\"_blank\">task 154</a>, we use the replacement <code>*[\\g&lt;0&gt;][::-1]</code> to reverse the matched substring.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F19858864a5ad3794a7f03f2c939d7d0c%2Ftask154.png?generation=1762352767054097&amp;alt=media\" alt=\"\"></p>\n<pre><code> re\np= g,k=:(re.sub(*+,,))\n</code></pre>\n<h2>\"Dimension\" recursion</h2>\n<p>A conceptually difficult but very powerful technique is to re-use the function <code>p</code> for different shapes of input: a 2d grid (<code>list[list[int]]</code>), 1d row (<code>list[int]</code>), or 0d cell (<code>int</code>). This can lead to significant savings in cases where the 2d operation and 1d operation can use overlapping code. There are several variations, but one template is </p>\n<pre><code>p= g:g*!=[p(r) r  g] F(g)\n</code></pre>\n<p>where <code>F</code> is the <code>int -&gt; int</code> operation. The condition <code>g*0!=0</code> checks if the input is a list, and separates the 2d/1d cases from the <code>int</code> case. For example, <a href=\"https://arcprize.org/play?task=1190e5a7\" target=\"_blank\">task 021</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Ff57a0b5f3eb82838b73861450f4138c9%2Ftask021.png?generation=1762353096588159&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g:g*-*-[p(g:=r) r  g  g!=r][::]\n</code></pre>\n<p>In this task, the row and column operations can use the exact same code: skip identical consecutive elements and downsample by 2. Note that the formula <code>g*-1*-1</code> can be used here instead of <code>g*0!=0</code> because cells never have value 0 (black) in this task.</p>\n<p>This technique becomes extremely potent when utilizing additional arguments to <code>p</code>. For example, <a href=\"https://arcprize.org/play?task=2204b7a8\" target=\"_blank\">task 040</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F8e6181a6221b01f0d3b0702d29a6c088%2Ftask040.png?generation=1762081699904648&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g,h=[]:g*!=[*(p,g[:]*+g[:]*,h+g)] h%~h&amp;g\n</code></pre>\n<p>The high-level idea behind this solution is to color each cell according to the nearest corner of the grid. The trick is that recursively applying <code>g[:1]*5+g[9:]*5</code> will yield the colors of the nearest corners (taking advantage that the grid is always 10x10). At the end of the recursion, <code>h</code> will contain the current cell and <code>g</code> will contain the nearest corner. Note that <code>h%~h&amp;g</code> is a math trick to efficiently express <code>h and g</code>. </p>\n<h2>Guess-and-check</h2>\n<p>In some problems, it is difficult to search for an object, but easy to verify whether an object is correct. As such, it is more efficient to simply keep guessing the object until the correct one is found. One example is <a href=\"https://arcprize.org/play?task=53b68214\" target=\"_blank\">task 124</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F44b16e2152f1f2fa9f4f7233ea1f88eb%2Ftask124.png?generation=1762353293066996&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g,x=,y=:(G:=[(i//y*x*[]+g[i%y])[:] i  ()])*(G[:]&lt;g&lt;G) p(g,~-x%,y^)\n</code></pre>\n<p>The goal of this task is to extend a pattern of height 2–3 and a horizontal offset of 0–2. In this solution, we guess the height <code>y</code> and offset <code>x</code> and use <code>G[:4]&lt;g&lt;G</code> to check if it agrees with the input. If the condition is true, the function returns, otherwise it will proceed to the next guess <code>p(g,~-x%3,y^1)</code>.</p>\n<p>Built-ins such as <code>max</code> and <code>min</code> could also be used to choose the correct guess. For example, <a href=\"https://arcprize.org/play?task=8efcae92\" target=\"_blank\">task 216</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fb920b7a5e5abbf618da37fce208e1a18%2Ftask216.png?generation=1762360787264792&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g:((-(c:=(l:=[r[x%:x%] r  g[x%:x%]],g).count)(),c(),c(),l) x  (**))[]\n</code></pre>\n<p>It's difficult to enumerate <em>only</em> the blue rectangles; it's much easier to enumerate <em>every</em> rectangular region and check that it's a valid blue rectangle. To enumerate every rectangular region, we use the math trick that <code>x%17</code>, <code>x%21</code>, <code>x%19</code>, <code>x%22</code> go through every combination of values (by the Chinese Remainder Theorem). To pick out the answer, we use <code>max</code> to simultaneously find the most red cells (via <code>c(2)</code>) and verify the region is, in fact, a complete blue rectangle, i.e., has no black cells <code>-c(0)</code> and cannot be extended with more blue cells <code>c(1)</code>.</p>\n<h2>3x3 neighborhood iterator</h2>\n<p>A compact method to iterate over all 3x3 neighborhoods in the grid is</p>\n<pre><code>[F(i)*h, (,g,g[:],g[:])*i, (,h,h[:],h[:])]\n</code></pre>\n<p>Using <code>eval</code>/<code>exec</code>, the repetition in the two <code>for</code> clauses can be reduced, as seen in our solution to <a href=\"https://arcprize.org/play?task=ae4f1146\" target=\"_blank\">task 271</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fee5ca850dceadf7941a37f955cf36f00%2Ftask271.png?generation=1762354444600807&amp;alt=media\" alt=\"\"></p>\n<pre><code>()\n</code></pre>\n<h2>pysearch</h2>\n<p>Many problems required small lookup tables, e.g., mapping of one color to another. We used the utility <a href=\"https://github.com/lynn/pysearch\" target=\"_blank\">pysearch</a> extensively to find the shortest expressions that reproduced a desired input–output relationship.</p>\n<p>A more sophisticated usage of pysearch was to find formulas that could be iterated to produce the desired transformation. One example is <a href=\"https://arcprize.org/play?task=b60334d2\" target=\"_blank\">task 282</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fd1faf8da9bc7d16a476d5caa6630de23%2Ftask282.png?generation=1762354635428855&amp;alt=media\" alt=\"\"></p>\n<p>Using pysearch with custom code to simulate four rotate-and-transform operations, we found that the desired pattern could be produced with the formula <code>p//5|p^c</code>, where <code>p</code> is the previous color (when scanning left-to-right in each row) and <code>c</code> is the current color. With some clever usage of <code>eval</code> and slice recursion, our solution is:</p>\n<pre><code>p= g:g[:][(*)*r, (*p(g*))]\n</code></pre>\n<h2>Cheese and hardcoding</h2>\n<p>In code golf parlance, \"cheese\" refers to solutions that exploit weaknesses in the judge, rather than adhering to the intended solution (similar to the concept of overfitting in machine learning). In this competition, most tasks had hundreds of tests, so opportunities for cheese were limited. However, an example of severe cheese is <a href=\"https://arcprize.org/play?task=d9fac9be\" target=\"_blank\">task 346</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F001f5b8739ff3dafe0bd704d64aa05f8%2Ftask346.png?generation=1762354908751599&amp;alt=media\" alt=\"\"></p>\n<p>For human eyes, the intention is obviously to pick the color which has a point surrounded by the other color. But that usually is the least frequent color in the grid, which is much easier to express in Python. By optimizing the slice of the grid to look at, we can force the least frequent color to always match the solution:</p>\n<pre><code>p= g:[[(f:=(g[:]+g,[])[:],key=f.count)]]\n</code></pre>\n<p>In addition, we solved two problems by hard coding a lookup table: <a href=\"https://arcprize.org/play?task=239be575\" target=\"_blank\">task 048</a> and <a href=\"https://arcprize.org/play?task=ce602527\" target=\"_blank\">task 319</a>. Both of these tasks need only 1 bit to resolve, so hard coding a table of bits was reasonably efficient. (In fact, task 048 required less than 1 bit per test case, because the answer was more likely to be 8 than 0 by a factor of about 2:1, which has an entropy of 0.92 bits.) Our hard-coding technique is quite interesting by itself, using the hash of the grid combined with data stored in a byte string:</p>\n<pre><code>(o:=((*%g,)))//[o%]%\n</code></pre>\n<p>Extra bytes can be added to the hashed data <code>b\"%a\"%g</code> to optimize the efficiency of the look up table, which can be searched by brute force.</p>\n<h2>zlib compression</h2>\n<p>Using a combination of Python's <code>zlib</code> and <code>exec</code>, it is possible to run compressed code. This technique was useful for solutions longer than about 200 bytes. To get the best possible compression, we had two pieces of infrastructure: </p>\n<ol>\n<li>We used <code>zopfli</code> or <code>libdeflate</code> for an initial pass of compression. However, these tools do not account for the extra cost of embedding data into a Python script, i.e., certain bytes must be escaped. We wrote a re-encoder that used the same Huffman tree from <code>zopfli</code> or <code>libdeflate</code>, and then used dynamic programming to find the optimal encoding.</li>\n<li>We used ad-hoc scripts to try different combinations of variable names to find what compressed best.</li>\n</ol>\n<p>Golfing with compression completely changed the techniques for optimal code size. Compression would make code repetition almost free, so the goal with zlib golfing is to use the same constructs over and over, rather than trying to find the shortest construct for each step of the algorithm.</p>\n<h2>No shell</h2>\n<p>It is worth mentioning that although shell commands were allowed in the competition (e.g. <code>os.popen</code>), and likely would have been useful in certain tasks (e.g., perl was available and is an excellent golfing language), we did not use this technique as per mutual agreement among the teams in the top 3. The reason being that the legality of this technique was clarified somewhat late in the competition, and fully exploring this technique would add considerably more work to an already large workload.</p>\n<h1>Acknowledgements</h1>\n<p>We are grateful to <a href=\"https://www.kaggle.com/garrymoss\" target=\"_blank\">@garrymoss</a>'s <a href=\"https://www.kaggle.com/code/garrymoss/golfed-solutions-and-explanations-to-hard-problems\" target=\"_blank\">Golfed solutions and explanations to hard problems</a>, <a href=\"https://www.kaggle.com/kenkrige\" target=\"_blank\">@kenkrige</a>'s <a href=\"https://www.kaggle.com/code/kenkrige/chipping-skills-3-regex/notebook\" target=\"_blank\">Chipping skills 3: Regex\n</a>, and the commenters there for sharing solutions that improved on our best at the time. We would also like to thank everyone who contributed to the public scores spreadsheet, which was an invaluable source of score benchmarks.</p>",
  "comments": [
    {
      "id": 3310235,
      "author_name": "Michael Tkach",
      "author_url": "",
      "post_date": "2025-11-02T12:37:55.150000",
      "content": "<p>Fantastic write-up! And congratulations on the first place!</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 3309666,
      "author_name": "Mr. Bruce",
      "author_url": "",
      "post_date": "2025-11-01T06:15:08.450000",
      "content": "<p>I really enjoy reading about your all's way of doing the whole approach in different ways, and yeah, that's fun.</p>",
      "votes": -2,
      "replies": []
    }
  ],
  "comment_count": 2,
  "messages": [
    {
      "id": 3309606,
      "postDate": "2025-11-01T03:23:14.807Z",
      "content": "<p>First and foremost, we are very grateful to the organizers of the competition for such a unique opportunity to engage in competitive code golf with the broader Kaggle community. The last three months have been a thrilling experience for all of us, and finishing in first is a tremendous honor.</p>\n<h1>Overview</h1>\n<h2>Solutions</h2>\n<p>Our code repository is <a href=\"https://github.com/Seek64/NeurIPS-Code-Golf-2025\" target=\"_blank\">here</a>. The final contest submission was from <a href=\"https://github.com/Seek64/NeurIPS-Code-Golf-2025/tree/9a5d156eabdc35732688751c091bf57277f70c21\" target=\"_blank\">this commit</a>. To reproduce our entry, you can download the repo and simply run <code>python auto_zip.py</code>, which will create the <code>submission.zip</code> file.</p>\n<h2>Approach</h2>\n<p>Our team consisted of 5 experienced code golfers. Our approach to this competition was very manual, with all solutions authored by hand. The only important automation was zlib-based compression, which applied to 21 of the 400 tasks (more on that below).</p>\n<h2>Results</h2>\n<p>Our good results in the competition can be mainly attributed to expertise in golfing and a large investment of time. To help understand the time investment, the average task might take a few hours to hand-optimize to a high level (including time of both the original author and people who reviewed), plus many tasks needed to be revisited as we discovered new or improved techniques. Multiplying by 400 tasks and it becomes quite an undertaking, even for a team of 5 splitting the work.</p>\n<p>The competition was extremely close among the top 3 teams, and there is no particular technique or strategy we possessed that gave a decisive advantage over 2nd or 3rd. Indeed, reviewing others' solutions post-competition, we missed some useful techniques that the other teams had. </p>\n<h1>Techniques</h1>\n<p>Because all the problems are grid-based, there were many recurring techniques in our solutions. In this section, we outline some of the highlights, focusing on techniques that were unique to this contest rather than standard Python golf knowledge. However, this is in no way a complete list: every problem was hand-optimized and so there are many unique tricks.</p>\n<h2>Short grid operations</h2>\n<p>Because these operations are quite short, they were essential building blocks of solutions.</p>\n<p><strong>Grid operations</strong></p>\n<ul>\n<li><code>zip(*g)</code> transposes the grid</li>\n<li><code>g[::-1]</code> vertically mirrors the grid</li>\n<li><code>zip(*g[::-1])</code> rotates 90 degrees</li>\n<li><code>sum(g,[])</code> flattens the 2-d grid to a 1-d list. Note that in many cases <code>sum(g,g)</code> could be used to save a byte.</li>\n<li><code>filter(any,g)</code> removes empty rows</li>\n</ul>\n<p><strong>Row operations</strong></p>\n<ul>\n<li><code>min</code>, <code>max</code>, <code>any</code>, <code>all</code>, and <code>sorted</code> built-in functions were used ubiquitously. For <code>min</code>, <code>max</code>, and <code>sorted</code>, the <code>key</code> argument was often utilized, e.g., to get the least/most common color.</li>\n<li><code>{}.fromkeys(r)</code> will get the unique colors, sorted in order of first appearance. Alternatively, a dict comprehension could be used: <code>{v:0for v in r}</code></li>\n<li><code>filter(int,r)</code> removes black cells</li>\n</ul>\n<h2>Recursion and loops</h2>\n<p>Another fundamental building block was using recursion or loops to repeat operations. Many solutions are of the form \"do operation X, rotate 90 degrees, repeat 3 more times\".</p>\n<p>The baseline recursion template is <code>p=lambda g,n=3:-n*g or F(p(g,n-1))</code>, taking advantage that <code>-n*g</code> is falsy for <code>n=0...3</code>. This is more efficient than writing out a loop such as <code>for n in range(4)</code>. In special cases, there are shorter methods or useful alternatives:</p>\n<ul>\n<li>If the recursion is just depth 2, then one can write <code>p=lambda g,h=0:F(h or p(g,g))</code>, saving 4 bytes in most cases. The variation <code>p=lambda g,*h:F(*h or p(*g))</code> saves another 2 bytes but removes the first row.</li>\n<li>Depth 2 recursion with transposition could sometimes be done as <code>p=lambda g:[*map(F:=lambda*r:...,*map(F,*g))]</code>, saving 1 more byte.</li>\n<li>If the grid is fixed height, then there's a \"slice recursion\" technique, where you double the grid with each iteration and use a slice to stop the recursion when the desired depth is reached: <code>p=lambda g:F(g[70:]or p(g*2))</code> will iterate 4 times on a 10x10 grid. This saves bytes because it eliminates extra arguments to <code>p</code>. For example, <a href=\"https://arcprize.org/play?task=4093f84a\" target=\"_blank\">task 093</a> uses <code>p=lambda g:g[57330:]or ...</code> to iterate 12 times over a 14x14 grid, because 57330 = 14 * (2^12 - 1).</li>\n<li>As an alternative to recursion, there's a trick to make short loops: <code>p=lambda g:[g:=F(g)for _ in g][3]</code>. This is useful when <code>g</code> needs to be used multiple times inside <code>F</code>.</li>\n<li><code>exec/eval</code> can be used with string multiplication to repeat code, e.g., <code>exec(\"F(x);\"*9)</code>. It's very situational whether it's useful, but it can lead to small byte saves in a number of cases. An example is <a href=\"https://arcprize.org/play?task=05269061\" target=\"_blank\">task 007</a>:</li>\n</ul>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F5bb82570bc9fa51ff63d349c678194dc%2Ftask007.png?generation=1762352161257321&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g:(*)\n</code></pre>\n<p>In this task, there is a pattern that repeats with length 3, and we must fill in the missing cells. To do so, we take the maximum element of the flattened grid after slicing by steps of 3, and we mutate the grid so that we can always use the same slice, avoiding any need for loop variables. Because the grid is always 7x7, we can use string multiplication with <code>eval</code> to do each cell operation 7 times and each row operation 7 times.</p>\n<h2>List comprehensions</h2>\n<p>The most common structure of our solutions was to use a list comprehension to transform the grid. The most basic form of the code will look like this:</p>\n<pre><code>p= g:[[F(x) x  r] r  g]\n</code></pre>\n<p>This operates on single cells only, which is quite limiting. To do more complex operations that require looking beyond a single cell, state variables can be introduced with the walrus <code>:=</code> operator. For example, to include the cumulative sum of the row, one can write</p>\n<pre><code>p= g:[(s:=)[F(x,s:=s+x) x  r] r  g]\n</code></pre>\n<p>Initializing the state variable could sometimes be done more efficiently using <code>map</code> and <code>lambda</code>:</p>\n<pre><code>[(a:=)[F(x,a:=...) x  r)*r, (*g)]\n\n[*(*r,a=:[F(x,a:=...) x  r],*g)]\n</code></pre>\n<p>Another technique is mutating the list while operating on it. In particular, <code>list.pop()</code> was a source of multiple tricks. For example, it could facilitate \"lookahead\" in a list comprehension, such as testing whether a certain color appears later in the row:</p>\n<pre><code>\np= g:[[F(x, r[i:]) i,x  (r)] r  g]\n\np= g:[[F(r.pop(), r) _  r*] r  g] \n</code></pre>\n<p><code>list.pop()</code> can also implicitly reverse a list when popping from the back instead of front:</p>\n<pre><code>p= g:[[F(r.pop()) _  r*][::-] r  g] \n\np= g:[[F(r.pop()) _  r*] r  g] \n</code></pre>\n<h2>Regular expressions</h2>\n<p>Regular expressions, in particular <code>re.sub</code>, are extremely powerful for concisely editing strings. Therefore, it is often shorter to convert the given input matrix into a string, perform the substitution, and convert back to a matrix using <code>eval</code>. Our final submission used regex in a total of 46 solutions.</p>\n<p>A simple example of regex can be seen for <a href=\"https://arcprize.org/play?task=bb43febb\" target=\"_blank\">Task 294</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F8099f0e927e62d2227bae03158cf7da1%2Ftask294.png?generation=1762352542313904&amp;alt=media\" alt=\"\"></p>\n<pre><code> re\np= g:(re.sub(,,(g)))\n</code></pre>\n<p>The above substitution replaces all gray pixels with red if they are in the middle of a diagonal of three gray pixels. Since the grid size is fixed (10x10), we know that diagonally connected pixels are 34 characters apart.</p>\n<p>For more complex drawing tasks, a <em>rotate-and-replace</em> strategy was often used:</p>\n<pre><code>p= g:[g:=(re.sub(,,)) _  g][]\n</code></pre>\n<p>We employed different variations of this approach (list comprehension, recursion, or loops) depending on the task. \nAn interesting trick we found to save one byte is that f-strings allow unpacking, i.e., <code>f\"{*zip(*g[::-1]),}\"</code> vs <code>str([*zip(*g[::-1])])</code>.</p>\n<p>In some of the most complex tasks, we utilized multiple substitutions with different expressions. One way of doing so is:</p>\n<pre><code>p= g:[g:=(re.sub(*s,)) s [(,)]*+[(,)]*][-]\n</code></pre>\n<p>Because the result after <code>re.sub</code> is <code>eval</code>ed, the replacement could be any Python expression. This was a source for some interesting tricks. For example, in <a href=\"https://arcprize.org/play?task=6855a6e4\" target=\"_blank\">task 154</a>, we use the replacement <code>*[\\g&lt;0&gt;][::-1]</code> to reverse the matched substring.</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F19858864a5ad3794a7f03f2c939d7d0c%2Ftask154.png?generation=1762352767054097&amp;alt=media\" alt=\"\"></p>\n<pre><code> re\np= g,k=:(re.sub(*+,,))\n</code></pre>\n<h2>\"Dimension\" recursion</h2>\n<p>A conceptually difficult but very powerful technique is to re-use the function <code>p</code> for different shapes of input: a 2d grid (<code>list[list[int]]</code>), 1d row (<code>list[int]</code>), or 0d cell (<code>int</code>). This can lead to significant savings in cases where the 2d operation and 1d operation can use overlapping code. There are several variations, but one template is </p>\n<pre><code>p= g:g*!=[p(r) r  g] F(g)\n</code></pre>\n<p>where <code>F</code> is the <code>int -&gt; int</code> operation. The condition <code>g*0!=0</code> checks if the input is a list, and separates the 2d/1d cases from the <code>int</code> case. For example, <a href=\"https://arcprize.org/play?task=1190e5a7\" target=\"_blank\">task 021</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Ff57a0b5f3eb82838b73861450f4138c9%2Ftask021.png?generation=1762353096588159&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g:g*-*-[p(g:=r) r  g  g!=r][::]\n</code></pre>\n<p>In this task, the row and column operations can use the exact same code: skip identical consecutive elements and downsample by 2. Note that the formula <code>g*-1*-1</code> can be used here instead of <code>g*0!=0</code> because cells never have value 0 (black) in this task.</p>\n<p>This technique becomes extremely potent when utilizing additional arguments to <code>p</code>. For example, <a href=\"https://arcprize.org/play?task=2204b7a8\" target=\"_blank\">task 040</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F8e6181a6221b01f0d3b0702d29a6c088%2Ftask040.png?generation=1762081699904648&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g,h=[]:g*!=[*(p,g[:]*+g[:]*,h+g)] h%~h&amp;g\n</code></pre>\n<p>The high-level idea behind this solution is to color each cell according to the nearest corner of the grid. The trick is that recursively applying <code>g[:1]*5+g[9:]*5</code> will yield the colors of the nearest corners (taking advantage that the grid is always 10x10). At the end of the recursion, <code>h</code> will contain the current cell and <code>g</code> will contain the nearest corner. Note that <code>h%~h&amp;g</code> is a math trick to efficiently express <code>h and g</code>. </p>\n<h2>Guess-and-check</h2>\n<p>In some problems, it is difficult to search for an object, but easy to verify whether an object is correct. As such, it is more efficient to simply keep guessing the object until the correct one is found. One example is <a href=\"https://arcprize.org/play?task=53b68214\" target=\"_blank\">task 124</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F44b16e2152f1f2fa9f4f7233ea1f88eb%2Ftask124.png?generation=1762353293066996&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g,x=,y=:(G:=[(i//y*x*[]+g[i%y])[:] i  ()])*(G[:]&lt;g&lt;G) p(g,~-x%,y^)\n</code></pre>\n<p>The goal of this task is to extend a pattern of height 2–3 and a horizontal offset of 0–2. In this solution, we guess the height <code>y</code> and offset <code>x</code> and use <code>G[:4]&lt;g&lt;G</code> to check if it agrees with the input. If the condition is true, the function returns, otherwise it will proceed to the next guess <code>p(g,~-x%3,y^1)</code>.</p>\n<p>Built-ins such as <code>max</code> and <code>min</code> could also be used to choose the correct guess. For example, <a href=\"https://arcprize.org/play?task=8efcae92\" target=\"_blank\">task 216</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fb920b7a5e5abbf618da37fce208e1a18%2Ftask216.png?generation=1762360787264792&amp;alt=media\" alt=\"\"></p>\n<pre><code>p= g:((-(c:=(l:=[r[x%:x%] r  g[x%:x%]],g).count)(),c(),c(),l) x  (**))[]\n</code></pre>\n<p>It's difficult to enumerate <em>only</em> the blue rectangles; it's much easier to enumerate <em>every</em> rectangular region and check that it's a valid blue rectangle. To enumerate every rectangular region, we use the math trick that <code>x%17</code>, <code>x%21</code>, <code>x%19</code>, <code>x%22</code> go through every combination of values (by the Chinese Remainder Theorem). To pick out the answer, we use <code>max</code> to simultaneously find the most red cells (via <code>c(2)</code>) and verify the region is, in fact, a complete blue rectangle, i.e., has no black cells <code>-c(0)</code> and cannot be extended with more blue cells <code>c(1)</code>.</p>\n<h2>3x3 neighborhood iterator</h2>\n<p>A compact method to iterate over all 3x3 neighborhoods in the grid is</p>\n<pre><code>[F(i)*h, (,g,g[:],g[:])*i, (,h,h[:],h[:])]\n</code></pre>\n<p>Using <code>eval</code>/<code>exec</code>, the repetition in the two <code>for</code> clauses can be reduced, as seen in our solution to <a href=\"https://arcprize.org/play?task=ae4f1146\" target=\"_blank\">task 271</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fee5ca850dceadf7941a37f955cf36f00%2Ftask271.png?generation=1762354444600807&amp;alt=media\" alt=\"\"></p>\n<pre><code>()\n</code></pre>\n<h2>pysearch</h2>\n<p>Many problems required small lookup tables, e.g., mapping of one color to another. We used the utility <a href=\"https://github.com/lynn/pysearch\" target=\"_blank\">pysearch</a> extensively to find the shortest expressions that reproduced a desired input–output relationship.</p>\n<p>A more sophisticated usage of pysearch was to find formulas that could be iterated to produce the desired transformation. One example is <a href=\"https://arcprize.org/play?task=b60334d2\" target=\"_blank\">task 282</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fd1faf8da9bc7d16a476d5caa6630de23%2Ftask282.png?generation=1762354635428855&amp;alt=media\" alt=\"\"></p>\n<p>Using pysearch with custom code to simulate four rotate-and-transform operations, we found that the desired pattern could be produced with the formula <code>p//5|p^c</code>, where <code>p</code> is the previous color (when scanning left-to-right in each row) and <code>c</code> is the current color. With some clever usage of <code>eval</code> and slice recursion, our solution is:</p>\n<pre><code>p= g:g[:][(*)*r, (*p(g*))]\n</code></pre>\n<h2>Cheese and hardcoding</h2>\n<p>In code golf parlance, \"cheese\" refers to solutions that exploit weaknesses in the judge, rather than adhering to the intended solution (similar to the concept of overfitting in machine learning). In this competition, most tasks had hundreds of tests, so opportunities for cheese were limited. However, an example of severe cheese is <a href=\"https://arcprize.org/play?task=d9fac9be\" target=\"_blank\">task 346</a>:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F001f5b8739ff3dafe0bd704d64aa05f8%2Ftask346.png?generation=1762354908751599&amp;alt=media\" alt=\"\"></p>\n<p>For human eyes, the intention is obviously to pick the color which has a point surrounded by the other color. But that usually is the least frequent color in the grid, which is much easier to express in Python. By optimizing the slice of the grid to look at, we can force the least frequent color to always match the solution:</p>\n<pre><code>p= g:[[(f:=(g[:]+g,[])[:],key=f.count)]]\n</code></pre>\n<p>In addition, we solved two problems by hard coding a lookup table: <a href=\"https://arcprize.org/play?task=239be575\" target=\"_blank\">task 048</a> and <a href=\"https://arcprize.org/play?task=ce602527\" target=\"_blank\">task 319</a>. Both of these tasks need only 1 bit to resolve, so hard coding a table of bits was reasonably efficient. (In fact, task 048 required less than 1 bit per test case, because the answer was more likely to be 8 than 0 by a factor of about 2:1, which has an entropy of 0.92 bits.) Our hard-coding technique is quite interesting by itself, using the hash of the grid combined with data stored in a byte string:</p>\n<pre><code>(o:=((*%g,)))//[o%]%\n</code></pre>\n<p>Extra bytes can be added to the hashed data <code>b\"%a\"%g</code> to optimize the efficiency of the look up table, which can be searched by brute force.</p>\n<h2>zlib compression</h2>\n<p>Using a combination of Python's <code>zlib</code> and <code>exec</code>, it is possible to run compressed code. This technique was useful for solutions longer than about 200 bytes. To get the best possible compression, we had two pieces of infrastructure: </p>\n<ol>\n<li>We used <code>zopfli</code> or <code>libdeflate</code> for an initial pass of compression. However, these tools do not account for the extra cost of embedding data into a Python script, i.e., certain bytes must be escaped. We wrote a re-encoder that used the same Huffman tree from <code>zopfli</code> or <code>libdeflate</code>, and then used dynamic programming to find the optimal encoding.</li>\n<li>We used ad-hoc scripts to try different combinations of variable names to find what compressed best.</li>\n</ol>\n<p>Golfing with compression completely changed the techniques for optimal code size. Compression would make code repetition almost free, so the goal with zlib golfing is to use the same constructs over and over, rather than trying to find the shortest construct for each step of the algorithm.</p>\n<h2>No shell</h2>\n<p>It is worth mentioning that although shell commands were allowed in the competition (e.g. <code>os.popen</code>), and likely would have been useful in certain tasks (e.g., perl was available and is an excellent golfing language), we did not use this technique as per mutual agreement among the teams in the top 3. The reason being that the legality of this technique was clarified somewhat late in the competition, and fully exploring this technique would add considerably more work to an already large workload.</p>\n<h1>Acknowledgements</h1>\n<p>We are grateful to <a href=\"https://www.kaggle.com/garrymoss\" target=\"_blank\">@garrymoss</a>'s <a href=\"https://www.kaggle.com/code/garrymoss/golfed-solutions-and-explanations-to-hard-problems\" target=\"_blank\">Golfed solutions and explanations to hard problems</a>, <a href=\"https://www.kaggle.com/kenkrige\" target=\"_blank\">@kenkrige</a>'s <a href=\"https://www.kaggle.com/code/kenkrige/chipping-skills-3-regex/notebook\" target=\"_blank\">Chipping skills 3: Regex\n</a>, and the commenters there for sharing solutions that improved on our best at the time. We would also like to thank everyone who contributed to the public scores spreadsheet, which was an invaluable source of score benchmarks.</p>",
      "rawMarkdown": "First and foremost, we are very grateful to the organizers of the competition for such a unique opportunity to engage in competitive code golf with the broader Kaggle community. The last three months have been a thrilling experience for all of us, and finishing in first is a tremendous honor.\n\n# Overview\n\n## Solutions\n\nOur code repository is [here](https://github.com/Seek64/NeurIPS-Code-Golf-2025). The final contest submission was from [this commit](https://github.com/Seek64/NeurIPS-Code-Golf-2025/tree/9a5d156eabdc35732688751c091bf57277f70c21). To reproduce our entry, you can download the repo and simply run `python auto_zip.py`, which will create the `submission.zip` file.\n\n## Approach\n\nOur team consisted of 5 experienced code golfers. Our approach to this competition was very manual, with all solutions authored by hand. The only important automation was zlib-based compression, which applied to 21 of the 400 tasks (more on that below).\n\n## Results\n\nOur good results in the competition can be mainly attributed to expertise in golfing and a large investment of time. To help understand the time investment, the average task might take a few hours to hand-optimize to a high level (including time of both the original author and people who reviewed), plus many tasks needed to be revisited as we discovered new or improved techniques. Multiplying by 400 tasks and it becomes quite an undertaking, even for a team of 5 splitting the work.\n\nThe competition was extremely close among the top 3 teams, and there is no particular technique or strategy we possessed that gave a decisive advantage over 2nd or 3rd. Indeed, reviewing others' solutions post-competition, we missed some useful techniques that the other teams had. \n\n# Techniques\n\nBecause all the problems are grid-based, there were many recurring techniques in our solutions. In this section, we outline some of the highlights, focusing on techniques that were unique to this contest rather than standard Python golf knowledge. However, this is in no way a complete list: every problem was hand-optimized and so there are many unique tricks.\n\n## Short grid operations\n\nBecause these operations are quite short, they were essential building blocks of solutions.\n\n__Grid operations__\n* `zip(*g)` transposes the grid\n* `g[::-1]` vertically mirrors the grid\n* `zip(*g[::-1])` rotates 90 degrees\n* `sum(g,[])` flattens the 2-d grid to a 1-d list. Note that in many cases `sum(g,g)` could be used to save a byte.\n* `filter(any,g)` removes empty rows\n\n__Row operations__\n* `min`, `max`, `any`, `all`, and `sorted` built-in functions were used ubiquitously. For `min`, `max`, and `sorted`, the `key` argument was often utilized, e.g., to get the least/most common color.\n* `{}.fromkeys(r)` will get the unique colors, sorted in order of first appearance. Alternatively, a dict comprehension could be used: `{v:0for v in r}`\n* `filter(int,r)` removes black cells\n\n## Recursion and loops\n\nAnother fundamental building block was using recursion or loops to repeat operations. Many solutions are of the form \"do operation X, rotate 90 degrees, repeat 3 more times\".\n\nThe baseline recursion template is `p=lambda g,n=3:-n*g or F(p(g,n-1))`, taking advantage that `-n*g` is falsy for `n=0...3`. This is more efficient than writing out a loop such as `for n in range(4)`. In special cases, there are shorter methods or useful alternatives:\n\n* If the recursion is just depth 2, then one can write `p=lambda g,h=0:F(h or p(g,g))`, saving 4 bytes in most cases. The variation `p=lambda g,*h:F(*h or p(*g))` saves another 2 bytes but removes the first row.\n* Depth 2 recursion with transposition could sometimes be done as `p=lambda g:[*map(F:=lambda*r:...,*map(F,*g))]`, saving 1 more byte.\n* If the grid is fixed height, then there's a \"slice recursion\" technique, where you double the grid with each iteration and use a slice to stop the recursion when the desired depth is reached: `p=lambda g:F(g[70:]or p(g*2))` will iterate 4 times on a 10x10 grid. This saves bytes because it eliminates extra arguments to `p`. For example, [task 093](https://arcprize.org/play?task=4093f84a) uses `p=lambda g:g[57330:]or ...` to iterate 12 times over a 14x14 grid, because 57330 = 14 * (2^12 - 1).\n* As an alternative to recursion, there's a trick to make short loops: `p=lambda g:[g:=F(g)for _ in g][3]`. This is useful when `g` needs to be used multiple times inside `F`.\n* `exec/eval` can be used with string multiplication to repeat code, e.g., `exec(\"F(x);\"*9)`. It's very situational whether it's useful, but it can lead to small byte saves in a number of cases. An example is [task 007](https://arcprize.org/play?task=05269061):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F5bb82570bc9fa51ff63d349c678194dc%2Ftask007.png?generation=1762352161257321&alt=media)\n\n```python\np=lambda g:eval(f'[{\"max(sum(g:=g[1:3]+g,[])[2::3]),\"*7}],'*7)\n```\nIn this task, there is a pattern that repeats with length 3, and we must fill in the missing cells. To do so, we take the maximum element of the flattened grid after slicing by steps of 3, and we mutate the grid so that we can always use the same slice, avoiding any need for loop variables. Because the grid is always 7x7, we can use string multiplication with `eval` to do each cell operation 7 times and each row operation 7 times.\n\n## List comprehensions\n\nThe most common structure of our solutions was to use a list comprehension to transform the grid. The most basic form of the code will look like this:\n```python\np=lambda g:[[F(x)for x in r]for r in g]\n```\nThis operates on single cells only, which is quite limiting. To do more complex operations that require looking beyond a single cell, state variables can be introduced with the walrus `:=` operator. For example, to include the cumulative sum of the row, one can write\n```python\np=lambda g:[(s:=0)or[F(x,s:=s+x)for x in r]for r in g]\n```\nInitializing the state variable could sometimes be done more efficiently using `map` and `lambda`:\n```python\n[(a:=0)or[F(x,a:=...)for x in r)for*r,in zip(*g)]\n#shortens to\n[*map(lambda*r,a=0:[F(x,a:=...)for x in r],*g)]\n```\n\nAnother technique is mutating the list while operating on it. In particular, `list.pop()` was a source of multiple tricks. For example, it could facilitate \"lookahead\" in a list comprehension, such as testing whether a certain color appears later in the row:\n```python\n#Original version, check if there is a gray cell in the row to the right of the current cell\np=lambda g:[[F(x,5in r[i:])for i,x in enumerate(r)]for r in g]\n#Optimized with pop()\np=lambda g:[[F(r.pop(0),5in r)for _ in r*1]for r in g] \n```\n\n`list.pop()` can also implicitly reverse a list when popping from the back instead of front:\n```python\np=lambda g:[[F(r.pop(0))for _ in r*1][::-1]for r in g] \n#shortens to\np=lambda g:[[F(r.pop())for _ in r*1]for r in g] \n```\n\n## Regular expressions\n\nRegular expressions, in particular `re.sub`, are extremely powerful for concisely editing strings. Therefore, it is often shorter to convert the given input matrix into a string, perform the substitution, and convert back to a matrix using `eval`. Our final submission used regex in a total of 46 solutions.\n\nA simple example of regex can be seen for [Task 294](https://arcprize.org/play?task=bb43febb):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F8099f0e927e62d2227bae03158cf7da1%2Ftask294.png?generation=1762352542313904&alt=media)\n\n```python\nimport re\np=lambda g:eval(re.sub(\"(?<=5.{34})5(?=.{34}5)\",\"2\",str(g)))\n```\n\nThe above substitution replaces all gray pixels with red if they are in the middle of a diagonal of three gray pixels. Since the grid size is fixed (10x10), we know that diagonally connected pixels are 34 characters apart.\n\nFor more complex drawing tasks, a *rotate-and-replace* strategy was often used:\n\n```python\np=lambda g:[g:=eval(re.sub(\"...\",\"...\",f\"{*zip(*g[::-1]),}\"))for _ in g][3]\n```\nWe employed different variations of this approach (list comprehension, recursion, or loops) depending on the task. \nAn interesting trick we found to save one byte is that f-strings allow unpacking, i.e., `f\"{*zip(*g[::-1]),}\"` vs `str([*zip(*g[::-1])])`.\n\nIn some of the most complex tasks, we utilized multiple substitutions with different expressions. One way of doing so is:\n\n```python\np=lambda g:[g:=eval(re.sub(*s,f\"{*zip(*g[::-1]),}\"))for s in[(\"...\",\"...\")]*16+[(\"...\",\"...\")]*4][-1]\n```\n\nBecause the result after `re.sub` is `eval`ed, the replacement could be any Python expression. This was a source for some interesting tricks. For example, in [task 154](https://arcprize.org/play?task=6855a6e4), we use the replacement `*[\\g<0>][::-1]` to reverse the matched substring.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F19858864a5ad3794a7f03f2c939d7d0c%2Ftask154.png?generation=1762352767054097&alt=media)\n\n```python\nimport re\np=lambda g,k=0:eval(re.sub(\"[^(2]{9}2\"*2+\"?\",\"*[\\g<0>][::-1]\",f\"{*zip(*k or p(g,g)),}\"))\n```\n\n## \"Dimension\" recursion\n\nA conceptually difficult but very powerful technique is to re-use the function `p` for different shapes of input: a 2d grid (`list[list[int]]`), 1d row (`list[int]`), or 0d cell (`int`). This can lead to significant savings in cases where the 2d operation and 1d operation can use overlapping code. There are several variations, but one template is \n```python\np=lambda g:g*0!=0and[p(r)for r in g]or F(g)\n```\nwhere `F` is the `int -> int` operation. The condition `g*0!=0` checks if the input is a list, and separates the 2d/1d cases from the `int` case. For example, [task 021](https://arcprize.org/play?task=1190e5a7):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Ff57a0b5f3eb82838b73861450f4138c9%2Ftask021.png?generation=1762353096588159&alt=media)\n\n```python\np=lambda g:g*-1*-1or[p(g:=r)for r in g if g!=r][::2]\n```\nIn this task, the row and column operations can use the exact same code: skip identical consecutive elements and downsample by 2. Note that the formula `g*-1*-1` can be used here instead of `g*0!=0` because cells never have value 0 (black) in this task.\n\nThis technique becomes extremely potent when utilizing additional arguments to `p`. For example, [task 040](https://arcprize.org/play?task=2204b7a8):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F8e6181a6221b01f0d3b0702d29a6c088%2Ftask040.png?generation=1762081699904648&alt=media)\n\n```python\np=lambda g,h=[]:g*0!=0and[*map(p,g[:1]*5+g[9:]*5,h+g)]or h%~h&g\n```\nThe high-level idea behind this solution is to color each cell according to the nearest corner of the grid. The trick is that recursively applying `g[:1]*5+g[9:]*5` will yield the colors of the nearest corners (taking advantage that the grid is always 10x10). At the end of the recursion, `h` will contain the current cell and `g` will contain the nearest corner. Note that `h%~h&g` is a math trick to efficiently express `h and g`. \n\n## Guess-and-check\n\nIn some problems, it is difficult to search for an object, but easy to verify whether an object is correct. As such, it is more efficient to simply keep guessing the object until the correct one is found. One example is [task 124](https://arcprize.org/play?task=53b68214):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F44b16e2152f1f2fa9f4f7233ea1f88eb%2Ftask124.png?generation=1762353293066996&alt=media)\n\n```python\np=lambda g,x=0,y=2:(G:=[(i//y*x*[0]+g[i%y])[:10]for i in range(10)])*(G[:4]<g<G)or p(g,~-x%3,y^1)\n```\n\nThe goal of this task is to extend a pattern of height 2–3 and a horizontal offset of 0–2. In this solution, we guess the height `y` and offset `x` and use `G[:4]<g<G` to check if it agrees with the input. If the condition is true, the function returns, otherwise it will proceed to the next guess `p(g,~-x%3,y^1)`.\n\nBuilt-ins such as `max` and `min` could also be used to choose the correct guess. For example, [task 216](https://arcprize.org/play?task=8efcae92):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fb920b7a5e5abbf618da37fce208e1a18%2Ftask216.png?generation=1762360787264792&alt=media)\n\n```python\np=lambda g:max((-(c:=sum(l:=[r[x%17:x%21]for r in g[x%19:x%22]],g).count)(0),c(2),c(1),l)for x in range(8**6))[3]\n```\n\nIt's difficult to enumerate *only* the blue rectangles; it's much easier to enumerate *every* rectangular region and check that it's a valid blue rectangle. To enumerate every rectangular region, we use the math trick that `x%17`, `x%21`, `x%19`, `x%22` go through every combination of values (by the Chinese Remainder Theorem). To pick out the answer, we use `max` to simultaneously find the most red cells (via `c(2)`) and verify the region is, in fact, a complete blue rectangle, i.e., has no black cells `-c(0)` and cannot be extended with more blue cells `c(1)`.\n\n## 3x3 neighborhood iterator\n\nA compact method to iterate over all 3x3 neighborhoods in the grid is\n```python\n[F(i)for*h,in map(zip,g,g[1:],g[2:])for*i,in map(zip,h,h[1:],h[2:])]\n```\nUsing `eval`/`exec`, the repetition in the two `for` clauses can be reduced, as seen in our solution to [task 271](https://arcprize.org/play?task=ae4f1146):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fee5ca850dceadf7941a37f955cf36f00%2Ftask271.png?generation=1762354444600807&alt=media)\n\n```python\nexec(f\"p=lambda g:max([str(g).count('1'),g]{'for*g,in map(zip,g,g[1:],g[2:])'*2})[1]\")\n```\n\n## pysearch\n\nMany problems required small lookup tables, e.g., mapping of one color to another. We used the utility [pysearch](https://github.com/lynn/pysearch) extensively to find the shortest expressions that reproduced a desired input–output relationship.\n\nA more sophisticated usage of pysearch was to find formulas that could be iterated to produce the desired transformation. One example is [task 282](https://arcprize.org/play?task=b60334d2):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2Fd1faf8da9bc7d16a476d5caa6630de23%2Ftask282.png?generation=1762354635428855&alt=media)\n\nUsing pysearch with custom code to simulate four rotate-and-transform operations, we found that the desired pattern could be produced with the formula `p//5|p^c`, where `p` is the previous color (when scanning left-to-right in each row) and `c` is the current color. With some clever usage of `eval` and slice recursion, our solution is:\n\n```python\np=lambda g:g[99:]or[eval(8*\"+(x:=r.pop()),x//5|x^0\")for*r,in zip(*p(g*2))]\n```\n\n## Cheese and hardcoding\n\nIn code golf parlance, \"cheese\" refers to solutions that exploit weaknesses in the judge, rather than adhering to the intended solution (similar to the concept of overfitting in machine learning). In this competition, most tasks had hundreds of tests, so opportunities for cheese were limited. However, an example of severe cheese is [task 346](https://arcprize.org/play?task=d9fac9be):\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10081%2F001f5b8739ff3dafe0bd704d64aa05f8%2Ftask346.png?generation=1762354908751599&alt=media)\n\nFor human eyes, the intention is obviously to pick the color which has a point surrounded by the other color. But that usually is the least frequent color in the grid, which is much easier to express in Python. By optimizing the slice of the grid to look at, we can force the least frequent color to always match the solution:\n```python\np=lambda g:[[min(f:=sum(g[:6]+g,[])[11:],key=f.count)]]\n```\n\nIn addition, we solved two problems by hard coding a lookup table: [task 048](https://arcprize.org/play?task=239be575) and [task 319](https://arcprize.org/play?task=ce602527). Both of these tasks need only 1 bit to resolve, so hard coding a table of bits was reasonably efficient. (In fact, task 048 required less than 1 bit per test case, because the answer was more likely to be 8 than 0 by a factor of about 2:1, which has an entropy of 0.92 bits.) Our hard-coding technique is quite interesting by itself, using the hash of the grid combined with data stored in a byte string:\n```python\n(o:=hash((*b\"%a\"%g,)))//b'data_goes_here'[o%14]%2\n```\nExtra bytes can be added to the hashed data `b\"%a\"%g` to optimize the efficiency of the look up table, which can be searched by brute force.\n\n## zlib compression\n\nUsing a combination of Python's `zlib` and `exec`, it is possible to run compressed code. This technique was useful for solutions longer than about 200 bytes. To get the best possible compression, we had two pieces of infrastructure: \n1. We used `zopfli` or `libdeflate` for an initial pass of compression. However, these tools do not account for the extra cost of embedding data into a Python script, i.e., certain bytes must be escaped. We wrote a re-encoder that used the same Huffman tree from `zopfli` or `libdeflate`, and then used dynamic programming to find the optimal encoding.\n2. We used ad-hoc scripts to try different combinations of variable names to find what compressed best.\n\nGolfing with compression completely changed the techniques for optimal code size. Compression would make code repetition almost free, so the goal with zlib golfing is to use the same constructs over and over, rather than trying to find the shortest construct for each step of the algorithm.\n\n## No shell\n\nIt is worth mentioning that although shell commands were allowed in the competition (e.g. `os.popen`), and likely would have been useful in certain tasks (e.g., perl was available and is an excellent golfing language), we did not use this technique as per mutual agreement among the teams in the top 3. The reason being that the legality of this technique was clarified somewhat late in the competition, and fully exploring this technique would add considerably more work to an already large workload.\n\n# Acknowledgements\n\nWe are grateful to @garrymoss's [Golfed solutions and explanations to hard problems](https://www.kaggle.com/code/garrymoss/golfed-solutions-and-explanations-to-hard-problems), @kenkrige's [Chipping skills 3: Regex\n](https://www.kaggle.com/code/kenkrige/chipping-skills-3-regex/notebook), and the commenters there for sharing solutions that improved on our best at the time. We would also like to thank everyone who contributed to the public scores spreadsheet, which was an invaluable source of score benchmarks.",
      "votes": 27
    },
    {
      "id": 3310235,
      "postDate": "2025-11-02T12:37:55.150Z",
      "content": "<p>Fantastic write-up! And congratulations on the first place!</p>",
      "rawMarkdown": "Fantastic write-up! And congratulations on the first place!",
      "votes": 1
    },
    {
      "id": 3309666,
      "postDate": "2025-11-01T06:15:08.450Z",
      "content": "<p>I really enjoy reading about your all's way of doing the whole approach in different ways, and yeah, that's fun.</p>",
      "rawMarkdown": "I really enjoy reading about your all's way of doing the whole approach in different ways, and yeah, that's fun.",
      "votes": -2
    }
  ],
  "complete": true,
  "source": "live",
  "fetched_at": "2026-10-19T12:52:08Z",
  "competition_id": 95282,
  "competition_slug": "google-code-golf-2025",
  "team_id": 14196298,
  "solution_url": "/competitions/google-code-golf-2025/writeups/cgi"
}