| 123456789101112131415161718192021222324252627282930313233 |
- namespace Day20;
- public class ConjunctionModule : Module
- {
- private readonly Dictionary<string, bool> _inputStates = new Dictionary<string, bool>();
-
- public ConjunctionModule(string name)
- : base(name)
- {
- }
-
- public override string GetState()
- {
- return string.Concat(_inputs.Select(m => StateOf(m) ? "1" : "0"));
- }
- public override IEnumerable<Pulse> Process(Pulse p)
- {
- _inputStates[p.Source] = p.Value;
- var outPulse = !AllHigh();
- return _outputs.Select(o => new Pulse(Name, o.Name, outPulse));
- }
- private bool StateOf(Module m)
- {
- return _inputStates.GetValueOrDefault(m.Name, false);
- }
- private bool AllHigh()
- {
- return _inputs.All(StateOf);
- }
- }
|