refactor
This commit is contained in:
parent
0da07275d0
commit
fe5a92fb37
@ -2,105 +2,84 @@ import fs from "fs-extra";
|
|||||||
import fp from "lodash/fp.js";
|
import fp from "lodash/fp.js";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
import * as p1 from './part1.mjs';
|
import * as p1 from "./part1.mjs";
|
||||||
|
|
||||||
function decodedDigits(mapping) {
|
function decodedDigits(mapping) {
|
||||||
const s = fp.invert(mapping);
|
const s = fp.invert(mapping);
|
||||||
let solution = fp.invert(p1.digits);
|
let solution = fp.invert(p1.digits);
|
||||||
solution = fp.mapKeys(
|
solution = fp.mapKeys(
|
||||||
k => k.split('').map(
|
(k) =>
|
||||||
l => s[l]
|
k
|
||||||
).join('')
|
.split("")
|
||||||
,solution );
|
.map((l) => s[l])
|
||||||
|
.join(""),
|
||||||
|
solution
|
||||||
|
);
|
||||||
|
|
||||||
solution = fp.mapKeys( k => { k = k.split('').sort().join(''); return k }, solution );
|
solution = fp.mapKeys((k) => {
|
||||||
|
k = k.split("").sort().join("");
|
||||||
|
return k;
|
||||||
|
}, solution);
|
||||||
|
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
|
|
||||||
function guess(unknowns, guessed = {}, inputs) {
|
function guess(unknowns, guessed = {}, inputs) {
|
||||||
console.log({unknowns,guessed});
|
|
||||||
|
|
||||||
const [next] = Object.keys(unknowns);
|
const [next] = Object.keys(unknowns);
|
||||||
|
|
||||||
if (!next) return guessed;
|
if (!next) return guessed;
|
||||||
|
|
||||||
|
|
||||||
if (unknowns[next].size === 0) return;
|
if (unknowns[next].size === 0) return;
|
||||||
|
|
||||||
|
|
||||||
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(
|
newUnknowns = fp.mapValues((s) => {
|
||||||
s => {
|
|
||||||
const x = new Set(s.values());
|
const x = new Set(s.values());
|
||||||
x.delete(p);
|
x.delete(p);
|
||||||
return x;
|
return x;
|
||||||
}, newUnknowns);
|
}, newUnknowns);
|
||||||
|
|
||||||
//console.log({ 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) {
|
||||||
const s = fp.invert(r);
|
const solution = decodedDigits(r);
|
||||||
let solution = fp.invert(p1.digits);
|
|
||||||
solution = fp.mapKeys(
|
|
||||||
k => k.split('').map(
|
|
||||||
l => s[l]
|
|
||||||
).join('')
|
|
||||||
,solution );
|
|
||||||
|
|
||||||
solution = fp.mapKeys( k => { k = k.split('').sort().join(''); return k }, solution );
|
if (inputs.every((i) => solution.hasOwnProperty(i.join("")))) {
|
||||||
|
|
||||||
console.log({r, solution });
|
|
||||||
console.log({inputs})
|
|
||||||
|
|
||||||
if( inputs.some( i => ! solution.hasOwnProperty(i.join('')) ) ) {
|
|
||||||
console.log("Oh noes");
|
|
||||||
} else {
|
|
||||||
console.log("Oh yea");
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findMapping(input) {
|
export function findMapping(input) {
|
||||||
const values = 'abcdefg'.split('');
|
const values = "abcdefg".split("");
|
||||||
|
|
||||||
const possibility = Object.fromEntries(values.map(
|
const possibility = Object.fromEntries(
|
||||||
v => [ v, new Set(values) ]
|
values.map((v) => [v, new Set(values)])
|
||||||
));
|
);
|
||||||
|
|
||||||
for (const seq of input) {
|
for (const seq of input) {
|
||||||
const p = Object.values(p1.digits).filter(
|
const p = Object.values(p1.digits)
|
||||||
d => d.length === seq.length
|
.filter((d) => d.length === seq.length)
|
||||||
).join('').split('');
|
.join("")
|
||||||
|
.split("");
|
||||||
|
|
||||||
for (const l of seq) {
|
for (const l of seq) {
|
||||||
possibility[l] = new Set(
|
possibility[l] = new Set(p.filter((x) => possibility[l].has(x)));
|
||||||
p.filter( x => possibility[l].has(x) )
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// console.log(possibility);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const one = input.find((i) => i.length === 2);
|
||||||
|
const seven = input.find((i) => i.length === 3);
|
||||||
const one = input.find( i => i.length === 2 );
|
const [a] = seven.filter((x) => !one.includes(x));
|
||||||
const seven = input.find( i => i.length === 3 );
|
|
||||||
const [a] = seven.filter( x => !one.includes(x) );
|
|
||||||
|
|
||||||
for (const l of Object.values(possibility)) {
|
for (const l of Object.values(possibility)) {
|
||||||
l.delete('a');
|
l.delete("a");
|
||||||
}
|
}
|
||||||
|
|
||||||
possibility[a] = new Set(['a']);
|
possibility[a] = new Set(["a"]);
|
||||||
console.log(possibility);
|
|
||||||
|
|
||||||
return guess(possibility, {}, input);
|
return guess(possibility, {}, input);
|
||||||
}
|
}
|
||||||
@ -110,13 +89,9 @@ export function decodedOutput({input,output}) {
|
|||||||
|
|
||||||
const digits = decodedDigits(mapping);
|
const digits = decodedDigits(mapping);
|
||||||
|
|
||||||
console.log({ digits, output });
|
return parseInt(output.map((e) => digits[e.join("")]).join(""));
|
||||||
|
|
||||||
return parseInt(output.map( e => digits[e.join('')] ).join(''));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function solution(input) {
|
export function solution(input) {
|
||||||
|
|
||||||
return _.sum(input.map(decodedOutput));
|
return _.sum(input.map(decodedOutput));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ const input = fs.readFile("input", "utf8").then(p1.processInput);
|
|||||||
|
|
||||||
tap.test("part1", async (t) => {
|
tap.test("part1", async (t) => {
|
||||||
t.equal(p1.solution(await sample), 26);
|
t.equal(p1.solution(await sample), 26);
|
||||||
t.equal(p1.solution(await input), 26);
|
t.equal(p1.solution(await input), 375);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test("part2", async (t) => {
|
tap.test("part2", async (t) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user