我在玩下面的电路图游戏。
3个输入可以打开。
游戏的解决方案是打开输入3、4和8。

这里是游戏规则:
这条线路的三根电线都被扯断了。
你必须选择把它们插在哪里才能让激光再次工作。
该集成电路具有AND、OR和XOR逻辑连接器。
另外:左边的两根电线必须有动力才能给右电线供电。
或者:在左边的两根电线中至少有一条必须供电,才能给右线供电。
异或:在左边的两根电线中,只有一条必须有动力才能给右线供电。
我想写一个JavaScript算法来解决它,但我不知道如何写它.
我试过强迫使用排列,但没有成功地编写逻辑条件测试。
发布于 2022-12-02 19:31:59
我没有写逻辑条件测试
标记每一个线网,并使用变量。目前还不清楚如何处理输入的输出回送,您可以假设二极管或隐式OR条件。
const circuit(inputs) {
const and0 = inputs[0] && inputs[1];
const input2 = inputs[2] || and0; // implicit
const and1 = input2 && inputs[3];
const and2 = inputs[4] && inputs[5];
const input6 = inputs[6] || and2; // implicit
const or0 = input6 || inputs[7];
const or1 = and1 || and2; // or `and1 || input6`?
const xor = or0 != and2; // or `or0 != input6`?
const and3 = or1 && xor;
return and3;
}现在,您可以使用8个布尔人的数组来野蛮地执行此操作。
发布于 2022-12-02 19:27:06
我想要做到这一点并不容易
以下是我的尝试:
function test(a, b, c, d, e, f, g, h) {
const ab = a && b;
const abcd = (ab || c) && d;
const ef = e && f;
const efgh = ef || g || h;
const abcdef = abcd || ef;
const efgh2 = ef !== efgh;
const abcdefgh = abcdef && efgh2;
return abcdefgh;
}游乐场:
const result = document.querySelector('#result')
const wrapper = document.querySelector('#wrapper')
const inputs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].map(e => {
const div = document.createElement('div')
const label = document.createElement('label')
label.textContent = e
label.setAttribute('for', e)
div.appendChild(label)
const input = document.createElement('input')
input.setAttribute('type', 'checkbox')
input.setAttribute('id', e)
div.appendChild(input)
wrapper.appendChild(div)
return input
})
update()
wrapper.addEventListener('change', update)
function update() {
const value = test(...inputs.map(e => e.checked))
result.textContent = value ? 'true' : 'false'
}
function test(a, b, c, d, e, f, g, h) {
const ab = a && b
const abcd = (ab || c) && d
const ef = e && f
const efgh = (ef || g || h)
const abcdef = abcd || ef
const efgh2 = ef !== efgh
const abcdefgh = abcdef && efgh2
return abcdefgh
}<div id="wrapper"></div>
<p>result: <span id="result"></span></p>
自动求解器:
for (const inp of getInputs()) {
const result = test(...inp)
if (result) {
const values = inp.map((e, i) => e ? i + 1 : null).filter(e => e !== null)
console.log(values)
break
}
}
function test(a, b, c, d, e, f, g, h) {
const ab = a && b
const abcd = (ab || c) && d
const ef = e && f
const efgh = (ef || g || h)
const abcdef = abcd || ef
const efgh2 = ef !== efgh
const abcdefgh = abcdef && efgh2
return abcdefgh
}
function* getInputs() {
for (let i = 0; i < 2 ** 8; i += 1) {
yield [...i.toString(2).padStart(8, '0')].map(e => e === '1')
}
}
https://stackoverflow.com/questions/74660240
复制相似问题