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

第 1 天:卡路里计数 (Calorie counting)

了解 Kotlin Advent of Code 模板以及在 Kotlin 中处理字符串和集合的便捷函数,例如 maxOf()sumOf()。查看扩展函数如何帮助您以优雅的方式构建解决方案。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 1 | Kotlin

第 2 天:石头剪刀布 (Rock paper scissors)

了解 Kotlin 中 Char 类型上的操作,查看 Pair 类型和 to 构造函数如何很好地配合模式匹配。了解如何使用 compareTo() 函数对您自己的对象进行排序。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 2 | Kotlin

第 3 天:背包重整 (Rucksack reorganization)

了解 kotlinx.benchmark 库如何帮助您了解代码的性能特性。查看 intersect 等集合操作如何帮助您选择重叠数据,并查看同一解决方案的不同实现之间的性能比较。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 3 | Kotlin

第 4 天:营地清理 (Camp cleanup)

查看 infixoperator 函数如何让您的代码更具表现力,以及 StringIntRange 类型的扩展函数如何让解析输入变得简单。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 4 | Kotlin

第 5 天:供应堆栈 (Supply stacks)

学习使用工厂函数构建更复杂的对象,如何使用正则表达式,以及双端队列 ArrayDeque 类型。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 5 | Kotlin

第 6 天:调谐麻烦 (Tuning trouble)

通过 kotlinx.benchmark 库查看更深入的性能调查,比较同一解决方案的 16 个不同变体的特性。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 6 | Kotlin

第 7 天:设备上没有剩余空间 (No space left on device)

学习如何建模树形结构,并查看以编程方式生成 Kotlin 代码的演示。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 7 | Kotlin

第 8 天:树顶树屋 (Treetop tree house)

查看实际运行中的 sequence 构建器,以及程序的初稿与惯用 Kotlin 解决方案之间的巨大差异(特邀嘉宾 Roman Elizarov!)。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 8 | Kotlin

第 9 天:绳桥 (Rope bridge)

查看 run 函数、带标签的返回以及便捷的标准库函数,如 coerceInzipWithNext。查看如何使用 ListMutableList 构造函数构建给定大小的列表,并预览基于 Kotlin 的问题陈述可视化。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 9 | Kotlin

第 10 天:阴极射线管 (Cathode-ray tube)

了解区间和 in 运算符如何让区间检查变得自然,如何将函数参数转换为接收者,以及对 tailrec 修饰符的简要探索。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 10 | Kotlin

第 11 天:中间的猴子 (Monkey in the middle)

查看如何从可变的命令式代码转向利用不可变和只读数据结构的更具函数式的方法。了解上下文接收者,以及我们的嘉宾如何专为 Advent of Code 构建自己的可视化库。

  • Advent of Code 上阅读谜题说明
  • 在视频中查看解决方案:

YouTube Advent of Code 2022 Day 11 | Kotlin

第 12 天:爬山算法 (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 的博客文章

第 1 天:声呐扫描 (Sonar sweep)

应用窗口化和计数函数来处理整数对和三元组。

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

第 2 天:潜水! (Dive!)

了解析构声明和 when 表达式。

  • Advent of Code 上阅读谜题说明
  • 查看 Pasha Finkelshteyn 在 GitHub 上的解决方案,或观看视频:

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

第 3 天:二进制诊断 (Binary diagnostic)

探索处理二进制数的不同方法。

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

第 4 天:巨型乌贼 (Giant squid)

了解如何解析输入并引入一些领域类以进行更方便的处理。

  • Advent of Code 上阅读谜题说明
  • 查看 Anton Arhipov 在 GitHub 上的解决方案,或观看视频:

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

Advent of Code 2020

您可以在我们的 GitHub 仓库中找到 Advent of Code 2020 谜题的所有解决方案。

第 1 天:报告修复 (Report repair)

探索输入处理、遍历列表、构建 Map 的不同方式,以及使用 let 函数简化代码。

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

第 2 天:密码哲学 (Password philosophy)

探索字符串实用函数、正则表达式、集合操作,以及 let 函数如何帮助转换表达式。

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

第 3 天:雪橇轨迹 (Toboggan trajectory)

比较命令式和更多函数式代码风格,使用 Pair 和 reduce() 函数,在列选择模式下编辑代码,并修复整数溢出。

  • Advent of Code 上阅读谜题说明
  • 查看 Mikhail Dvorkin 在 GitHub 上的解决方案,或观看视频:

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

第 4 天:护照处理 (Passport processing)

应用 when 表达式并探索验证输入的各种方式:实用函数、使用区间、检查集合成员资格以及匹配特定的正则表达式。

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

第 5 天:二进制登机 (Binary boarding)

使用 Kotlin 标准库函数(replace()toInt()find())处理数字的二进制表示,探索强大的局部函数,并学习如何在 Kotlin 1.5 中使用 max() 函数。

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

第 6 天:定制海关 (Custom customs)

学习如何使用标准库函数对字符串和集合中的字符进行分组和计数:map()reduce()sumOf()intersect()union()

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

第 7 天:便利提篮 (Handy haversacks)

学习如何使用正则表达式,如何在 Kotlin 中使用 Java 的 HashMap compute() 方法进行 Map 值的动态计算,使用 forEachLine() 函数读取文件,并比较两类搜索算法:深度优先和广度优先。

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

第 8 天:手持设备停机 (Handheld halting)

应用密封类和 lambda 来表示指令,应用 Kotlin 集合来发现程序执行中的循环,使用序列和 sequence { } 构建器函数构建延迟集合,并尝试实验性的 measureTimedValue() 函数来检查性能指标。

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

第 9 天:编码错误 (Encoding error)

探索在 Kotlin 中操作列表的不同方式,使用 any()firstOrNull()firstNotNullOfOrNull()windowed()takeIf()scan() 函数,这些函数体现了惯用的 Kotlin 风格。

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

接下来的步骤