15.7 用地道 Kotlin 风格解答 Advent of Code 谜题
6 分钟阅读
15.7 用地道 Kotlin 风格解答 Advent of Code 谜题
Advent of Code 是一年一度的十二月活动,从 12 月 1 日到 12 月 25 日每天都会发布一道节日主题的谜题。在 Advent of Code 的创建者 Eric Wastl 的许可下,我们将展示如何用地道的 Kotlin 风格来解答这些谜题:
为 Advent of Code 做好准备
我们会带你了解一些基本技巧,帮助你开始用 Kotlin 解答 Advent of Code 挑战:
- 使用这个 GitHub 模板创建项目
- 看看 Kotlin 开发者布道师 Sebastian Aigner 的欢迎视频:
视频:Get Ready for Advent of Code 2021
Advent of Code 2022
第 1 天:卡路里计数
了解 Kotlin Advent of Code 模板,以及在 Kotlin 中处理字符串和集合的便捷函数,例如 maxOf() 和 sumOf()。看看扩展函数如何帮助你以优雅的方式组织解法。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 1 | Kotlin
第 2 天:石头剪刀布
理解 Kotlin 中 Char 类型的操作,看看 Pair 类型和 to 构造器如何与模式匹配很好地配合。了解如何使用 compareTo() 函数为自己的对象排序。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 2 | Kotlin
第 3 天:背包重新整理
了解 kotlinx.benchmark 库如何帮助你理解代码的性能特征。看看 intersect 这样的集合操作如何帮助你筛选出重叠的数据,并比较同一解法不同实现之间的性能。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 3 | Kotlin
第 4 天:营地清理
看看 infix 和 operator 函数如何让代码更具表现力,以及 String 和 IntRange 类型的扩展函数如何让输入解析变得简单。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 4 | Kotlin
第 5 天:补给堆栈
了解如何使用工厂函数构造更复杂的对象、如何使用正则表达式,以及双端队列 ArrayDeque 类型。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 5 | Kotlin
第 6 天:调谐故障
借助 kotlinx.benchmark 库进行更深入的性能研究,比较同一解法 16 种不同变体的特征。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 6 | Kotlin
第 7 天:设备空间不足
了解如何为树形结构建模,并观看以编程方式生成 Kotlin 代码的演示。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 7 | Kotlin
第 8 天:树梢树屋
看看 sequence 构建器的实际应用,以及程序的初稿与地道的 Kotlin 解法能有多大差别(特邀嘉宾 Roman Elizarov!)。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 8 | Kotlin
第 9 天:绳桥
看看 run 函数、带标签的 return,以及 coerceIn、zipWithNext 等便捷的标准库函数。了解如何使用 List 和 MutableList 构造器构建指定大小的列表,并一窥基于 Kotlin 的题目可视化。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 9 | Kotlin
第 10 天:阴极射线管
了解区间和 in 运算符如何让范围检查变得自然,函数参数如何变成接收者,并简要探索 tailrec 修饰符。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 10 | Kotlin
第 11 天:中间的猴子
看看如何从可变的命令式代码转向更多使用不可变和只读数据结构的函数式风格。了解上下文接收者,以及我们的嘉宾如何专门为 Advent of Code 打造了自己的可视化库。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 11 | Kotlin
第 12 天:爬山算法
使用队列、ArrayDeque、函数引用以及 tailrec 修饰符,用 Kotlin 解决寻路问题。
- 阅读 Advent of Code 上的谜题描述
- 在视频中查看解法:
Advent of Code 2022 Day 12 | Kotlin
Advent of Code 2021
提示: 阅读我们关于 Advent of Code 2021 的博客文章
第 1 天:声呐扫描
运用 windowed 和 count 函数来处理整数对和整数三元组。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Anton Arhipov 的解法,或观看视频:
Advent of Code 2021 in Kotlin, Day 1: Sonar Sweep
第 2 天:下潜!
了解解构声明和 when 表达式。
- 阅读 Advent of Code 上的谜题描述
- 在 GitHub 上查看 Pasha Finkelshteyn 的解法,或观看视频:
Advent of Code 2021 in Kotlin, Day 2: Dive!
第 3 天:二进制诊断
探索处理二进制数的不同方式。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Sebastian Aigner 的解法,或观看视频:
Advent of Code 2021 in Kotlin, Day 3: Binary Diagnostic
第 4 天:巨型鱿鱼
了解如何解析输入,并引入一些领域类以便更方便地处理数据。
- 阅读 Advent of Code 上的谜题描述
- 在 GitHub 上查看 Anton Arhipov 的解法,或观看视频:
Advent of Code 2021 in Kotlin, Day 4: Giant Squid
Advent of Code 2020
提示: 你可以在我们的 GitHub 仓库中找到 Advent of Code 2020 谜题的全部解法。
第 1 天:报告修复
探索输入处理、遍历列表、构建映射的不同方式,以及如何使用 let 函数简化代码。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Svetlana Isakova 的解法,或观看视频:
Learn Kotlin With the Kotlin Team: Advent of Code 2020 #1
第 2 天:密码哲学
探索字符串工具函数、正则表达式、集合操作,以及 let 函数如何帮助你转换表达式。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Svetlana Isakova 的解法,或观看视频:
Learn Kotlin with The Kotlin Team: Advent of Code 2020 #2
第 3 天:雪橇轨迹
比较命令式风格与更函数式的风格,使用整数对和 reduce() 函数,在列选择模式下编辑代码,并修复整数溢出问题。
- 阅读 Advent of Code 上的谜题描述
- 在 GitHub 上查看 Mikhail Dvorkin 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #3
第 4 天:护照处理
运用 when 表达式,并探索验证输入的不同方式:工具函数、处理区间、检查集合成员关系以及匹配特定的正则表达式。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Sebastian Aigner 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #4
第 5 天:二进制登机
使用 Kotlin 标准库函数(replace()、toInt()、find())处理数字的二进制表示,探索强大的局部函数,并了解如何在 Kotlin 1.5 中使用 max() 函数。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Svetlana Isakova 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #5
第 6 天:自定义海关
了解如何使用标准库函数 map()、reduce()、sumOf()、intersect() 和 union() 对字符串和集合中的字符进行分组和计数。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Anton Arhipov 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #6
第 7 天:便利背包
了解如何使用正则表达式、如何从 Kotlin 中使用 Java 的 compute() 方法对 HashMap 的值进行动态计算、如何使用 forEachLine() 函数读取文件,并比较两种搜索算法:深度优先和广度优先。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Pasha Finkelshteyn 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #7
第 8 天:掌机停机
运用密封类和 lambda 来表示指令,使用 Kotlin 集合发现程序执行中的循环,使用序列和 sequence { } 构建器函数构造惰性集合,并试用实验性的 measureTimedValue() 函数来查看性能指标。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Sebastian Aigner 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #8
第 9 天:编码错误
探索在 Kotlin 中操作列表的不同方式,使用 any()、firstOrNull()、firstNotNullOfOrNull()、windowed()、takeIf() 和 scan() 等函数,它们正是地道 Kotlin 风格的典范。
- 阅读 Advent of Code 上的谜题描述
- 在 Kotlin 博客上查看 Svetlana Isakova 的解法,或观看视频:
Learn Kotlin with the Kotlin Team: Advent of Code 2020 #9
接下来做什么?
- 通过 Kotlin Koans完成更多练习
- 学习 JetBrains Academy 的免费 Kotlin Core 课程,创建可运行的应用程序