Skip to content

使用慣用 Kotlin 解決 Advent of Code 謎題

Advent of Code 是每年 12 月舉行的年度盛會,從 12 月 1 日到 12 月 25 日,每天都會發佈一個以節日為主題的謎題。經 Advent of Code 創始人 Eric Wastl 的許可,我們將展示如何使用慣用的 Kotlin 風格來解決這些謎題:

為 Advent of Code 做好準備

我們將帶您了解如何使用 Kotlin 開始解決 Advent of Code 挑戰的基本技巧:

  • 使用 此 GitHub 範本 來建立專案
  • 觀看 Kotlin 技術傳教士 Sebastian Aigner 的歡迎影片:

Advent of Code 2022

第一天:卡路里計數 (Calorie counting)

進一步了解 Kotlin Advent of Code 範本 以及在 Kotlin 中處理字串與集合的便利函式,例如 maxOf()sumOf()。看看擴充方法如何幫助您以優雅的方式建構解決方案。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 1 | Kotlin

第二天:剪刀石頭布 (Rock paper scissors)

了解 Kotlin 中 Char 型別的操作,看看 Pair 型別和 to 建構函式如何與模式配對完美配合。了解如何使用 compareTo() 函式對您自己的物件進行排序。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 2 | Kotlin

第三天:背包整理 (Rucksack reorganization)

了解 kotlinx.benchmark 程式庫如何幫助您了解程式碼的效能特性。看看像 intersect 這樣的集合運算如何幫助您選取重疊的資料,並查看相同解決方案的不同實作之間的效能比較。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 3 | Kotlin

第四天:營地清理 (Camp cleanup)

看看 infixoperator 函式如何讓您的程式碼更具表現力,以及 StringIntRange 型別的擴充方法如何讓解析輸入變得容易。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 4 | Kotlin

第五天:供應堆疊 (Supply stacks)

了解如何使用工廠函式建構更複雜的物件、如何使用正規表示式,以及雙端隊列 ArrayDeque 型別。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 5 | Kotlin

第六天:調頻煩惱 (Tuning trouble)

使用 kotlinx.benchmark 程式庫進行更深入的效能調查,比較同一個解決方案的 16 種不同變體的特性。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 6 | Kotlin

第七天:裝置空間不足 (No space left on device)

了解如何建立樹狀結構模型,並查看以程式化方式產生 Kotlin 程式碼的演示。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 7 | Kotlin

第八天:樹頂樹屋 (Treetop tree house)

看看實務中的 sequence 產生器,以及程式的初稿與慣用的 Kotlin 解決方案之間有多大的差異(特別嘉賓 Roman Elizarov!)。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 8 | Kotlin

第九天:繩索橋 (Rope bridge)

查看 run 函式、帶標籤的 return,以及便利的標準函式庫函式,如 coerceInzipWithNext。了解如何使用 ListMutableList 建構函式建立給定大小的列表,並一窺基於 Kotlin 的問題陳述視覺化。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 9 | Kotlin

第十天:陰極射線管 (Cathode-ray tube)

了解範圍(ranges)和 in 運算子如何讓檢查範圍變得自然,函式參數如何轉換為接收者(receivers),以及對 tailrec 修飾符的簡要探索。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 10 | Kotlin

第十一天:中間的猴子 (Monkey in the middle)

了解如何從可變的、指令式程式碼轉向利用不可變且唯讀資料結構的函式編程方式。了解上下文接收者(context receivers),以及我們的嘉賓如何專為 Advent of Code 建立自己的視覺化程式庫。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 11 | Kotlin

第十二天:爬山演算法 (Hill Climbing algorithm)

使用隊列、ArrayDeque、函式參照和 tailrec 修飾符來解決 Kotlin 的路徑尋找問題。

  • Advent of Code 上閱讀謎題描述
  • 在影片中查看解決方案:

YouTube Advent of Code 2022 Day 12 | Kotlin

Advent of Code 2021

閱讀我們關於 Advent of Code 2021 的部落格文章

第一天:聲納掃描 (Sonar sweep)

應用視窗化(windowed)和計數函式來處理成對和三組整數。

YouTube Advent of Code 2021 in Kotlin, Day 1: Sonar Sweep

第二天:潛水! (Dive!)

了解解構宣告和 when 運算式。

  • Advent of Code 上閱讀謎題描述
  • GitHub 上查看 Pasha Finkelshteyn 的解決方案,或觀看影片:

YouTube Advent of Code 2021 in Kotlin, Day 2: Dive!

第三天:二進位診斷 (Binary diagnostic)

探索處理二進位數字的不同方法。

YouTube Advent of Code 2021 in Kotlin, Day 3: Binary Diagnostic

第四天:巨型魷魚 (Giant squid)

了解如何解析輸入並引入一些領域類別以進行更方便的處理。

  • Advent of Code 上閱讀謎題描述
  • GitHub 上查看 Anton Arhipov 的解決方案,或觀看影片:

YouTube Advent of Code 2021 in Kotlin, Day 4: Giant Squid

Advent of Code 2020

您可以在我們的 GitHub 儲存庫 中找到 Advent of Code 2020 謎題的所有解決方案。

第一天:報告修復 (Report repair)

探索輸入處理、對列表進行迭代、建置 Map 的不同方式,以及使用 let 函式來簡化您的程式碼。

YouTube Learn Kotlin With the Kotlin Team: Advent of Code 2020 #1

第二天:密碼哲學 (Password philosophy)

探索字串公用函式、正規表示式、集合操作,以及 let 函式如何幫助轉換您的運算式。

YouTube Learn Kotlin with The Kotlin Team: Advent of Code 2020 #2

第三天:雪橇軌跡 (Toboggan trajectory)

比較指令式和更具函式編程風格的程式碼、使用 Pair 和 reduce() 函式、在欄選取模式下編輯程式碼,以及修復整數溢位。

  • Advent of Code 上閱讀謎題描述
  • GitHub 上查看 Mikhail Dvorkin 的解決方案,或觀看影片:

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #3

第四天:護照處理 (Passport processing)

應用 when 運算式並探索驗證輸入的不同方式:公用函式、使用範圍、檢查集合成員資格以及配對特定的正規表示式。

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #4

第五天:二進位登機 (Binary boarding)

使用 Kotlin 標準函式庫函式 (replace()toInt()find()) 來處理數字的二進位表示,探索強大的區域函式,並了解如何在 Kotlin 1.5 中使用 max() 函式。

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #5

第六天:自訂海關 (Custom customs)

了解如何使用標準函式庫函式:map()reduce()sumOf()intersect()union() 對字串和集合中的字元進行分組和計數。

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #6

第七天:便利的背囊 (Handy haversacks)

了解如何使用正規表示式、從 Kotlin 中使用 Java HashMap 的 compute() 方法進行動態值計算、使用 forEachLine() 函式讀取檔案,並比較兩種類型的搜尋演算法:深度優先和廣度優先。

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #7

第八天:手持設備停機 (Handheld halting)

應用密封類別和 Lambda 來表示指令,應用 Kotlin 集合來發現程式執行中的迴圈,使用序列和 sequence { } 產生器函式來建構延遲載入集合,並嘗試實驗性的 measureTimedValue() 函式來檢查效能指標。

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #8

第九天:編碼錯誤 (Encoding error)

探索在 Kotlin 中使用 any()firstOrNull()firstNotNullOfOrNull()windowed()takeIf()scan() 函式操作列表的不同方法,這些都是慣用 Kotlin 風格的典範。

YouTube Learn Kotlin with the Kotlin Team: Advent of Code 2020 #9

下一步?