From cb503f40fe80c41ffe3ebb97285adea8d7b19c10 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sun, 13 Nov 2022 12:56:18 -0500 Subject: [PATCH] 2015 day 7 --- AOC.Test/Test2015.cs | 2 + AOC2015/Day07.cs | 112 ++++++++++++++++++++++++++++++++++-- AOC2015/input2015/test07.in | 8 +++ 3 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 AOC2015/input2015/test07.in diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index c5a5f28..e6f6fab 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -12,6 +12,7 @@ public class Test2015 // [DataRow(typeof(Day04), "346386", "9958218")] [DataRow(typeof(Day05), "258", "53")] // [DataRow(typeof(Day06), "543903", "14687245")] + [DataRow(typeof(Day07), "3176", "14710")] public void TestAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -24,6 +25,7 @@ public class Test2015 // [DataRow(typeof(Day04), "609043", "6742839")] [DataRow(typeof(Day05), "1", "1")] [DataRow(typeof(Day06), "1000000", "1000000")] + // [DataRow(typeof(Day07), "", "")] // test input doesn't have "a" key public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/AOC2015/Day07.cs b/AOC2015/Day07.cs index e2bf1f2..bcf8e45 100644 --- a/AOC2015/Day07.cs +++ b/AOC2015/Day07.cs @@ -5,11 +5,115 @@ /// public sealed class Day07 : Day { - public Day07() : base(2015, 7, "Puzzle Name") + private readonly Dictionary _wires = new(); + private readonly Dictionary> _actions = new(); + + public Day07() : base(2015, 7, "Some Assembly Required") { } - public override object Part1() => ""; + public override object Part1() + { + ProcessInstructions(); + return _actions["a"](); + } - public override object Part2() => ""; -} + private void ProcessInstructions() + { + _actions.Clear(); + _wires.Clear(); + + foreach (var line in Input) + { + var split = line.Split(' '); + var destination = split.Last(); + + switch (split.Length) + { + case 3: + if (ushort.TryParse(split[0], out var val)) + { + _actions.Add(destination, () => val); + _wires.Add(destination, val); + } + else + { + _actions.Add(destination, () => + { + if (_wires.ContainsKey(destination)) return _wires[destination]; + + var res = _actions[split[0]](); + _wires.Add(destination, res); + return res; + }); + } + + break; + case 4: + _actions.Add(destination, () => (ushort)~_actions[split[1]]()); + break; + case 5: + switch (split[1]) + { + case "AND": + _actions.Add(destination, () => + { + if (_wires.ContainsKey(destination)) return _wires[destination]; + + var res = (ushort)((ushort.TryParse(split[0], out var v) + ? v + : _actions[split[0]]()) & _actions[split[2]]()); + _wires.Add(destination, res); + return res; + }); + break; + case "OR": + _actions.Add(destination, () => + { + if (_wires.ContainsKey(destination)) return _wires[destination]; + + var res = (ushort)((ushort.TryParse(split[0], out var v) + ? v + : _actions[split[0]]()) | _actions[split[2]]()); + _wires.Add(destination, res); + return res; + }); + break; + case "LSHIFT": + _actions.Add(destination, () => + { + if (_wires.ContainsKey(destination)) return _wires[destination]; + + var res = (ushort)(_actions[split[0]]() << ushort.Parse(split[2])); + _wires.Add(destination, res); + return res; + }); + break; + case "RSHIFT": + _actions.Add(destination, () => + { + if (_wires.ContainsKey(destination)) return _wires[destination]; + + var res = (ushort)(_actions[split[0]]() >> ushort.Parse(split[2])); + _wires.Add(destination, res); + return res; + }); + break; + } + + break; + } + } + } + + public override object Part2() + { + ProcessInstructions(); + var p1 = _actions["a"](); + + ProcessInstructions(); + + _actions["b"] = () => p1; + return _actions["a"](); + } +} \ No newline at end of file diff --git a/AOC2015/input2015/test07.in b/AOC2015/input2015/test07.in new file mode 100644 index 0000000..27b4f8d --- /dev/null +++ b/AOC2015/input2015/test07.in @@ -0,0 +1,8 @@ +123 -> x +456 -> y +x AND y -> d +x OR y -> e +x LSHIFT 2 -> f +y RSHIFT 2 -> g +NOT x -> h +NOT y -> i