Day 6: Wait for It


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

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

    Scala3

    // math.floor(i) == i if i.isWhole, but we want i-1
    def hardFloor(d: Double): Long = (math.floor(math.nextAfter(d, Double.NegativeInfinity))).toLong
    def hardCeil(d: Double): Long = (math.ceil(math.nextAfter(d, Double.PositiveInfinity))).toLong
    
    def wins(t: Long, d: Long): Long =
        val det = math.sqrt(t*t/4.0 - d)
        val high = hardFloor(t/2.0 + det)
        val low = hardCeil(t/2.0 - det)
        (low to high).size
    
    def task1(a: List[String]): Long = 
        def readLongs(s: String) = s.split(raw"\s+").drop(1).map(_.toLong)
        a match
            case List(s"Time: $time", s"Distance: $dist") => readLongs(time).zip(readLongs(dist)).map(wins).product
            case _ => 0L
    
    def task2(a: List[String]): Long =
        def readLong(s: String) = s.replaceAll(raw"\s+", "").toLong
        a match
            case List(s"Time: $time", s"Distance: $dist") => wins(readLong(time), readLong(dist))
            case _ => 0L