Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 20, 2024
1 parent 35cb310 commit bc3a8e8
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/lang/solve/solve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function solve(
problem: Solution,
options: { limit: number },
): Array<Solution> {
const problems = [problem]
const partialSolutions = [problem]
const solutions: Array<Solution> = []
const limit = options.limit || Infinity
while (true) {
Expand All @@ -87,28 +87,28 @@ export function solve(
return solutions.slice(0, limit)
}

// NOTE Working on the first `problem`.
const problem = problems.shift()
if (problem === undefined) {
// NOTE Working on the first `partialSolution`.
const partialSolution = partialSolutions.shift()
if (partialSolution === undefined) {
return solutions
}

if (problem.goals.length === 0) {
solutions.push(problem)
if (partialSolution.goals.length === 0) {
solutions.push(partialSolution)
continue
}

const [goal, ...restGoals] = problem.goals
const newSolution = solutionUpdate(problem, { goals: restGoals })
const newProblems = pursue(newSolution, goal)
const [goal, ...restGoals] = partialSolution.goals
const newSolution = solutionUpdate(partialSolution, { goals: restGoals })
const newPartialSolutions = pursue(newSolution, goal)

// NOTE We try to be fair by pushing
// the newly generated partial solutions to the end.
for (const solution of newProblems) {
for (const solution of newPartialSolutions) {
if (solution.goals.length === 0) {
solutions.push(solution)
} else {
problems.push(solution)
partialSolutions.push(solution)
}
}
}
Expand Down

0 comments on commit bc3a8e8

Please sign in to comment.