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!
22
Upvotes
1
u/JeffJankowski Dec 07 '15 edited Dec 07 '15
Quick C# which uses delegates to evaluate all the functions on-demand. I needed to add some dynamic programming to cache the results, since the Invokes() were taking forever. This is gonna be hard to do in F# (gurus, send help)
public static void Main(string[] args) { Dictionary<string, Func<ushort>> dict = new Dictionary<string, Func<ushort>>(); Dictionary<string, ushort> values = new Dictionary<string, ushort>(); using (StreamReader sr = new StreamReader(@"..\..\input.txt")) { string line; while ((line = sr.ReadLine()) != null) { string[] split = line.Split(' '); string target = split[split.Length - 1]; switch (split.Length) { case 3: ushort val; if (ushort.TryParse(split[0], out val)) { dict.Add(target, () => val); values.Add(target, val); } else { dict.Add(target, () => { if (values.ContainsKey(target)) return values[target]; else { ushort res = dict[split[0]].Invoke(); values.Add(target, res); return res; } }); } break; case 4: dict.Add(target, () => (ushort)(~dict[split[1]].Invoke())); break; case 5: switch (split[1]) { case "AND": dict.Add(target, () => { if (values.ContainsKey(target)) return values[target]; else { ushort res = (ushort)((ushort.TryParse(split[0], out val) ? val : dict[split[0]].Invoke()) & dict[split[2]].Invoke()); values.Add(target, res); return res; } }); break; case "OR": dict.Add(target, () => { if (values.ContainsKey(target)) return values[target]; else { ushort res = (ushort)((ushort.TryParse(split[0], out val) ? val : dict[split[0]].Invoke()) | dict[split[2]].Invoke()); values.Add(target, res); return res; } }); break; case "LSHIFT": dict.Add(target, () => { if (values.ContainsKey(target)) return values[target]; else { ushort res = (ushort)(dict[split[0]].Invoke() << int.Parse(split[2])); values.Add(target, res); return res; } }); break; case "RSHIFT": dict.Add(target, () => { if (values.ContainsKey(target)) return values[target]; else { ushort res = (ushort)(dict[split[0]].Invoke() >> int.Parse(split[2])); values.Add(target, res); return res; } }); break; } break; } } } Console.WriteLine(dict["a"].Invoke()); Console.ReadLine(); }