112 lines
2.9 KiB
TypeScript
112 lines
2.9 KiB
TypeScript
|
import { operators, pool } from './globals'
|
||
|
import { OperatorType } from './types'
|
||
|
import { randRange } from './utils'
|
||
|
|
||
|
type HistoryType = { a: number; b: number; op: OperatorType }[]
|
||
|
|
||
|
export function operate(operator: OperatorType, valA: number, valB: number): number {
|
||
|
switch (operator) {
|
||
|
case '+':
|
||
|
return valA + valB
|
||
|
case '-':
|
||
|
return valA - valB
|
||
|
case '*':
|
||
|
return valA * valB
|
||
|
case '/':
|
||
|
return valA / valB
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function isSolvable(result: number, plaquettes: number[]): boolean {
|
||
|
function printHistory(history: HistoryType): void {
|
||
|
for (const item of history) {
|
||
|
if (item.a < item.b) [item.a, item.b] = [item.b, item.a]
|
||
|
console.log(
|
||
|
`${item.a} ${item.op} ${item.b} = ${operate(item.op, item.a, item.b)}`,
|
||
|
)
|
||
|
}
|
||
|
console.log()
|
||
|
}
|
||
|
|
||
|
function loopOperations(plaquettes: number[], history: HistoryType): void {
|
||
|
for (let i = 0; i < plaquettes.length - 1; ++i) {
|
||
|
for (let j = i + 1; j < plaquettes.length; ++j) {
|
||
|
let a = plaquettes[i]
|
||
|
let b = plaquettes[j]
|
||
|
if (a < b) [a, b] = [b, a]
|
||
|
for (const op of operators) {
|
||
|
if (op === '/' && a % b !== 0) continue
|
||
|
recursOperation(op, a, b, plaquettes, history)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function recursOperation(
|
||
|
operator: OperatorType,
|
||
|
valA: number,
|
||
|
valB: number,
|
||
|
oldPlaquettes: number[],
|
||
|
oldHistory: HistoryType,
|
||
|
): void {
|
||
|
if (found) return
|
||
|
if (new Date().getTime() - startTime.getTime() > 250) return
|
||
|
|
||
|
const plaquettes = [...oldPlaquettes]
|
||
|
const history = [...oldHistory]
|
||
|
|
||
|
// remove values from plaquettes
|
||
|
const idxA = plaquettes.findIndex(p => p === valA)
|
||
|
plaquettes.splice(idxA, 1)
|
||
|
const idxB = plaquettes.findIndex(p => p === valB)
|
||
|
plaquettes.splice(idxB, 1)
|
||
|
|
||
|
// calculate result and push it into plaquettes
|
||
|
const r = operate(operator, valA, valB)
|
||
|
plaquettes.push(r)
|
||
|
|
||
|
// Save step
|
||
|
history.push({ a: valA, b: valB, op: operator })
|
||
|
|
||
|
// Found the solution
|
||
|
if (plaquettes.indexOf(result) > -1) {
|
||
|
histories.push(history)
|
||
|
if (!found) {
|
||
|
found = true
|
||
|
console.log('1e solution trouvée en ' + (Date.now() - start) + 'ms')
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
// Exhausted all plaquettes
|
||
|
else if (plaquettes.length === 1) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
loopOperations(plaquettes, history)
|
||
|
}
|
||
|
|
||
|
const histories: HistoryType[] = []
|
||
|
|
||
|
// Start calculations
|
||
|
const start = Date.now()
|
||
|
let found = false
|
||
|
const startTime = new Date()
|
||
|
loopOperations(plaquettes, [])
|
||
|
|
||
|
if (histories.length) {
|
||
|
printHistory(histories[0])
|
||
|
}
|
||
|
console.log(new Date().getTime() - startTime.getTime() + 'ms')
|
||
|
|
||
|
return found
|
||
|
|
||
|
// histories.sort((a, b) => a.length - b.length)
|
||
|
// console.log(
|
||
|
// `${histories.length} combinaisons trouvées en ${Date.now() - start}ms`,
|
||
|
// ) // Entre 18 et 1410
|
||
|
// if (histories.length) {
|
||
|
// console.log()
|
||
|
// printHistory(histories[0])
|
||
|
// }
|
||
|
}
|