Advent of Code 2021 day 2
12/02/2021, 5:00 AMDavid Whittaker
12/02/2021, 5:10 AMwhen
!pavi2410
12/02/2021, 5:11 AMKroppeb
12/02/2021, 5:21 AMKroppeb
12/02/2021, 5:21 AMKroppeb
12/02/2021, 5:23 AMpavi2410
12/02/2021, 5:25 AMKroppeb
12/02/2021, 6:36 AMPaul Woitaschek
12/02/2021, 7:41 AMMichael de Kaste
12/02/2021, 7:56 AMPaul Woitaschek
12/02/2021, 8:26 AMMichael Böiers
12/02/2021, 9:27 AMfun List<String>.day02() = with(parse()) { follow(Pos::move1) to follow(Pos::move2) }
private fun List<String>.parse() = map { it.split(" ") }.map { it[0] to it[1].toInt() }
private inline fun List<Pair<String, Int>>.follow(f: Pos.(String, Int) -> Unit) =
fold(Pos()) { p, (cmd, x) -> p.apply { f(cmd, x) } }.calc()
private data class Pos(var horizontal: Int = 0, var depth: Int = 0, var aim: Int = 0) {
fun move1(cmd: String, x: Int) {
when (cmd) {
"forward" -> horizontal += x
"up" -> depth -= x
"down" -> depth += x
}
}
fun move2(cmd: String, x: Int) {
when (cmd) {
"forward" -> {
horizontal += x; depth += aim * x
}
"up" -> aim -= x
"down" -> aim += x
}
}
fun calc() = horizontal * depth
}
ephemient
12/02/2021, 9:36 AMgammax
12/02/2021, 9:45 AMTriple
that plays really well with destructuring declarations.ephemient
12/02/2021, 9:49 AM.map { (first, second) -> first to second.toInt() }
for exampleMichael de Kaste
12/02/2021, 9:56 AMMichael Böiers
12/02/2021, 10:06 AMephemient
12/02/2021, 10:08 AMMichael de Kaste
12/02/2021, 10:17 AMPair<Map<Int, List<List<{String & Int}>>>, List<String>>
for instance. But then again, I tend to preprocess data a lot because we're dealing with 2 parts on the puzzles.Paul Woitaschek
12/02/2021, 11:13 AMMichael Böiers
12/02/2021, 11:26 AMMichael Böiers
12/02/2021, 11:26 AMPaul Woitaschek
12/02/2021, 11:27 AMMichael Böiers
12/02/2021, 11:28 AMtypealias Grid = Map<Point, Tile>
.Paul Woitaschek
12/02/2021, 11:29 AMPaul Woitaschek
12/02/2021, 11:29 AMMichael Böiers
12/02/2021, 11:30 AMJesse Hill
12/02/2021, 4:01 PMcompanion object
though, that was a nice way to define the initial position and to parse the input.Luke
12/02/2021, 4:08 PMgnu
12/02/2021, 4:08 PMhttps://www.youtube.com/watch?v=0GWTTSMatO8&list=PLlFc5cFwUnmwfLRLvIM7aV7s73eSTL005&index=8▾
Sebastian Aigner
12/02/2021, 4:16 PMgnu
12/02/2021, 4:31 PMSebastian Aigner
12/02/2021, 4:47 PMMichael Böiers
12/02/2021, 8:09 PMEmil Kantis
12/02/2021, 10:12 PM