首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何编写javascript算法求解逻辑布尔和/或/或电路图

如何编写javascript算法求解逻辑布尔和/或/或电路图
EN

Stack Overflow用户
提问于 2022-12-02 18:45:46
回答 2查看 41关注 0票数 -2

我在玩下面的电路图游戏。

3个输入可以打开。

游戏的解决方案是打开输入3、4和8。

这里是游戏规则:

这条线路的三根电线都被扯断了。

你必须选择把它们插在哪里才能让激光再次工作。

该集成电路具有AND、OR和XOR逻辑连接器。

另外:左边的两根电线必须有动力才能给右电线供电。

或者:在左边的两根电线中至少有一条必须供电,才能给右线供电。

异或:在左边的两根电线中,只有一条必须有动力才能给右线供电。

我想写一个JavaScript算法来解决它,但我不知道如何写它.

我试过强迫使用排列,但没有成功地编写逻辑条件测试。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-12-02 19:31:59

我没有写逻辑条件测试

标记每一个线网,并使用变量。目前还不清楚如何处理输入的输出回送,您可以假设二极管或隐式OR条件。

代码语言:javascript
运行
复制
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个布尔人的数组来野蛮地执行此操作。

票数 3
EN

Stack Overflow用户

发布于 2022-12-02 19:27:06

我想要做到这一点并不容易

以下是我的尝试:

代码语言:javascript
运行
复制
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;
}

游乐场:

代码语言:javascript
运行
复制
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
}
代码语言:javascript
运行
复制
<div id="wrapper"></div>
<p>result: <span id="result"></span></p>

自动求解器:

代码语言:javascript
运行
复制
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')
  }
}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74660240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档