Save 37% off PRO during our Black Friday Sale! »

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.

2777430c1f9869e411ab5d8b17d8ba2a?s=128

Todd Ginsberg

January 27, 2021
Tweet

Transcript

  1. None
  2. None
  3. Photo by Elena Mozhvilo on Unsplash Photo by Markus Spiske

    on Unsplash
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. • •

  13. • • •

  14. None
  15. 2020 Unofficial Advent of Code Survey - Jeroen Heijmans

  16. None
  17. Daniel Lin (@ephemient) Solved every daily puzzle in four different

    languages. ephemient.github.io/aoc2020/
  18. Joris Portegies Zwart (@jorispz) Solves puzzles using Kotlin Multiplatform. github.com/jorispz/aoc-2020

  19. Jakub Gwóźdź Solved every puzzle and provided a visualization. In

    Kotlin! jakubgwozdz.github.io/advent-of- code-2020/
  20. Courtesy of @FuriousProgramm @FuriousProgramm Also attempting to solve this puzzle.

    Manually.
  21. Since 2017, I have solved each puzzle each day, in

    Kotlin and blogged about it. https://todd.ginsberg.com
  22. None
  23. val letters = listOf("A", "B", "C", "D") val numbers =

    listOf(1, 2, 3, 4)
  24. 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<Pair<String,Int>>
  25. val letters = listOf("A", "B", "C", "D")

  26. val letters = listOf("A", "B", "C", "D") letters.zipWithNext() // [(A,

    B), (B, C), (C, D)] // List<Pair<String,String>>
  27. val letters = listOf("A", "B", "C", "D")

  28. val letters = listOf("A", "B", "C", "D") letters.chunked(2) // [[A,

    B], [C, D]] // List<List<String>>
  29. val letters = listOf("A", "B", "C", "D", "E", "F")

  30. 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<List<String>>
  31. val letters = listOf("A", "B", "C", "D", "E", "F") letters.windowed(3,

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

    4, false) // [[A, B, C, D]]
  33. val theString = "Hello" theString.padEnd(7, '!') // Hello!!

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

  35. val theString = "Left Right"

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

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

    ") // "Right"
  38. val someList = listOf("A", "B", "C")

  39. val someList = listOf("A", "B", "C") someList.any { it.length ==

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

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

    2 } // True!
  42. "3".toInt() // 3

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

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

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

  46. • •

  47. • • •

  48. • • • •

  49. • • • • •

  50. None
  51. None
  52. None
  53. None
  54. None
  55. None
  56. None