Skip to content

Instantly share code, notes, and snippets.

@d12
Created October 7, 2021 04:17
Show Gist options
  • Save d12/09ae24690fa45bbcf158740e5435036f to your computer and use it in GitHub Desktop.
Save d12/09ae24690fa45bbcf158740e5435036f to your computer and use it in GitHub Desktop.
input = File.read("7_input.txt").gsub("OR", "|").gsub("AND", "&").gsub("LSHIFT", "<<").gsub("RSHIFT", ">>").gsub("NOT", "~").lines.map(&:chomp)
@map = {}
input.each do |expression|
left_side, right_side = expression.split("->")
@map[right_side.strip] = left_side
end
@memo = {}
def evaluate(label)
return @memo[label] if @memo[label]
label.scan(/[a-z]+/).uniq.each do |cap|
label.gsub!(cap, evaluate(@map[cap]).to_s)
end
@memo[label] = eval(label)
end
puts evaluate(@map["a"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment