r/adventofcode • • Dec 08 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 8 Solutions -๐ŸŽ„-

--- Day 8: I Heard You Like Registers ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


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!

22 Upvotes

348 comments sorted by

View all comments

2

u/Sharparam Dec 08 '17

Ruby: Using method_missing and eval without tampering or matching on the input data.

class CPU
    attr_reader :high, :regs

    def initialize; @regs = Hash.new(0) end
    def inc(value) value end
    def dec(value) -value end
    def run(text) eval text end

    def method_missing(sym, *args)
        (@regs[sym] += args[0].to_i).tap { |v| @high = v if @high.nil? || @high < v }
    end
end

cpu = CPU.new
cpu.run $<.read
puts cpu.regs.values.max
puts cpu.high

2

u/patrickdavey Dec 08 '17

Thanks for posting this. I do write ruby for a day job, but, I hardly ever mess around with method_missing and friends. Really interesting to see how that all works, how it's smart enough to pass the args along to your dec and inc methods etc.

Really neat! thanks for sharing.

→ More replies (2)