Day 9: Mirage Maintenance

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 5 mins

  • cvttsd2si@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    11 months ago

    Scala3

    def diffs(a: Seq[Long]): List[Long] =
        a.drop(1).zip(a).map(_ - _).toList
    
    def predictNext(a: Seq[Long], combine: (Seq[Long], Long) => Long): Long =
        if a.forall(_ == 0) then 0 else combine(a, predictNext(diffs(a), combine))
    
    def predictAllNexts(a: List[String], combine: (Seq[Long], Long) => Long): Long = 
        a.map(l => predictNext(l.split(raw"\s+").map(_.toLong), combine)).sum
    
    def task1(a: List[String]): Long = predictAllNexts(a, _.last + _)
    def task2(a: List[String]): Long = predictAllNexts(a, _.head - _)