Created
October 7, 2021 04:17
-
-
Save d12/09ae24690fa45bbcf158740e5435036f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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