Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advent of Code in Kotlin: Lessons Learned

Advent of Code in Kotlin: Lessons Learned

An introduction to the Advent of Code and what I learned about solving puzzles in Kotlin.

Todd Ginsberg

January 27, 2021
Tweet

More Decks by Todd Ginsberg

Other Decks in Programming

Transcript

  1. Photo by Elena Mozhvilo on Unsplash Photo by Markus Spiske on Unsplash

    View full-size slide

  2. 2020 Unofficial Advent of Code Survey - Jeroen Heijmans

    View full-size slide

  3. Daniel Lin (@ephemient)
    Solved every daily puzzle in
    four different languages.
    ephemient.github.io/aoc2020/

    View full-size slide

  4. Joris Portegies Zwart (@jorispz)
    Solves puzzles using Kotlin
    Multiplatform.
    github.com/jorispz/aoc-2020

    View full-size slide

  5. Jakub Gwóźdź
    Solved every puzzle and
    provided a visualization.
    In Kotlin!
    jakubgwozdz.github.io/advent-of-
    code-2020/

    View full-size slide

  6. Courtesy of @FuriousProgramm
    @FuriousProgramm
    Also attempting to solve this
    puzzle.
    Manually.

    View full-size slide

  7. Since 2017, I have solved
    each puzzle each day, in
    Kotlin and blogged about it.
    https://todd.ginsberg.com

    View full-size slide

  8. val letters = listOf("A", "B", "C", "D")
    val numbers = listOf(1, 2, 3, 4)

    View full-size slide

  9. val letters = listOf("A", "B", "C", "D")
    val numbers = listOf(1, 2, 3, 4)
    letters.zip(numbers)
    // [(A, 1), (B, 2), (C, 3), (D, 4)]
    // List>

    View full-size slide

  10. val letters = listOf("A", "B", "C", "D")

    View full-size slide

  11. val letters = listOf("A", "B", "C", "D")
    letters.zipWithNext()
    // [(A, B), (B, C), (C, D)]
    // List>

    View full-size slide

  12. val letters = listOf("A", "B", "C", "D")

    View full-size slide

  13. val letters = listOf("A", "B", "C", "D")
    letters.chunked(2)
    // [[A, B], [C, D]]
    // List>

    View full-size slide

  14. val letters = listOf("A", "B", "C", "D", "E", "F")

    View full-size slide

  15. val letters = listOf("A", "B", "C", "D", "E", "F")
    letters.windowed(3)
    // [[A, B, C], [B, C, D], [C, D, E], [D, E, F]]
    // List>

    View full-size slide

  16. val letters = listOf("A", "B", "C", "D", "E", "F")
    letters.windowed(3, 3)
    // [[A, B, C], [D, E, F]]

    View full-size slide

  17. val letters = listOf("A", "B", "C", "D", "E", "F")
    letters.windowed(4, 4, false)
    // [[A, B, C, D]]

    View full-size slide

  18. val theString = "Hello"
    theString.padEnd(7, '!')
    // Hello!!

    View full-size slide

  19. val theString = "Hello"
    theString.padStart(7, '!')
    // !!Hello

    View full-size slide

  20. val theString = "Left Right"

    View full-size slide

  21. val theString = "Left Right"
    theString.substringBefore(" ") // "Left"

    View full-size slide

  22. val theString = "Left Right"
    theString.substringBefore(" ") // "Left"
    theString.substringAfter(" ") // "Right"

    View full-size slide

  23. val someList = listOf("A", "B", "C")

    View full-size slide

  24. val someList = listOf("A", "B", "C")
    someList.any { it.length == 1 } // True!

    View full-size slide

  25. val someList = listOf("A", "B", "C")
    someList.all { it.length == 1 } // True!

    View full-size slide

  26. val someList = listOf("A", "B", "C")
    someList.none { it.length == 2 } // True!

    View full-size slide

  27. "3".toInt() // 3

    View full-size slide

  28. "3".toInt() // 3
    '3'.toInt() // 51

    View full-size slide

  29. fun Char.asInt(): Int =
    this.toString().toInt()

    View full-size slide

  30. fun Char.asInt(): Int =
    this.toString().toInt()
    '3'.asInt() // 3

    View full-size slide





  31. View full-size slide






  32. View full-size slide