Circuit { public Optional<SpecificValue> signalOf(SignalCarrier signalCarrier) { if (signalCarrier instanceof SpecificValue) { return Optional.of((SpecificValue) signalCarrier); } if (cache.containsKey(signalCarrier)) { return cache.get(signalCarrier); } Optional<SignalCarrier> wire = circuit.stream() .filter(i -> i.getWire().equals(signalCarrier)) .map(Instruction::getSignalCarrier) .findFirst(); if (wire.isPresent()) { Optional<SpecificValue> resolved = wire.get().resolve(this); cache.put(signalCarrier, resolved); return resolved; } return Optional.empty(); } Circuit(String circuit); Optional<SpecificValue> signalOf(SignalCarrier signalCarrier); Map<Wire, SpecificValue> signals();  }
@Test public void should_carry_signal_on_a_wire() { Circuit circuit = new Circuit("123 -> x"); Optional<SpecificValue> x = circuit.signalOf(new Wire("x")); assertThat(x).contains(new SpecificValue(123)); }
@Test public void should_carry_signal_through_LSHIFT_gate_with_wire_by_value() { Circuit circuit = new Circuit("123 -> x\n" + "x LSHIFT 2 -> f"); Optional<SpecificValue> d = circuit.signalOf(new Wire("f")); assertThat(d).contains(new SpecificValue(492)); }
@Test public void should_not_carry_signal_through_LSHIFT_gate_with_undefined_wire() { Circuit circuit = new Circuit("x LSHIFT 2 -> f"); Optional<SpecificValue> d = circuit.signalOf(new Wire("f")); assertThat(d).isEmpty(); }
@Test public void should_carry_signal_through_RSHIFT_gate_with_wire_by_value() { Circuit circuit = new Circuit("456 -> y\n" + "y RSHIFT 2 -> g"); Optional<SpecificValue> d = circuit.signalOf(new Wire("g")); assertThat(d).contains(new SpecificValue(114)); }
@Test public void should_not_carry_signal_through_RSHIFT_gate_with_undefined_wire() { Circuit circuit = new Circuit("y RSHIFT 2 -> g"); Optional<SpecificValue> d = circuit.signalOf(new Wire("g")); assertThat(d).isEmpty(); }
@Test public void should_carry_signal_through_NOT_gate_with_wire() { Circuit circuit = new Circuit("123 -> x\n" + "NOT x -> h"); Optional<SpecificValue> d = circuit.signalOf(new Wire("h")); assertThat(d).contains(new SpecificValue(65412)); }
@Test public void should_not_carry_signal_through_NOT_gate_with_undefined_wire() { Circuit circuit = new Circuit("NOT x -> h"); Optional<SpecificValue> d = circuit.signalOf(new Wire("h")); assertThat(d).isEmpty(); }
@Test public void should_not_carry_signal_unknown_in_circuit() { Circuit circuit = new Circuit("123 -> x"); Optional<SpecificValue> d = circuit.signalOf(new Wire("y")); assertThat(d).isEmpty(); }
@Test public void should_carry_another_signal_on_a_wire() { Circuit circuit = new Circuit("45 -> y"); Optional<SpecificValue> y = circuit.signalOf(new Wire("y")); assertThat(y).contains(new SpecificValue(45)); }
@Test public void should_carry_signal_through_a_wire_with_another_wire() { Circuit circuit = new Circuit("123 -> x\n" + "x -> y"); Optional<SpecificValue> d = circuit.signalOf(new Wire("y")); assertThat(d).contains(new SpecificValue(123)); }
@Test public void should_carry_signal_through_AND_gate_with_specific_value_at_left_and_specific_value_at_right() { Circuit circuit = new Circuit("123 AND 456 -> d"); Optional<SpecificValue> d = circuit.signalOf(new Wire("d")); assertThat(d).contains(new SpecificValue(72)); }
@Test public void should_carry_signal_through_AND_gate_with_specific_value_at_left_and_wire_at_right() { Circuit circuit = new Circuit("456 -> y\n" + "123 AND y -> d"); Optional<SpecificValue> d = circuit.signalOf(new Wire("d")); assertThat(d).contains(new SpecificValue(72)); }
@Test public void should_carry_signal_through_AND_gate_with_wire_at_left_and_wire_at_right() { Circuit circuit = new Circuit("123 -> x\n" + "456 -> y\n" + "x AND y -> d"); Optional<SpecificValue> d = circuit.signalOf(new Wire("d")); assertThat(d).contains(new SpecificValue(72)); }
@Test public void should_not_carry_signal_through_AND_gate_when_wire_is_undefined() { Circuit circuit = new Circuit("x AND y -> d"); Optional<SpecificValue> d = circuit.signalOf(new Wire("d")); assertThat(d).isEmpty(); }
@Test public void should_carry_signal_through_OR_gate_with_wire_at_left_and_wire_at_right() { Circuit circuit = new Circuit("123 -> x\n" + "456 -> y\n" + "x OR y -> d"); Optional<SpecificValue> d = circuit.signalOf(new Wire("d")); assertThat(d).contains(new SpecificValue(507)); }
@Test public void should_not_carry_signal_through_OR_gate_with_undefined_wires() { Circuit circuit = new Circuit("x OR y -> d"); Optional<SpecificValue> d = circuit.signalOf(new Wire("d")); assertThat(d).isEmpty(); }