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

preview_player
Показать описание
Let’s continue to learn Kotlin by completing tasks from Advent of Code 2020! In this series, we show how you can solve Advent of Code 2020 puzzles using idiomatic Kotlin so you not just learn Kotlin you learn how to write concise idiomatic Kotlin code.

Advent of Code Day 3: Toboggan trajectory
We’ll learn how to take code written in an imperative fashion and rewrite it in a more functional style. We’ll also discuss how to work with pairs, use ‘reduce’, edit code in column selection mode, and fix integer overflows.

0:00 Advent of Code 2020 Day 3 in Kotlin
0:23 Problem description
2:07 Solving part I
4:19 Rewriting the solution in a functional style
4:41 Solving part II

* Used with the permission of Advent of Code (Eric Wastl)
#Kotlin #Idiomatic #Tutorial #AdventOfCode #AdventOfCode2020 #AdventOfСodeInKotlin #Day3
Рекомендации по теме
Комментарии
Автор

I have never used kotlin. This is the first video I have seen on using it. I am happily surprised I can immediately understand what you are talking about.

cloneab
Автор

“Solve problem, use kotlin, have fun” indeed!

TimSchraepen
Автор

I don't agree with the code style of the end result... the arithmetics are exactly the kind of code you scratch your head over two weeks later. It works, it is efficient, but not intention revealing at all. I'd prefer a regular loop which tracks a "currentX" and "currentY" and adds the delta until currentY is greater/equal list length. CurrentX has to be taken modulo width after each addition. Like so:

var treeCount = 0
var currentX = 0
for(currentY in 0 until lines.size step dy) {
currentX = (currentX +dx) % width
val character = lines[currentY][currentX]
if(character == '#'){
treeCount++
}
}
return treeCount

AlanDarkworld
Автор

Awesome, kotlin bring the pleasure to make software again

wendersonfsouza
Автор

Thank you for the video! One suggestion I have is to always use the very handy tool by JetBrains IDEs where a small tooltip pops at the bottom every time you press a shortcut. The other videos had that alright, but this one didn't.

GakisStylianos
Автор

For challenge 2, would `fold` be better than `map().reduce()`?

For instance,
vectors.fold(1) { acc: BigInteger, vector: Pair<Int, Int> -> acc * solve(field, vector) }

blinkmacalahan