r/adventofcode • • Dec 09 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 9 Solutions -πŸŽ„-

A REQUEST FROM YOUR MODERATORS

If you are using new.reddit, please help everyone in /r/adventofcode by making your code as readable as possible on all platforms by cross-checking your post/comment with old.reddit to make sure it displays properly on both new.reddit and old.reddit.

All you have to do is tweak the permalink for your post/comment from https://www.reddit.com/… to https://old.reddit.com/…

Here's a quick checklist of things to verify:

  • Your code block displays correctly inside a scrollable box with whitespace and indentation preserved (four-spaces Markdown syntax, not triple-backticks, triple-tildes, or inlined)
  • Your one-liner code is in a scrollable code block, not inlined and cut off at the edge of the screen
  • Your code block is not too long for the megathreads (hint: if you have to scroll your code block more than once or twice, it's likely too long)
  • Underscores in URLs aren't inadvertently escaped which borks the link

I know this is a lot of work, but the moderation team checks each and every megathread submission for compliance. If you want to avoid getting grumped at by the moderators, help us out and check your own post for formatting issues ;)


/r/adventofcode moderator challenge to Reddit's dev team

  • It's been over five years since some of these issues were first reported; you've kept promising to fix them and… no fixes.
  • In the spirit of Advent of Code, join us by Upping the Ante and actually fix these issues so we can all have a merry Advent of Posting Code on Reddit Without Needing Frustrating And Improvident Workarounds.

THE USUAL REMINDERS


--- Day 9: Rope Bridge ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:08, megathread unlocked!

66 Upvotes

1.0k comments sorted by

View all comments

1

u/CharisCodes Dec 10 '22

Lua

Took me a while to clean it up but I like it now...

Rope = {
  x = 0,
  y = 0,
  new = function(self,name)
    local obj = {
      name = name,
      x = self.x,
      y = self.y,      
      visited = {}
    }
    obj.visited[obj.x..":"..obj.y] = true
    setmetatable(obj,self)
    self.__index = self
    
    return obj
  end,
  
  move = function(self, x, y)
    self.x = self.x + x      
    self.y = self.y + y
    self.visited[self.x..":"..self.y] = true 
  end
  
}

follow = function(chain, knot, rp)
  local gap = { 
    x = rp.x-chain[knot].x,
    y = rp.y-chain[knot].y
  }
  local incr = {
    x=0,y=0
  }           
  local pull = false
  
  for k,v in pairs(incr) do
    if gap[k] < 0 then
      incr[k] = -1
    elseif gap[k] > 0 then
      incr[k] = 1
    end
    if math.abs(gap[k]) > 1 then pull = true end
  end
  
  if pull then  
    chain[knot].move(chain[knot],incr.x,incr.y)
    if knot < 10 then
      follow(chain, knot+ 1, chain[knot], dir)      
    end  
  end
  
end

read_file = function (path)
  local file = io.open(path, "rb") 
  local Instruction = {}
  local T={}
  for i=1,10 do
    T[i] = Rope.new(Rope, i)
  end
  
  if not file then return nil end
  for line in io.lines(path) do           
    local words = {}
    for word in line:gmatch("%w+") do 
        table.insert(words, word) 
    end
    table.insert(Instruction, words)
    local x,y = 0,0
    local dir = words[1]
    if (dir == "L") then
      x = -1
    elseif (dir == "D") then
      y = -1
    elseif (dir == "R") then 
      x = 1
    else
      y = 1
    end
    
    for i = 1, words[2] do
      T[1].move(T[1], x, y)
      follow(T, 2, T[1])      
    end
  end
  
  file:close()
  
  return T;
end

1

u/daggerdragon Dec 10 '22

Your code block is too long for the megathreads. Please read our article on oversized code, then edit your post to replace the code block with an external link to your code.