ConjunctionModule.cs 804 B

123456789101112131415161718192021222324252627282930313233
  1. namespace Day20;
  2. public class ConjunctionModule : Module
  3. {
  4. private readonly Dictionary<string, bool> _inputStates = new Dictionary<string, bool>();
  5. public ConjunctionModule(string name)
  6. : base(name)
  7. {
  8. }
  9. public override string GetState()
  10. {
  11. return string.Concat(_inputs.Select(m => StateOf(m) ? "1" : "0"));
  12. }
  13. public override IEnumerable<Pulse> Process(Pulse p)
  14. {
  15. _inputStates[p.Source] = p.Value;
  16. var outPulse = !AllHigh();
  17. return _outputs.Select(o => new Pulse(Name, o.Name, outPulse));
  18. }
  19. private bool StateOf(Module m)
  20. {
  21. return _inputStates.GetValueOrDefault(m.Name, false);
  22. }
  23. private bool AllHigh()
  24. {
  25. return _inputs.All(StateOf);
  26. }
  27. }