Quest 3: The Deepest Fit

  • 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)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

Also, don’t wait for me to make these posts, feel free to post yourself :)

  • janAkali@lemmy.sdf.org
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    13 hours ago

    Nim

    Reading this day’s quest I was at first a bit confused what it asks me to calculate. Took me about a minute to realize that most of descriptions are not important and actual calculations for each part are very simple:

    part 1 wants sum of each unique crate size
    part 2 is same but only 20 smallest
    part 3 is max count, because you can always put most crates in one large set and you only need 1 extra set per duplicate crate

    proc solve_part1*(input: string): Solution =
      var seen: set[int16]
      for num in input.split(','):
        let num = parseInt(num).int16
        if num in seen: continue
        else:
          seen.incl num
          result.intVal += num
    
    proc solve_part2*(input: string): Solution =
      var set = input.split(',').mapIt(parseInt(it).uint16)
      set.sort()
      result := set.deduplicate(isSorted=true)[0..<20].sum()
    
    proc solve_part3*(input: string): Solution =
      var cnt = input.split(',').toCountTable()
      result := cnt.largest.val
    

    Full solution at Codeberg: solution.nim