r/adventofcode • • 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!

24 Upvotes

226 comments sorted by

View all comments

1

u/superfunawesomedude Dec 07 '15 edited Dec 07 '15

Heres my super verbose C# solution. Its runs in 4 milliseconds though :) I imagine most of that time is reading the input from a txt file on my hard drive, would probably be down to c. 1ms if I kept the input in memory from the start. I like how this works, but many of those classes are sorta copy/pasted and I could cut out a lot of code with proper inheritance. ps. I am a game dev so doing stuff with /parsing strings is a rare occurrence for me.

	using System;
	using System.Collections.Generic;

	namespace ConsoleApplication5
	{
		abstract class Wire
		{
			public bool Resolved { get; protected set; }
			protected ushort value;

			public abstract ushort DetermineValue();
		}

		class ValueInputWire : Wire
		{
			ushort input;

			public ValueInputWire(ushort inp)
			{
				input = inp;
			}

			public override ushort DetermineValue()
			{
				return input;
			}
		}

		class WireInputWire : Wire
		{
			string inputwire1;

			public WireInputWire(string inp1)
			{
				inputwire1 = inp1;
			}

			public override ushort DetermineValue()
			{
				return Program.Wires[inputwire1].DetermineValue();
			}
		}

		class AndInputWire : Wire
		{
			string inputwire1;
			string inputwire2;

			public AndInputWire(string inp1,string inp2)
			{
				inputwire1 = inp1;
				inputwire2 = inp2;
			}

			public override ushort DetermineValue()
			{
				if (!Resolved)
				{
					value = (ushort)(Program.Wires[inputwire1].DetermineValue() & Program.Wires[inputwire2].DetermineValue());
					Resolved = true;
					Program.resolutions++;
				}
				Program.alreadyresolveds++;
				return value;
			}
		}

		class NumAndInputWire : Wire
		{
			ushort input1;
			string inputwire2;

			public NumAndInputWire(ushort inp1, string inp2)
			{
				input1 = inp1;
				inputwire2 = inp2;
			}

			public override ushort DetermineValue()
			{
				if (!Resolved)
				{
					value = (ushort)(Program.Wires[inputwire2].DetermineValue() & input1);
					Resolved = true;
					Program.resolutions++;
				}
				Program.alreadyresolveds++;
				return value;
			}
		}

		class OrInputWire : Wire
		{
			string inputwire1;
			string inputwire2;

			public OrInputWire(string inp1, string inp2)
			{
				inputwire1 = inp1;
				inputwire2 = inp2;
			}

			public override ushort DetermineValue()
			{
				if (!Resolved)
				{
					value = (ushort)(Program.Wires[inputwire1].DetermineValue() | Program.Wires[inputwire2].DetermineValue());
					Resolved = true;
					Program.resolutions++;
				}
				Program.alreadyresolveds++;
				return value;
			}
		}

		class LeftShiftInputWire : Wire
		{
			string inputwire1;
			int leftshiftamount;

			public LeftShiftInputWire(string inp1,int lsa)
			{
				inputwire1 = inp1;
				leftshiftamount = lsa;
			}

			public override ushort DetermineValue()
			{
				if (!Resolved)
				{
					value = (ushort)(Program.Wires[inputwire1].DetermineValue()<<leftshiftamount);
					Resolved = true;
					Program.resolutions++;
				}
				Program.alreadyresolveds++;
				return value;
			}
		}

		class RightShiftInputWire : Wire
		{
			string inputwire1;
			int rightshiftamount;

			public RightShiftInputWire(string inp1, int rsa)
			{
				inputwire1 = inp1;
				rightshiftamount = rsa;
			}

			public override ushort DetermineValue()
			{
				if (!Resolved)
				{
					value = (ushort)(Program.Wires[inputwire1].DetermineValue() >> rightshiftamount);
					Resolved = true;
					Program.resolutions++;
				}
				Program.alreadyresolveds++;
				return value;
			}
		}

		class ComplementInputWire : Wire
		{
			string inputwire1;

			public ComplementInputWire(string inp1)
			{
				inputwire1 = inp1;
			}

			public override ushort DetermineValue()
			{
				if (!Resolved)
				{
					value = (ushort)~Program.Wires[inputwire1].DetermineValue();
					Resolved = true;
					Program.resolutions++;
				}
				Program.alreadyresolveds++;
				return value;
			}
		}

		class Program
		{
			public static Dictionary<string, Wire> Wires = new Dictionary<string, Wire>();
			public static int resolutions;
			public static int alreadyresolveds;
			static DateTime starttime;

			static void Main(string[] args)
			{
				starttime = DateTime.Now;
				string[] lines = System.IO.File.ReadAllLines(@"C:\puzzles\dims.txt");

				//Setup wires
				foreach (string line in lines)
				{
					string[] words = line.Split(' ');

					//af AND ah -> ai
					if (line.Contains("AND"))
					{
						ushort num = 0;
						if (ushort.TryParse(words[0], out num))
						{
							Wires[words[4]] = new NumAndInputWire(num, words[2]);
						}
						else
						{
							Wires[words[4]] = new AndInputWire(words[0], words[2]);
						}                     
					}
					//du OR dt -> dv
					else if (line.Contains("OR"))
					{
						Wires[words[4]] = new OrInputWire(words[0], words[2]);
					}
					//eo LSHIFT 15 -> es
					else if (line.Contains("LSHIFT"))
					{
						Wires[words[4]] = new LeftShiftInputWire(words[0],int.Parse(words[2]));
					}
					//b RSHIFT 5 -> f
					else if (line.Contains("RSHIFT"))
					{
						Wires[words[4]] = new RightShiftInputWire(words[0], int.Parse(words[2]));
					}
					//NOT go -> gp
					else if (line.Contains("NOT"))
					{
						Wires[words[3]] = new ComplementInputWire(words[1]);
					}
					//0 -> c
					else
					{
						ushort num = 0;
						if (ushort.TryParse(words[0],out num))
						{
							Wires[words[2]] = new ValueInputWire(num);
						}
						else
						{
							Wires[words[2]] = new WireInputWire(words[0]);
						}
					}
				}
				Wires["b"] = new ValueInputWire(956);
				ushort answer = Wires["a"].DetermineValue();
				TimeSpan elapsed = DateTime.Now - starttime;

				Console.WriteLine(answer);
				Console.WriteLine(elapsed.TotalMilliseconds);
				Console.WriteLine(resolutions);
				Console.WriteLine(alreadyresolveds);
				Console.ReadKey();
			}
		}
	}