r/adventofcode • • Dec 20 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-

--- Day 20: A Regular Map ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 20

Transcript:

My compiler crashed while running today's puzzle because it ran out of ___.


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

edit: Leaderboard capped, thread unlocked at 00:59:30!

17 Upvotes

153 comments sorted by

View all comments

1

u/jtgorn Dec 21 '18

Main part of my solution which works much more generally than solutions using simple stack. I keep all posible positions after previous character and move from all of them simultaneously

def move(ch,positions)
  positions.map do |pos|
    old_pos = pos.clone
    case ch
      when 'N' then pos[0] += -1
      when 'S' then pos[0] += +1
      when 'E' then pos[1] += +1
      when 'W' then pos[1] += -1
    end
    add_door(old_pos,pos)
    pos
  end
end

def solve(input, positions)
  positions = positions.deep_clone
  initial_positions = positions.deep_clone
  end_positions = []
  loop do
    case ch = input.shift
      when 'N','S','W','E'
        positions = move(ch,positions)
      when '('
        positions = solve(input, positions)
        positions.uniq!
      when '|',')', nil
        end_positions += positions.deep_clone
        return end_positions if ch==')' or ch.nil? 
        positions = initial_positions.deep_clone
      else raise
    end
    puts "After #{ch} I have #{positions.count} positions"
  end
end

p solve(input.to_a, [[0,0]])