{
  "id": 614727,
  "title": "15th Place Solution",
  "content": "<p>We would like to thank the host and the Kaggle team for organizing such a creative and inspiring competition.\nIt was our first experience with code golf, and we learned a lot through this exciting challenge.</p>\n<p>Team Member: <a href=\"https://www.kaggle.com/chimaki821\" target=\"_blank\">@chimaki821</a> <a href=\"https://www.kaggle.com/isakatsuyoshi\" target=\"_blank\">@isakatsuyoshi</a> <a href=\"https://www.kaggle.com/denden12\" target=\"_blank\">@denden12</a> <a href=\"https://www.kaggle.com/hikari30\" target=\"_blank\">@hikari30</a> <a href=\"https://www.kaggle.com/kosirowada\" target=\"_blank\">@kosirowada</a></p>\n<p>table of contents:</p>\n<ol>\n<li>Code Golf by Hand</li>\n<li>Code Golf by LLM</li>\n<li>Code Golf by Rulebase</li>\n<li>Code Golf by Compression</li>\n</ol>\n<hr>\n<h2>1. Code Golf by Hand</h2>\n<p>Since it was difficult for LLMs to solve tasks from scratch or fundamentally improve the algorithms, we manually created many “initial solutions”.\nDuring this process, the discussions on the forum were extremely helpful. In particular, the two discussions we posted:</p>\n<p>[1] <a href=\"https://www.kaggle.com/competitions/google-code-golf-2025/discussion/611930\" target=\"_blank\">How do you golf neighbor checks? (task279)</a></p>\n<p>[2] <a href=\"https://www.kaggle.com/competitions/google-code-golf-2025/discussion/612504\" target=\"_blank\">Diagonal Extension (task034)</a></p>\n<p>led to productive exchanges with many participants, and we were able to incorporate the insights gained from them. We sincerely thank everyone who contributed.</p>\n<h3>1.1. Algorithmic Improvements</h3>\n<p>We focused on simplifying algorithms by observing the test cases carefully and exploiting structural patterns in the grids.\nOur main strategies were as follows:</p>\n<h4>(a) Simplify by Observation</h4>\n<ul>\n<li>Identify fixed input/output sizes or recurring numbers.</li>\n<li>Detect common layouts, such as one object in the upper half and another in the lower half.</li>\n<li>Apply operations to entire rows or grids whenever possible.</li>\n</ul>\n<h4>(b) Write Concise and Efficient Logic</h4>\n<ul>\n<li>Use high-level functions like <code>any</code>, <code>all</code>, <code>zip</code>, <code>map</code>, <code>filter</code>, and <code>sum</code>.</li>\n<li>Replace nested loops and index access with more direct operations.</li>\n<li>Prefer <code>list.index</code> or <code>enumerate</code> over manual <code>range</code> loops.</li>\n</ul>\n<h4>(c) Smart Techniques</h4>\n<ul>\n<li>Employ <strong>recursion</strong> and grid <strong>rotation</strong> to reduce repeated neighbor checks.</li>\n<li>Leverage <strong>regex</strong> where possible — we solved 31 tasks with regex-based solutions.<ul>\n<li>see <a href=\"https://www.kaggle.com/code/chimaki821/step-by-step-regex-practice\" target=\"_blank\">chimaki’s notebook</a> </li></ul></li>\n</ul>\n<h3>1.2. Identifying Similar Problems</h3>\n<p>While solving tasks manually, we noticed that many problems shared similar structures. Recognizing these patterns allowed us to apply successful strategies from one task to another.\nThanks to <a href=\"https://www.kaggle.com/denden12\" target=\"_blank\">@denden12</a>, who compiled a PDF summarizing all the tasks, we were able to discover such similarities more easily.</p>\n<p>For example:</p>\n<ul>\n<li>task154 and task390</li>\n<li>task351 and task400</li>\n<li>task246 and task335</li>\n</ul>\n<p>Since these problems were almost identical, we tackled them together and aimed for score improvements simultaneously.</p>\n<hr>\n<h2>2. Code Golf by LLM</h2>\n<p>We racked up a lot of points by taking strong human‑written seed solutions and then “follow‑up code‑golfing” them with AIs like GPT5 Pro.</p>\n<p>We adopted this workflow for several reasons:</p>\n<ul>\n<li>We wanted to conserve human effort and let AI handle what AI can.</li>\n<li>Current high-end AI can relatively easily golf code when the approach direction is clear.<ul>\n<li>However, AI struggles with 0-&gt;1 creation or escaping local optima, where manual code golf remains effective.</li></ul></li>\n<li>Diversity is key to escaping local optima, such as moving from a manual solution to AI, or from one AI to another, is the key to escaping local optima.</li>\n</ul>\n<h3>2.1. LLMs We Used</h3>\n<ul>\n<li>We tried GPT, Claude, Gemini, and Grok.</li>\n<li>In the early phase, we used Claude Code with DSL implementation to achieve AC results.</li>\n<li>After GPT‑5 was released, we shifted toward it. GPT‑5’s performance was astonishing, achieving many 0→1 AC results.</li>\n<li>In the end, we mainly used GPT‑5 Codex and GPT‑5 Pro.</li>\n</ul>\n<h3>2.2. Prompt Engineering</h3>\n<p>We put significant effort into prompt design and system-level context construction.\nThe key idea was to provide good examples and rich context so that the LLM could learn how to golf effectively.</p>\n<ul>\n<li>Added numerous examples of codes that had already matched the first-place scores.<ul>\n<li>This enabled the models to perform advanced transformations such as recursion, which are typically difficult for LLMs.</li></ul></li>\n<li>Included before/after examples of effective golfing tips.</li>\n<li>Used <a href=\"https://platform.openai.com/chat/edit?models=gpt-5&amp;optimize=true\" target=\"_blank\">GPT-5 Prompt Optimizer</a>  to systematically improve output quality and consistency.</li>\n<li>Supplemented prompts with custom text files, which included:<ul>\n<li>Programming-contest–style test cases.</li>\n<li>Translated <a href=\"https://www.kaggle.com/code/boristown/agi-chinese-hints-for-all-400-tasks\" target=\"_blank\">hints originally written in Chinese (by 暗黑AGI-san)</a> , converted into Japanese and English via the <a href=\"https://plamo.preferredai.jp/\" target=\"_blank\">PLaMo API</a> .</li></ul></li>\n<li>Combined these into a structured “prompt file” for each problem, containing code examples, test data, and textual hints.\nThe image below shows an example of such a prompt file used for AI-driven code golf experiments.</li>\n</ul>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F13641953%2Fa68a58665b6760f08a4f10415d0e989e%2Fimage.png?generation=1762391172517683&amp;alt=media\" alt=\"\"></p>\n<ul>\n<li>Using the tricks learned from writing shorter code manually helped the prompt get better scores. With this smart idea, even gpt-5-mini got a better score about one time out of every 50 tries.</li>\n</ul>\n<p>These structured prompt files helped the models understand both the problem format and the essence of golfing strategies, resulting in consistently better code compression.</p>\n<h3>2.3. Custom Evaluation Script for AI Agents</h3>\n<p>We modified the official evaluation script to better support LLM-based experimentation:</p>\n<ul>\n<li>Displayed both plain byte count and zlib/zopfli-compressed size.</li>\n<li>Computed and displayed the gap from the first-place score.<ul>\n<li>For example, GPT-5 Codex often reduced a ~50-point gap to match the 1st-place score.</li></ul></li>\n<li>Implemented a hack filter to reject invalid or exploitative outputs, such as:</li>\n</ul>\n<pre><code>  p= g:(,(),{:*_:})()\n</code></pre>\n<p>(a 46-byte snippet that passes any test case locally but fails on the leaderboard).</p>\n<h3>2.4. API Automation</h3>\n<p>After collecting a sufficient number of strong initial solutions, we automated the process:</p>\n<p>For each task, we prompted the models with:</p>\n<ul>\n<li>A set of programming-contest–style test cases,</li>\n<li>The currently shortest known code, and</li>\n<li>30–50 randomly selected solutions from other problems.</li>\n</ul>\n<p>Using gpt-5, gpt-5-mini, and grok-4, we performed iterative improvements.\nOn average, gpt-5 produced a few-byte improvement once every 50 problems, and gpt-5-mini once every 100 problems.</p>\n<hr>\n<h2>3. Code Golf by Rulebase</h2>\n<p>Our team maintained two synchronized datasets throughout the competition:</p>\n<ol>\n<li>Plain Shortest Codes : the shortest uncompressed version.</li>\n<li>Best Score Codes : the version achieving the best score considering compression.</li>\n</ol>\n<p>After each update or improvement, both datasets were automatically refreshed through a post-processing pipeline.</p>\n<h3>3.1. Rule-based Post-processing Notebook</h3>\n<p>We developed 23 rule-based notebooks that automatically executed sequentially every minute.\nEach notebook applied a specific transformation to further shorten the code while maintaining correctness.\nThe following summarizes the main rules used:</p>\n<ol>\n<li>Remove comments</li>\n<li>Remove unnecessary whitespaces</li>\n<li>Remove soft (non-critical) whitespaces</li>\n<li>Expand <code>if True:</code> blocks and remove <code>if False:</code> blocks entirely</li>\n<li>Replace <code>True/False</code> with <code>1/0</code></li>\n<li>Replace <code>len(x) == 0</code> with <code>not x</code></li>\n<li>Replace <code>x == False</code> with <code>not x</code></li>\n<li>Replace <code>x == True</code> with <code>x</code></li>\n<li>Safely remove <code>pass</code> statements</li>\n<li>Shorten <code>return None</code> → <code>return</code></li>\n<li>Remove trailing semicolons <code>;</code></li>\n<li>Remove redundant parentheses</li>\n<li>Remove unused imports</li>\n<li>Remove unnecessary <code>list()</code> calls for literals</li>\n<li>Automatically shorten local variable, function, and argument names to one character</li>\n<li>Apply global one-character renaming via AST transformation</li>\n<li>Remove indentation and compress function bodies into single lines</li>\n<li>Replace <code>l.append(x)</code> with <code>l += [x]</code></li>\n<li>Shorten <code>from X import Y</code> → <code>from X import *</code></li>\n<li>Minify identifiers using external libraries (e.g., pyminifier-based tools)</li>\n<li>Reduce indentation width from 4 spaces to 1 space</li>\n<li>Inject short aliases for built-in functions (e.g., <code>range → R</code>)</li>\n<li>Remove all <code>print</code> statements</li>\n</ol>\n<p>This automated pipeline continuously refined submissions to approach minimal byte counts.\nBy keeping these transformations modular and incremental, we ensured that even minor edits propagated safely to the latest code versions.</p>\n<h3>3.2. AI-based Post-processing</h3>\n<p>Beyond the rule-based framework, we also automated AI-based post-processing notebooks using OpenAI API, Gemini API, and Grok API.\nThese notebooks are periodically executed to perform more flexible, non-rule-based optimizations and capture improvements that purely static rules could not.</p>\n<h3>3.3. pysearch</h3>\n<p>We applied targeted transformations using <a href=\"https://github.com/lynn/pysearch\" target=\"_blank\">pysearch</a>.\nBy specifying desired input/output pairs, pysearch synthesizes the shortest logic that satisfies them, which improved scores for some problems.</p>\n<p>Example:\nFor <code>task144.py</code>, we needed <code>3*(x==y)</code> with <code>x∈[0,7]</code> and <code>y∈[0,2]</code>.\npysearch rewrote it to <code>3&gt;&gt;x+y</code>, saving 2 bytes.</p>\n<hr>\n<h2>4. Code Golf by Compression</h2>\n<ul>\n<li><p><strong>Grammar optimization</strong>: Considering zlib/zopfli compression, we sometimes intentionally increased raw byte count to gain a better compressed score.\nFor example, aliasing <code>range</code> as <code>R=range</code> and using <code>R()</code> can reduce raw bytes, but not aliasing can compress better.\nWe first performed some of these conversions manually, then turned them into prompt examples and used GPT‑5 to revise code accordingly.</p></li>\n<li><p><strong>Variable-name optimization</strong>: Based on Garry Moss’s notebook\n<a href=\"https://www.kaggle.com/code/garrymoss/compressed-variable-name-optimization\" target=\"_blank\"><code>compressed-variable-name-optimization</code></a>,\nwe accelerated and tuned the search.\nWe deferred the expensive correctness checks, first computing compressed byte counts to speed up iterations, and reset the trial counter whenever the score improved.\nIn practice, we ran the compression-aware rewrites first and then applied variable-name optimization, which yielded better overall compression efficiency.</p></li>\n</ul>",
  "comments": [],
  "comment_count": 0,
  "messages": [
    {
      "id": 3311881,
      "postDate": "2025-11-06T01:11:59.770Z",
      "content": "<p>We would like to thank the host and the Kaggle team for organizing such a creative and inspiring competition.\nIt was our first experience with code golf, and we learned a lot through this exciting challenge.</p>\n<p>Team Member: <a href=\"https://www.kaggle.com/chimaki821\" target=\"_blank\">@chimaki821</a> <a href=\"https://www.kaggle.com/isakatsuyoshi\" target=\"_blank\">@isakatsuyoshi</a> <a href=\"https://www.kaggle.com/denden12\" target=\"_blank\">@denden12</a> <a href=\"https://www.kaggle.com/hikari30\" target=\"_blank\">@hikari30</a> <a href=\"https://www.kaggle.com/kosirowada\" target=\"_blank\">@kosirowada</a></p>\n<p>table of contents:</p>\n<ol>\n<li>Code Golf by Hand</li>\n<li>Code Golf by LLM</li>\n<li>Code Golf by Rulebase</li>\n<li>Code Golf by Compression</li>\n</ol>\n<hr>\n<h2>1. Code Golf by Hand</h2>\n<p>Since it was difficult for LLMs to solve tasks from scratch or fundamentally improve the algorithms, we manually created many “initial solutions”.\nDuring this process, the discussions on the forum were extremely helpful. In particular, the two discussions we posted:</p>\n<p>[1] <a href=\"https://www.kaggle.com/competitions/google-code-golf-2025/discussion/611930\" target=\"_blank\">How do you golf neighbor checks? (task279)</a></p>\n<p>[2] <a href=\"https://www.kaggle.com/competitions/google-code-golf-2025/discussion/612504\" target=\"_blank\">Diagonal Extension (task034)</a></p>\n<p>led to productive exchanges with many participants, and we were able to incorporate the insights gained from them. We sincerely thank everyone who contributed.</p>\n<h3>1.1. Algorithmic Improvements</h3>\n<p>We focused on simplifying algorithms by observing the test cases carefully and exploiting structural patterns in the grids.\nOur main strategies were as follows:</p>\n<h4>(a) Simplify by Observation</h4>\n<ul>\n<li>Identify fixed input/output sizes or recurring numbers.</li>\n<li>Detect common layouts, such as one object in the upper half and another in the lower half.</li>\n<li>Apply operations to entire rows or grids whenever possible.</li>\n</ul>\n<h4>(b) Write Concise and Efficient Logic</h4>\n<ul>\n<li>Use high-level functions like <code>any</code>, <code>all</code>, <code>zip</code>, <code>map</code>, <code>filter</code>, and <code>sum</code>.</li>\n<li>Replace nested loops and index access with more direct operations.</li>\n<li>Prefer <code>list.index</code> or <code>enumerate</code> over manual <code>range</code> loops.</li>\n</ul>\n<h4>(c) Smart Techniques</h4>\n<ul>\n<li>Employ <strong>recursion</strong> and grid <strong>rotation</strong> to reduce repeated neighbor checks.</li>\n<li>Leverage <strong>regex</strong> where possible — we solved 31 tasks with regex-based solutions.<ul>\n<li>see <a href=\"https://www.kaggle.com/code/chimaki821/step-by-step-regex-practice\" target=\"_blank\">chimaki’s notebook</a> </li></ul></li>\n</ul>\n<h3>1.2. Identifying Similar Problems</h3>\n<p>While solving tasks manually, we noticed that many problems shared similar structures. Recognizing these patterns allowed us to apply successful strategies from one task to another.\nThanks to <a href=\"https://www.kaggle.com/denden12\" target=\"_blank\">@denden12</a>, who compiled a PDF summarizing all the tasks, we were able to discover such similarities more easily.</p>\n<p>For example:</p>\n<ul>\n<li>task154 and task390</li>\n<li>task351 and task400</li>\n<li>task246 and task335</li>\n</ul>\n<p>Since these problems were almost identical, we tackled them together and aimed for score improvements simultaneously.</p>\n<hr>\n<h2>2. Code Golf by LLM</h2>\n<p>We racked up a lot of points by taking strong human‑written seed solutions and then “follow‑up code‑golfing” them with AIs like GPT5 Pro.</p>\n<p>We adopted this workflow for several reasons:</p>\n<ul>\n<li>We wanted to conserve human effort and let AI handle what AI can.</li>\n<li>Current high-end AI can relatively easily golf code when the approach direction is clear.<ul>\n<li>However, AI struggles with 0-&gt;1 creation or escaping local optima, where manual code golf remains effective.</li></ul></li>\n<li>Diversity is key to escaping local optima, such as moving from a manual solution to AI, or from one AI to another, is the key to escaping local optima.</li>\n</ul>\n<h3>2.1. LLMs We Used</h3>\n<ul>\n<li>We tried GPT, Claude, Gemini, and Grok.</li>\n<li>In the early phase, we used Claude Code with DSL implementation to achieve AC results.</li>\n<li>After GPT‑5 was released, we shifted toward it. GPT‑5’s performance was astonishing, achieving many 0→1 AC results.</li>\n<li>In the end, we mainly used GPT‑5 Codex and GPT‑5 Pro.</li>\n</ul>\n<h3>2.2. Prompt Engineering</h3>\n<p>We put significant effort into prompt design and system-level context construction.\nThe key idea was to provide good examples and rich context so that the LLM could learn how to golf effectively.</p>\n<ul>\n<li>Added numerous examples of codes that had already matched the first-place scores.<ul>\n<li>This enabled the models to perform advanced transformations such as recursion, which are typically difficult for LLMs.</li></ul></li>\n<li>Included before/after examples of effective golfing tips.</li>\n<li>Used <a href=\"https://platform.openai.com/chat/edit?models=gpt-5&amp;optimize=true\" target=\"_blank\">GPT-5 Prompt Optimizer</a>  to systematically improve output quality and consistency.</li>\n<li>Supplemented prompts with custom text files, which included:<ul>\n<li>Programming-contest–style test cases.</li>\n<li>Translated <a href=\"https://www.kaggle.com/code/boristown/agi-chinese-hints-for-all-400-tasks\" target=\"_blank\">hints originally written in Chinese (by 暗黑AGI-san)</a> , converted into Japanese and English via the <a href=\"https://plamo.preferredai.jp/\" target=\"_blank\">PLaMo API</a> .</li></ul></li>\n<li>Combined these into a structured “prompt file” for each problem, containing code examples, test data, and textual hints.\nThe image below shows an example of such a prompt file used for AI-driven code golf experiments.</li>\n</ul>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F13641953%2Fa68a58665b6760f08a4f10415d0e989e%2Fimage.png?generation=1762391172517683&amp;alt=media\" alt=\"\"></p>\n<ul>\n<li>Using the tricks learned from writing shorter code manually helped the prompt get better scores. With this smart idea, even gpt-5-mini got a better score about one time out of every 50 tries.</li>\n</ul>\n<p>These structured prompt files helped the models understand both the problem format and the essence of golfing strategies, resulting in consistently better code compression.</p>\n<h3>2.3. Custom Evaluation Script for AI Agents</h3>\n<p>We modified the official evaluation script to better support LLM-based experimentation:</p>\n<ul>\n<li>Displayed both plain byte count and zlib/zopfli-compressed size.</li>\n<li>Computed and displayed the gap from the first-place score.<ul>\n<li>For example, GPT-5 Codex often reduced a ~50-point gap to match the 1st-place score.</li></ul></li>\n<li>Implemented a hack filter to reject invalid or exploitative outputs, such as:</li>\n</ul>\n<pre><code>  p= g:(,(),{:*_:})()\n</code></pre>\n<p>(a 46-byte snippet that passes any test case locally but fails on the leaderboard).</p>\n<h3>2.4. API Automation</h3>\n<p>After collecting a sufficient number of strong initial solutions, we automated the process:</p>\n<p>For each task, we prompted the models with:</p>\n<ul>\n<li>A set of programming-contest–style test cases,</li>\n<li>The currently shortest known code, and</li>\n<li>30–50 randomly selected solutions from other problems.</li>\n</ul>\n<p>Using gpt-5, gpt-5-mini, and grok-4, we performed iterative improvements.\nOn average, gpt-5 produced a few-byte improvement once every 50 problems, and gpt-5-mini once every 100 problems.</p>\n<hr>\n<h2>3. Code Golf by Rulebase</h2>\n<p>Our team maintained two synchronized datasets throughout the competition:</p>\n<ol>\n<li>Plain Shortest Codes : the shortest uncompressed version.</li>\n<li>Best Score Codes : the version achieving the best score considering compression.</li>\n</ol>\n<p>After each update or improvement, both datasets were automatically refreshed through a post-processing pipeline.</p>\n<h3>3.1. Rule-based Post-processing Notebook</h3>\n<p>We developed 23 rule-based notebooks that automatically executed sequentially every minute.\nEach notebook applied a specific transformation to further shorten the code while maintaining correctness.\nThe following summarizes the main rules used:</p>\n<ol>\n<li>Remove comments</li>\n<li>Remove unnecessary whitespaces</li>\n<li>Remove soft (non-critical) whitespaces</li>\n<li>Expand <code>if True:</code> blocks and remove <code>if False:</code> blocks entirely</li>\n<li>Replace <code>True/False</code> with <code>1/0</code></li>\n<li>Replace <code>len(x) == 0</code> with <code>not x</code></li>\n<li>Replace <code>x == False</code> with <code>not x</code></li>\n<li>Replace <code>x == True</code> with <code>x</code></li>\n<li>Safely remove <code>pass</code> statements</li>\n<li>Shorten <code>return None</code> → <code>return</code></li>\n<li>Remove trailing semicolons <code>;</code></li>\n<li>Remove redundant parentheses</li>\n<li>Remove unused imports</li>\n<li>Remove unnecessary <code>list()</code> calls for literals</li>\n<li>Automatically shorten local variable, function, and argument names to one character</li>\n<li>Apply global one-character renaming via AST transformation</li>\n<li>Remove indentation and compress function bodies into single lines</li>\n<li>Replace <code>l.append(x)</code> with <code>l += [x]</code></li>\n<li>Shorten <code>from X import Y</code> → <code>from X import *</code></li>\n<li>Minify identifiers using external libraries (e.g., pyminifier-based tools)</li>\n<li>Reduce indentation width from 4 spaces to 1 space</li>\n<li>Inject short aliases for built-in functions (e.g., <code>range → R</code>)</li>\n<li>Remove all <code>print</code> statements</li>\n</ol>\n<p>This automated pipeline continuously refined submissions to approach minimal byte counts.\nBy keeping these transformations modular and incremental, we ensured that even minor edits propagated safely to the latest code versions.</p>\n<h3>3.2. AI-based Post-processing</h3>\n<p>Beyond the rule-based framework, we also automated AI-based post-processing notebooks using OpenAI API, Gemini API, and Grok API.\nThese notebooks are periodically executed to perform more flexible, non-rule-based optimizations and capture improvements that purely static rules could not.</p>\n<h3>3.3. pysearch</h3>\n<p>We applied targeted transformations using <a href=\"https://github.com/lynn/pysearch\" target=\"_blank\">pysearch</a>.\nBy specifying desired input/output pairs, pysearch synthesizes the shortest logic that satisfies them, which improved scores for some problems.</p>\n<p>Example:\nFor <code>task144.py</code>, we needed <code>3*(x==y)</code> with <code>x∈[0,7]</code> and <code>y∈[0,2]</code>.\npysearch rewrote it to <code>3&gt;&gt;x+y</code>, saving 2 bytes.</p>\n<hr>\n<h2>4. Code Golf by Compression</h2>\n<ul>\n<li><p><strong>Grammar optimization</strong>: Considering zlib/zopfli compression, we sometimes intentionally increased raw byte count to gain a better compressed score.\nFor example, aliasing <code>range</code> as <code>R=range</code> and using <code>R()</code> can reduce raw bytes, but not aliasing can compress better.\nWe first performed some of these conversions manually, then turned them into prompt examples and used GPT‑5 to revise code accordingly.</p></li>\n<li><p><strong>Variable-name optimization</strong>: Based on Garry Moss’s notebook\n<a href=\"https://www.kaggle.com/code/garrymoss/compressed-variable-name-optimization\" target=\"_blank\"><code>compressed-variable-name-optimization</code></a>,\nwe accelerated and tuned the search.\nWe deferred the expensive correctness checks, first computing compressed byte counts to speed up iterations, and reset the trial counter whenever the score improved.\nIn practice, we ran the compression-aware rewrites first and then applied variable-name optimization, which yielded better overall compression efficiency.</p></li>\n</ul>",
      "rawMarkdown": "We would like to thank the host and the Kaggle team for organizing such a creative and inspiring competition.\nIt was our first experience with code golf, and we learned a lot through this exciting challenge.\n\nTeam Member: @chimaki821 @isakatsuyoshi @denden12 @hikari30 @kosirowada\n\ntable of contents:\n1. Code Golf by Hand\n2. Code Golf by LLM\n3. Code Golf by Rulebase\n4. Code Golf by Compression\n\n---\n\n## 1. Code Golf by Hand\n\nSince it was difficult for LLMs to solve tasks from scratch or fundamentally improve the algorithms, we manually created many “initial solutions”.\nDuring this process, the discussions on the forum were extremely helpful. In particular, the two discussions we posted:\n\n[1] [How do you golf neighbor checks? (task279)](https://www.kaggle.com/competitions/google-code-golf-2025/discussion/611930)\n\n[2] [Diagonal Extension (task034)](https://www.kaggle.com/competitions/google-code-golf-2025/discussion/612504)\n\nled to productive exchanges with many participants, and we were able to incorporate the insights gained from them. We sincerely thank everyone who contributed.\n\n### 1.1. Algorithmic Improvements\n\nWe focused on simplifying algorithms by observing the test cases carefully and exploiting structural patterns in the grids.\nOur main strategies were as follows:\n\n#### (a) Simplify by Observation\n\n* Identify fixed input/output sizes or recurring numbers.\n* Detect common layouts, such as one object in the upper half and another in the lower half.\n* Apply operations to entire rows or grids whenever possible.\n\n#### (b) Write Concise and Efficient Logic\n\n* Use high-level functions like `any`, `all`, `zip`, `map`, `filter`, and `sum`.\n* Replace nested loops and index access with more direct operations.\n* Prefer `list.index` or `enumerate` over manual `range` loops.\n\n#### (c) Smart Techniques\n* Employ **recursion** and grid **rotation** to reduce repeated neighbor checks.\n* Leverage **regex** where possible — we solved 31 tasks with regex-based solutions.\n  * see [chimaki’s notebook](https://www.kaggle.com/code/chimaki821/step-by-step-regex-practice) \n\n### 1.2. Identifying Similar Problems\n\nWhile solving tasks manually, we noticed that many problems shared similar structures. Recognizing these patterns allowed us to apply successful strategies from one task to another.\nThanks to @denden12, who compiled a PDF summarizing all the tasks, we were able to discover such similarities more easily.\n\nFor example:\n\n* task154 and task390\n* task351 and task400\n* task246 and task335\n\nSince these problems were almost identical, we tackled them together and aimed for score improvements simultaneously.\n\n---\n\n## 2. Code Golf by LLM \n\nWe racked up a lot of points by taking strong human‑written seed solutions and then “follow‑up code‑golfing” them with AIs like GPT5 Pro.\n\nWe adopted this workflow for several reasons:\n\n- We wanted to conserve human effort and let AI handle what AI can.\n- Current high-end AI can relatively easily golf code when the approach direction is clear.\n  - However, AI struggles with 0->1 creation or escaping local optima, where manual code golf remains effective.\n- Diversity is key to escaping local optima, such as moving from a manual solution to AI, or from one AI to another, is the key to escaping local optima.\n\n### 2.1. LLMs We Used\n\n- We tried GPT, Claude, Gemini, and Grok.\n- In the early phase, we used Claude Code with DSL implementation to achieve AC results.\n- After GPT‑5 was released, we shifted toward it. GPT‑5’s performance was astonishing, achieving many 0→1 AC results.\n- In the end, we mainly used GPT‑5 Codex and GPT‑5 Pro.\n\n### 2.2. Prompt Engineering\n\nWe put significant effort into prompt design and system-level context construction.\nThe key idea was to provide good examples and rich context so that the LLM could learn how to golf effectively.\n\n- Added numerous examples of codes that had already matched the first-place scores.\n  - This enabled the models to perform advanced transformations such as recursion, which are typically difficult for LLMs.\n- Included before/after examples of effective golfing tips.\n- Used [GPT-5 Prompt Optimizer](https://platform.openai.com/chat/edit?models=gpt-5&optimize=true)  to systematically improve output quality and consistency.\n- Supplemented prompts with custom text files, which included:\n  - Programming-contest–style test cases.\n  - Translated [hints originally written in Chinese (by 暗黑AGI-san)](https://www.kaggle.com/code/boristown/agi-chinese-hints-for-all-400-tasks) , converted into Japanese and English via the [PLaMo API](https://plamo.preferredai.jp/) .\n- Combined these into a structured “prompt file” for each problem, containing code examples, test data, and textual hints.\n  The image below shows an example of such a prompt file used for AI-driven code golf experiments.\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F13641953%2Fa68a58665b6760f08a4f10415d0e989e%2Fimage.png?generation=1762391172517683&alt=media)\n\n- Using the tricks learned from writing shorter code manually helped the prompt get better scores. With this smart idea, even gpt-5-mini got a better score about one time out of every 50 tries.\n\nThese structured prompt files helped the models understand both the problem format and the essence of golfing strategies, resulting in consistently better code compression.\n\n### 2.3. Custom Evaluation Script for AI Agents\n\nWe modified the official evaluation script to better support LLM-based experimentation:\n\n- Displayed both plain byte count and zlib/zopfli-compressed size.\n- Computed and displayed the gap from the first-place score.\n  - For example, GPT-5 Codex often reduced a ~50-point gap to match the 1st-place score.\n- Implemented a hack filter to reject invalid or exploitative outputs, such as:\n\n  ```python\n  p=lambda g:type(\"\",(),{\"__eq__\":lambda*_:1})()\n  ```\n  (a 46-byte snippet that passes any test case locally but fails on the leaderboard).\n\n### 2.4. API Automation\n\nAfter collecting a sufficient number of strong initial solutions, we automated the process:\n\nFor each task, we prompted the models with:\n\n- A set of programming-contest–style test cases,\n- The currently shortest known code, and\n- 30–50 randomly selected solutions from other problems.\n\nUsing gpt-5, gpt-5-mini, and grok-4, we performed iterative improvements.\nOn average, gpt-5 produced a few-byte improvement once every 50 problems, and gpt-5-mini once every 100 problems.\n\n---\n\n## 3. Code Golf by Rulebase\n\nOur team maintained two synchronized datasets throughout the competition:\n\n1. Plain Shortest Codes : the shortest uncompressed version.\n2. Best Score Codes : the version achieving the best score considering compression.\n\nAfter each update or improvement, both datasets were automatically refreshed through a post-processing pipeline.\n\n### 3.1. Rule-based Post-processing Notebook\n\nWe developed 23 rule-based notebooks that automatically executed sequentially every minute.\nEach notebook applied a specific transformation to further shorten the code while maintaining correctness.\nThe following summarizes the main rules used:\n\n1. Remove comments\n2. Remove unnecessary whitespaces\n3. Remove soft (non-critical) whitespaces\n4. Expand `if True:` blocks and remove `if False:` blocks entirely\n5. Replace `True/False` with `1/0`\n6. Replace `len(x) == 0` with `not x`\n7. Replace `x == False` with `not x`\n8. Replace `x == True` with `x`\n9. Safely remove `pass` statements\n10. Shorten `return None` → `return`\n11. Remove trailing semicolons `;`\n12. Remove redundant parentheses\n13. Remove unused imports\n14. Remove unnecessary `list()` calls for literals\n15. Automatically shorten local variable, function, and argument names to one character\n16. Apply global one-character renaming via AST transformation\n17. Remove indentation and compress function bodies into single lines\n18. Replace `l.append(x)` with `l += [x]`\n19. Shorten `from X import Y` → `from X import *`\n20. Minify identifiers using external libraries (e.g., pyminifier-based tools)\n21. Reduce indentation width from 4 spaces to 1 space\n22. Inject short aliases for built-in functions (e.g., `range → R`)\n23. Remove all `print` statements\n\nThis automated pipeline continuously refined submissions to approach minimal byte counts.\nBy keeping these transformations modular and incremental, we ensured that even minor edits propagated safely to the latest code versions.\n\n### 3.2. AI-based Post-processing\n\nBeyond the rule-based framework, we also automated AI-based post-processing notebooks using OpenAI API, Gemini API, and Grok API.\nThese notebooks are periodically executed to perform more flexible, non-rule-based optimizations and capture improvements that purely static rules could not.\n\n\n### 3.3. pysearch\n\nWe applied targeted transformations using [pysearch](https://github.com/lynn/pysearch).\nBy specifying desired input/output pairs, pysearch synthesizes the shortest logic that satisfies them, which improved scores for some problems.\n\nExample:\nFor `task144.py`, we needed `3*(x==y)` with `x∈[0,7]` and `y∈[0,2]`.\npysearch rewrote it to `3>>x+y`, saving 2 bytes.\n\n---\n\n## 4. Code Golf by Compression\n\n- **Grammar optimization**: Considering zlib/zopfli compression, we sometimes intentionally increased raw byte count to gain a better compressed score.\n  For example, aliasing `range` as `R=range` and using `R()` can reduce raw bytes, but not aliasing can compress better.\n  We first performed some of these conversions manually, then turned them into prompt examples and used GPT‑5 to revise code accordingly.\n\n- **Variable-name optimization**: Based on Garry Moss’s notebook\n  [`compressed-variable-name-optimization`](https://www.kaggle.com/code/garrymoss/compressed-variable-name-optimization),\n  we accelerated and tuned the search.\n  We deferred the expensive correctness checks, first computing compressed byte counts to speed up iterations, and reset the trial counter whenever the score improved.\n  In practice, we ran the compression-aware rewrites first and then applied variable-name optimization, which yielded better overall compression efficiency.\n",
      "votes": 11
    }
  ],
  "complete": true,
  "source": "live",
  "fetched_at": "2026-10-19T12:49:49Z",
  "competition_id": 95282,
  "competition_slug": "google-code-golf-2025",
  "team_id": 14194763,
  "solution_url": "/competitions/google-code-golf-2025/writeups/15th-place-solution"
}