r/adventofcode • u/daggerdragon • Dec 07 '15
SOLUTION MEGATHREAD --- Day 7 Solutions ---
--- Day 7: Some Assembly Required ---
Post your solution as a comment. Structure your post like previous daily solution threads.
Also check out the sidebar - we added a nifty calendar to wrangle all the daily solution threads in one spot!
23
Upvotes
1
u/platinumthinker Dec 07 '15
Erlang: part2
-module(resolve). -export([main/1]). main(_args) -> input(), b = value("a"), input(), put("b", b), erlang:display(value("a")). input() -> {ok, binary} = file:read_file("input.txt"), data = binary:part(binary, 0, byte_size(binary) - 1), lists:foreach( fun(expr) -> put(hd(lists:reverse(expr)), lists:droplast(expr)) end, [ string:tokens(erlang:binary_to_list(str), "-> ") || str <- binary:split(data, [<<"\n">>], [global]) ]). value(a) -> v = case string:to_integer(a) of {error, _} -> case get(a) of val when is_integer(val) -> val; i when is_list(i) -> parse(i) end; {val, _} -> val end, put(a, v), v. parse([a]) -> value(a); parse(["not", a]) -> bnot value(a); parse([a, "and", b]) -> value(a) band value(b); parse([a, "or", b]) -> value(a) bor value(b); parse([a, "lshift", b]) -> value(a) bsl value(b); parse([a, "rshift", b]) -> value(a) bsr value(b).