用地道 Kotlin 代码求解 Advent of Code 谜题

Advent of Code is an annual December event, where holiday-themed puzzles are published every day from December 1 to December 25. With the permission of Eric Wastl, creator of Advent of Code, we’ll show how to solve these puzzles using the idiomatic Kotlin style:

Advent of Code 2021

Get ready

We’ll take you through the basic tips on how to get up and running with solving Advent of Code challenges with Kotlin:

Day 1: Sonar sweep

Apply windowed and count functions to work with pairs and triplets of integers.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Anton Arhipov on the Kotlin Blog or watch the video:

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

Day 2: Dive!

Learn about destructuring declarations and the when expression.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Pasha Finkelshteyn on GitHub or watch the video:

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

Day 3: Binary diagnostic

Explore different ways to work with binary numbers.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Sebastian Aigner on Kotlin Blog or watch the video:

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

Day 4: Giant squid

Learn how to parse the input and introduce some domain classes for more convenient processing.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Anton Arhipov on the GitHub or watch the video:

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

Advent of Code 2020

You can find all the solutions for the Advent of Code 2020 puzzles in our GitHub repository.

Advent of Code 谜题 - 图1

Day 1: Report repair

Explore input handling, iterating over a list, different ways of building a map, and using the let function to simplify your code.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:

YouTube 视频:Kotlin Tutorial: Advent of Code Puzzles, Day 1

Day 2: Password philosophy

Explore string utility functions, regular expressions, operations on collections, and how the let function can be helpful to transform your expressions.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:

YouTube 视频:Kotlin Tutorial: Advent of Code Puzzles, Day 2

Day 3: Toboggan trajectory

Compare imperative and more functional code styles, work with pairs and the reduce() function, edit code in the column selection mode, and fix integer overflows.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Mikhail Dvorkin on GitHub or watch the video:

YouTube 视频:Kotlin Tutorial: Adopting a Functional Style for Advent of Code Puzzles

Day 4: Passport processing

Apply the when expression and explore different ways of how to validate the input: utility functions, working with ranges, checking set membership, and matching a particular regular expression.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Sebastian Aigner on the Kotlin Blog or watch the video:

YouTube 视频:Kotlin Tutorial: Validating and Sanitizing Input. Advent of Code Puzzles

Day 5: Binary boarding

Use the Kotlin standard library functions (replace(), toInt(), find()) to work with the binary representation of numbers, explore powerful local functions, and learn how to use the max() function in Kotlin 1.5.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:

YouTube 视频:Kotlin Tutorial: Binary Representation of Numbers. Advent of Code Puzzles

Day 6: Custom customs

Learn how to group and count characters in strings and collections using the standard library functions: map(), reduce(), sumOf(), intersect(), and union().

  • Read the puzzle description on Advent of Code
  • Check out the solution from Anton Arhipov on the Kotlin Blog or watch the video:

YouTube 视频:Idiomatic Kotlin: Operations with Sets

Day 7: Handy haversacks

Learn how to use regular expressions, use Java’s compute() method for HashMaps from Kotlin for dynamic calculations of the value in the map, use the forEachLine() function to read files, and compare two types of search algorithms: depth-first and breadth-first.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Pasha Finkelshteyn on the Kotlin Blog or watch the video:

YouTube 视频:Idiomatic Kotlin: Solving Advent of Code Puzzles and Traversing Trees

Day 8: Handheld halting

Apply sealed classes and lambdas to represent instructions, apply Kotlin sets to discover loops in the program execution, use sequences and the sequence { } builder function to construct a lazy collection, and try the experimental measureTimedValue() function to check performance metrics.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Sebastian Aigner on the Kotlin Blog or watch the video:

YouTube 视频:Sealed Classes, Sequences, Immutability: Idiomatic Kotlin Solving Advent of Code Puzzles

Day 9: Encoding error

Explore different ways to manipulate lists in Kotlin using the any(), firstOrNull(), firstNotNullOfOrNull(), windowed(), takeIf(), and scan() functions, which exemplify an idiomatic Kotlin style.

  • Read the puzzle description on Advent of Code
  • Check out the solution from Svetlana Isakova on the Kotlin Blog or watch the video:

YouTube 视频:Manipulating lists using windowed, scan, firstNotNullOfOrNull: Solving Advent of Code Puzzles

下一步做什么?