This commit is contained in:
Yanick Champoux 2021-12-08 13:21:28 -05:00
parent fe5a92fb37
commit e01357f8af

View File

@ -34,30 +34,27 @@ function guess(unknowns, guessed = {}, inputs) {
for (const p of unknowns[next].values()) { for (const p of unknowns[next].values()) {
let newUnknowns = fp.omit(next, unknowns); let newUnknowns = fp.omit(next, unknowns);
newUnknowns = fp.mapValues((s) => { newUnknowns = fp.mapValues((s) => {
const x = new Set(s.values()); return s.filter( x => x !== p )
x.delete(p);
return x;
}, newUnknowns); }, newUnknowns);
if (Object.values(newUnknowns).some((s) => s.size === 0)) continue; if (Object.values(newUnknowns).some((s) => s.size === 0)) continue;
const r = guess(newUnknowns, { ...guessed, [next]: p }, inputs); const r = guess(newUnknowns, { ...guessed, [next]: p }, inputs);
if (r) { if (!r) continue;
const solution = decodedDigits(r);
if (inputs.every((i) => solution.hasOwnProperty(i.join("")))) { const solution = decodedDigits(r);
return r;
} if (inputs.every((i) => solution.hasOwnProperty(i.join(""))))
} return r;
} }
} }
export function findMapping(input) { export function findMapping(input) {
const values = "abcdefg".split(""); const values = "abcdefg".split("");
const possibility = Object.fromEntries( let possibility = Object.fromEntries(
values.map((v) => [v, new Set(values)]) values.map((v) => [v, [ ...values ]])
); );
for (const seq of input) { for (const seq of input) {
@ -67,7 +64,7 @@ export function findMapping(input) {
.split(""); .split("");
for (const l of seq) { for (const l of seq) {
possibility[l] = new Set(p.filter((x) => possibility[l].has(x))); possibility[l] = _.intersection( possibility[l], p);
} }
} }
@ -75,11 +72,9 @@ export function findMapping(input) {
const seven = input.find((i) => i.length === 3); const seven = input.find((i) => i.length === 3);
const [a] = seven.filter((x) => !one.includes(x)); const [a] = seven.filter((x) => !one.includes(x));
for (const l of Object.values(possibility)) { possibility = fp.mapValues( fp.reject('a'), possibility);
l.delete("a");
}
possibility[a] = new Set(["a"]); possibility[a] = ["a"];
return guess(possibility, {}, input); return guess(possibility, {}, input);
} }