<Advent of Code 2021 day 2> :thread:
# advent-of-code
a
d
Was it me or was that just so much easier? Thank you
when
!
K 7
p
Yes
k
intellij decided to not update the resource file for some reason
was very confused why nothing was working
I'm also assuming everyone solved it in the same way
p
Yep, it was so trivial. Even Copilot suggested me the intcode snippet that saved me some time
k
I'm planning on redoing my template, cause it actually hurts copilot a lot
m
trivial task today, I suppose those happen in the first week. https://github.com/mdekaste/AdventOfCode2021/blob/main/src/main/kotlin/year2021/day2/Day2.kt
p
@Michael de Kaste Oops thats so much shorter than mine 😛
m
Copy code
fun 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
}
e
I used regex to parse initially but switched to split+when which was simpler and faster
💯 1
g
e
lists and arrays can be destructured as well, you could have written
Copy code
.map { (first, second) -> first to second.toInt() }
for example
👍 1
m
@Paul Woitaschek your solution is more typesafe, and making a proper class from input with enums and whatnot is what I usually do on the later, harder puzzles.
m
I usually don’t bother with enums or constants at all until there is a lot more complexity.
e
I did ADT in Haskell (because it's easy) and in Rust (because it's easier than error handling later on), but stuck with direct string-handling in Kotlin and Python today because of the relative ease
m
it depends on the complexity of the input indeed. I'd rather have a class with proper fields than a
Copy code
Pair<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.
p
I'd live to see both puzzles in advance, else I always have to change my first solution to dedupe the code
👍 2
m
I usually avoid using generic Pairs in favor of defining my own data classes. Destructuring not withstanding, it’s better to have a Point with x and y than a Pair with first and second.
Then simple typealiases go a long way 🙂
p
I hate typealiases and believe they should be only used to define function types
m
A matter of taste, probably. I see nothing wrong with something like
typealias Grid = Map<Point, Tile>
.
p
Yeah thats true. But often times they actually should be value classes
typealias Name = String typealias City = String That's hardcore error prone
m
^ Sure, that’s wrong. Should be value classes.
j
@Paul Woitaschek My solution for today had a lot of similarities with yours 🙂 (although it is a bit more verbose) https://github.com/JH108/advent-of-code-2021/blob/main/src/day02/Day02Logic.kt I didn’t think to use
companion object
though, that was a nice way to define the initial position and to parse the input.
❤️ 1
l
g
I used the more functional approach, @Sebastian Aigner presented in his "virtual machine" implementation (

https://www.youtube.com/watch?v=0GWTTSMatO8&amp;list=PLlFc5cFwUnmwfLRLvIM7aV7s73eSTL005&amp;index=8

). My solution: https://github.com/gnu11111/aoc-2021-in-kotlin/blob/main/src/main/kotlin/at/gnu/adventofcode/year2021/Day02.kt
🔥 2
s
Nice stuff @gnu – ended up quite similar to my own secondary implementation! 😄 https://github.com/SebastianAigner/advent-of-code-2021/blob/master/src/main/kotlin/Day02_Fold.kt
🤯 2
🙌 1
g
@Sebastian Aigner Nice! Is there a special reason, you used a "factory-pattern" with the "fromText()"-method inside a companion object this time, instead of the "constructor"-pattern you used in the "virtual machine"-video?
s
Not really. I’m just generally a fan of this pattern, but I wouldn't say it's more or less idiomatic.
t
Alright, here’s my take on it for today. Lots of ways we could have gone with this, I picked something I could explain, and hopefully introduces topics we need for future days: • BlogCode
🙌 4
m
^ nice! I played around with this today and really tried to find a concise solution. Finally settled on an imperative approach with a mutable data class. Using the run scope function removed a lot of clutter. https://github.com/MikeEnRegalia/AdventOfCode2021/blob/main/kotlin/src/main/kotlin/Day02Imp.kt
e
Nice use of Triple @gammax! K my solution looked a bit similar 🙂 https://github.com/Kantis/aoc2021/blob/main/src/main/kotlin/day02.kt
K 1