submitted 2 years ago* (last edited 2 years ago) by [M] to c/advent_of_code@programming.dev
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 6 points 2 years ago* (last edited 2 years ago)

Nim

Part 1:
The extrapolated value to the right is just the sum of all last values in the diff pyramid. 45 + 15 + 6 + 2 + 0 = 68
Part 2:
The extrapolated value to the left is just a right-folded difference (right-associated subtraction) between all first values in the pyramid. e.g. 10 - (3 - (0 - (2 - 0))) = 5

So, extending the pyramid is totally unneccessary.

Total runtime: 0.9 ms
Puzzle rating: Easy, but interesting 6.5/10
Full Code: day_09/solution.nim
Snippet:

proc solve(lines: seq[string]): AOCSolution[int] =
  for line in lines:
    var current = line.splitWhitespace().mapIt(it.parseInt())
    var firstValues: seq[int]

    while not current.allIt(it == 0):
      firstValues.add current[0]
      block p1:
        result.part1 += current[^1]

      var nextIter = newSeq[int](current.high)
      for i, v in current[1..^1]:
        nextIter[i] = v - current[i]
      current = nextIter

    block p2:
      result.part2 += firstValues.foldr(a-b)
  • source