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

  • @itslilith@lemmy.blahaj.zone
    link
    fedilink
    6
    edit-2
    1 year ago

    (Cursed) Python

    I solved the actual thing recursively in Rust, but I decided that wasn’t cursed enough, so I present: Polynomial fitting!

    import numpy.polynomial.polynomial as pol
    
    with open("input.txt") as f:
      lines = list(map(lambda l: list(map(int, l.split(" "))), f.read().split("\n")))
    
    lo, hi = 0, 0
    
    for line in lines:
      for i in range(len(line)):
        poly, (r, *_) = pol.Polynomial.fit(range(len(line)), line, full=True, deg=i)
        if r < 0.0000000001:
          break
    
      lo += int(round(poly(-1)))
      hi += int(round(poly(len(line))))
    
    print(f"Part 1: {hi}")
    print(f"Part 2: {lo}")