2021-12-08 17:45:55 +00:00
|
|
|
import fs from "fs-extra";
|
|
|
|
import fp from "lodash/fp.js";
|
|
|
|
import _ from "lodash";
|
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
import * as p1 from "./part1.mjs";
|
2021-12-08 17:45:55 +00:00
|
|
|
|
|
|
|
function decodedDigits(mapping) {
|
2021-12-08 17:50:53 +00:00
|
|
|
const s = fp.invert(mapping);
|
|
|
|
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);
|
|
|
|
|
|
|
|
return solution;
|
2021-12-08 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
function guess(unknowns, guessed = {}, inputs) {
|
|
|
|
const [next] = Object.keys(unknowns);
|
|
|
|
|
|
|
|
if (!next) return guessed;
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
if (unknowns[next].size === 0) return;
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
for (const p of unknowns[next].values()) {
|
|
|
|
let newUnknowns = fp.omit(next, unknowns);
|
|
|
|
newUnknowns = fp.mapValues((s) => {
|
2021-12-08 18:21:28 +00:00
|
|
|
return s.filter( x => x !== p )
|
2021-12-08 17:50:53 +00:00
|
|
|
}, newUnknowns);
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
if (Object.values(newUnknowns).some((s) => s.size === 0)) continue;
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
const r = guess(newUnknowns, { ...guessed, [next]: p }, inputs);
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 18:21:28 +00:00
|
|
|
if (!r) continue;
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 18:21:28 +00:00
|
|
|
const solution = decodedDigits(r);
|
|
|
|
|
|
|
|
if (inputs.every((i) => solution.hasOwnProperty(i.join(""))))
|
|
|
|
return r;
|
2021-12-08 17:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findMapping(input) {
|
2021-12-08 17:50:53 +00:00
|
|
|
const values = "abcdefg".split("");
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 18:21:28 +00:00
|
|
|
let possibility = Object.fromEntries(
|
|
|
|
values.map((v) => [v, [ ...values ]])
|
2021-12-08 17:50:53 +00:00
|
|
|
);
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
for (const seq of input) {
|
|
|
|
const p = Object.values(p1.digits)
|
|
|
|
.filter((d) => d.length === seq.length)
|
|
|
|
.join("")
|
|
|
|
.split("");
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
for (const l of seq) {
|
2021-12-08 18:21:28 +00:00
|
|
|
possibility[l] = _.intersection( possibility[l], p);
|
2021-12-08 17:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
const one = input.find((i) => i.length === 2);
|
|
|
|
const seven = input.find((i) => i.length === 3);
|
|
|
|
const [a] = seven.filter((x) => !one.includes(x));
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 18:21:28 +00:00
|
|
|
possibility = fp.mapValues( fp.reject('a'), possibility);
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 18:21:28 +00:00
|
|
|
possibility[a] = ["a"];
|
2021-12-08 17:45:55 +00:00
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
return guess(possibility, {}, input);
|
2021-12-08 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
export function decodedOutput({ input, output }) {
|
|
|
|
const mapping = findMapping([...input, ...output]);
|
2021-12-08 17:45:55 +00:00
|
|
|
|
|
|
|
const digits = decodedDigits(mapping);
|
|
|
|
|
2021-12-08 17:50:53 +00:00
|
|
|
return parseInt(output.map((e) => digits[e.join("")]).join(""));
|
2021-12-08 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function solution(input) {
|
2021-12-08 17:50:53 +00:00
|
|
|
return _.sum(input.map(decodedOutput));
|
2021-12-08 17:45:55 +00:00
|
|
|
}
|