this post was submitted on 02 Dec 2024
15 points (100.0% liked)

programming

255 readers
1 users here now

  1. Post about programming, interesting repos, learning to program, etc. Let's try to keep free software posts in the c/libre comm unless the post is about the programming/is to the repo.

  2. Do not doxx yourself by posting a repo that is yours and in any way leads to your personally identifying information. Use reports if necessary to alert mods to a potential doxxing.

  3. Be kind, keep struggle sessions focused on the topic of programming.

founded 2 years ago
MODERATORS
15
Advent of Code 2024 Day 2 (adventofcode.com)
submitted 7 months ago* (last edited 7 months ago) by zongor@hexbear.net to c/programming@hexbear.net
 

I did not have any time today to work on AoC. I spent today in meetings or working on a testing automation in Emacs Common Lisp.

Hopefully the rest of you had time to validate the safe reports from the nuclear fusion/fission plant.

@Enjoyer_of_Games@hexbear.net @Speaker@hexbear.net @gay_king_prince_charles@hexbear.net

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Zorothamya@hexbear.net 2 points 7 months ago* (last edited 7 months ago) (1 children)

This is my kotlin script for part 2

import java.io.File
import kotlin.math.abs

fun safeP(increasing: Boolean, pair: Pair<Int, Int>): Boolean =
    (abs(pair.second - pair.first) in 1..3) && (increasing == pair.second - pair.first > 0)

val lines: List<String> = File("day02-input").readLines().map { it.trim() }.filterNot { it == "" }
val sequences: List<List<Int>> = lines.map { it.split(" ").map { it.toInt() } }

fun getProgressions(seq: List<Int>) =
    generateSequence(-1) { if (it + 1 != seq.size) it + 1 else null }
        .map { progVariation: Int ->
            seq.filterIndexed { index, _ -> index != progVariation }
                .let { alternateSeq: List<Int> ->
                    alternateSeq.mapIndexed { i: Int, level: Int ->
                        if (i != alternateSeq.lastIndex) level to alternateSeq[i + 1] else null
                    }
                }
                .filterNotNull()
        }

val answer = sequences.count { sequence: List<Int> ->
    getProgressions(sequence).any { prog: List<Pair<Int, Int>> ->
        prog.all { safeP(true, it) } || prog.all { safeP(false, it) }
    }
}

println(answer)

I'm still only learning kotlin. In my opinion it's pretty shitty code due to the long call chains. I imagine to make it better I should probably break up those chains, assigining the intermediate values to different variables, to make it more readable. Do you agree and is there other stuff that you think ought to be improved?

[โ€“] zongor@hexbear.net 1 points 7 months ago

Prefix with I don't know much about Kotlin except that its a JVM language. It looks like you are programming in a functional style more than a OOP or imperative style. So in an imperative style you would have more intermediate values, but in a function style you are trying to reduce unnecessary intermediate values and have the output of one function pipe into the next function in the chain.