Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCodeDay7
{
class Program
{
static void Main(string[] args)
{
var input = new List<string>();
var sr = new StreamReader("../../input1.txt"); // select part one or two here.
string line;
while ((line = sr.ReadLine()) != null)
{
input.Add(line);
}
var wires = new List<Expr>();
foreach (var w in input)
{
var expr = new Expr();
var parts1 = w.Split(new[] { " -> " }, StringSplitOptions.None);
expr.Name = parts1[1];
var parts2 = parts1[0].Split(' ');
if (parts2.Length == 3)
{
expr.Operands.Add(new Item(parts2[0], null));
expr.Operands.Add(new Item(parts2[2], null));
expr.Operator = parts2[1];
}
else if (parts2.Length == 2)
{
expr.Operands.Add(new Item(parts2[1], null));
expr.Operator = parts2[0];
}
else if (parts2.Length == 1)
{
expr.Operands.Add(new Item(parts2[0], null));
expr.Operator = "EQUALS";
}
wires.Add(expr);
}
int cnt = 0;
while ((cnt = EvaluateAll(wires)) > 0)
{
var done = wires.Where(a => a.Value != null).ToList();
var notDone = wires.Where(a => a.Value == null);
foreach (var nd in notDone)
{
foreach (var operand in nd.Operands)
{
foreach (var d in done)
{
if (operand.SValue == d.Name)
{
operand.Value = d.Value;
}
}
}
}
Console.WriteLine(cnt);
}
Console.WriteLine("a = " + wires.FirstOrDefault(a => a.Name == "a").Value);
Console.ReadLine();
}
public static int EvaluateAll(List<Expr> wires)
{
var unsolved = wires.Where(a => a.Value == null);
foreach (var w in unsolved)
{
var c = 0;
foreach (var o in w.Operands)
{
ushort n;
if (ushort.TryParse(o.SValue, out n))
{
c++;
o.Value = n;
}
}
if (w.Operands.All(a => a.Value != null))
{
w.Value = Evaluate(w.Operator, w.Operands);
}
}
return wires.Count(a => a.Value == null);
}
public static ushort Evaluate(string oper, List<Item> operands)
{
if (operands.Count() == 2)
{
switch (oper)
{
case "AND":
return (ushort)(operands[0].Value.Value & operands[1].Value.Value);
case "OR":
return (ushort)(operands[0].Value.Value | operands[1].Value.Value);
case "LSHIFT":
return (ushort)(operands[0].Value.Value << operands[1].Value.Value);
case "RSHIFT":
return (ushort)(operands[0].Value.Value >> operands[1].Value.Value);
}
}
else if (operands.Count() == 1)
{
switch (oper)
{
case "EQUALS":
return operands[0].Value.Value;
case "NOT":
return (ushort)(~operands[0].Value.Value);
}
}
Console.WriteLine("oh noes, something's wrong!");
return 0;
}
public class Expr
{
public string Name { get; set; }
public ushort? Value { get; set; }
public List<Item> Operands { get; set; } = new List<Item>();
public string Operator { get; set; }
}
public class Item
{
public Item(string s, ushort? v)
{
SValue = s;
Value = v;
}
public string SValue { get; set; }
public ushort? Value { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.