2015 day 7
This commit is contained in:
parent
23b74f07d5
commit
cb503f40fe
|
@ -12,6 +12,7 @@ public class Test2015
|
||||||
// [DataRow(typeof(Day04), "346386", "9958218")]
|
// [DataRow(typeof(Day04), "346386", "9958218")]
|
||||||
[DataRow(typeof(Day05), "258", "53")]
|
[DataRow(typeof(Day05), "258", "53")]
|
||||||
// [DataRow(typeof(Day06), "543903", "14687245")]
|
// [DataRow(typeof(Day06), "543903", "14687245")]
|
||||||
|
[DataRow(typeof(Day07), "3176", "14710")]
|
||||||
public void TestAllDays(Type dayType, string part1, string part2)
|
public void TestAllDays(Type dayType, string part1, string part2)
|
||||||
{
|
{
|
||||||
Common.CheckDay(dayType, part1, part2);
|
Common.CheckDay(dayType, part1, part2);
|
||||||
|
@ -24,6 +25,7 @@ public class Test2015
|
||||||
// [DataRow(typeof(Day04), "609043", "6742839")]
|
// [DataRow(typeof(Day04), "609043", "6742839")]
|
||||||
[DataRow(typeof(Day05), "1", "1")]
|
[DataRow(typeof(Day05), "1", "1")]
|
||||||
[DataRow(typeof(Day06), "1000000", "1000000")]
|
[DataRow(typeof(Day06), "1000000", "1000000")]
|
||||||
|
// [DataRow(typeof(Day07), "", "")] // test input doesn't have "a" key
|
||||||
public void CheckTestInputs(Type dayType, string part1, string part2)
|
public void CheckTestInputs(Type dayType, string part1, string part2)
|
||||||
{
|
{
|
||||||
Day.UseTestInput = true;
|
Day.UseTestInput = true;
|
||||||
|
|
112
AOC2015/Day07.cs
112
AOC2015/Day07.cs
|
@ -5,11 +5,115 @@
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Day07 : Day
|
public sealed class Day07 : Day
|
||||||
{
|
{
|
||||||
public Day07() : base(2015, 7, "Puzzle Name")
|
private readonly Dictionary<string, ushort> _wires = new();
|
||||||
|
private readonly Dictionary<string, Func<ushort>> _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"]();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
Loading…
Reference in New Issue