refactor
This commit is contained in:
parent
e01357f8af
commit
8a8d79f436
@ -1,4 +1,3 @@
|
|||||||
import fs from "fs-extra";
|
|
||||||
import fp from "lodash/fp.js";
|
import fp from "lodash/fp.js";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
@ -6,22 +5,17 @@ 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);
|
|
||||||
solution = fp.mapKeys(
|
return _.flow([
|
||||||
(k) =>
|
fp.invert,
|
||||||
|
fp.mapKeys((k) =>
|
||||||
k
|
k
|
||||||
.split("")
|
.split("")
|
||||||
.map((l) => s[l])
|
.map((l) => s[l])
|
||||||
.join(""),
|
.join("")
|
||||||
solution
|
),
|
||||||
);
|
fp.mapKeys((k) => k.split("").sort().join("")),
|
||||||
|
])(p1.digits);
|
||||||
solution = fp.mapKeys((k) => {
|
|
||||||
k = k.split("").sort().join("");
|
|
||||||
return k;
|
|
||||||
}, solution);
|
|
||||||
|
|
||||||
return solution;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function guess(unknowns, guessed = {}, inputs) {
|
function guess(unknowns, guessed = {}, inputs) {
|
||||||
@ -29,13 +23,11 @@ function guess(unknowns, guessed = {}, inputs) {
|
|||||||
|
|
||||||
if (!next) return guessed;
|
if (!next) return guessed;
|
||||||
|
|
||||||
if (unknowns[next].size === 0) return;
|
for (const p of unknowns[next]) {
|
||||||
|
let newUnknowns = fp.mapValues(
|
||||||
for (const p of unknowns[next].values()) {
|
(s) => s.filter((x) => x !== p),
|
||||||
let newUnknowns = fp.omit(next, unknowns);
|
fp.omit(next, unknowns)
|
||||||
newUnknowns = fp.mapValues((s) => {
|
);
|
||||||
return s.filter( x => x !== p )
|
|
||||||
}, newUnknowns);
|
|
||||||
|
|
||||||
if (Object.values(newUnknowns).some((s) => s.size === 0)) continue;
|
if (Object.values(newUnknowns).some((s) => s.size === 0)) continue;
|
||||||
|
|
||||||
@ -53,9 +45,7 @@ function guess(unknowns, guessed = {}, inputs) {
|
|||||||
export function findMapping(input) {
|
export function findMapping(input) {
|
||||||
const values = "abcdefg".split("");
|
const values = "abcdefg".split("");
|
||||||
|
|
||||||
let possibility = Object.fromEntries(
|
let possibility = Object.fromEntries(values.map((v) => [v, values]));
|
||||||
values.map((v) => [v, [ ...values ]])
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const seq of input) {
|
for (const seq of input) {
|
||||||
const p = Object.values(p1.digits)
|
const p = Object.values(p1.digits)
|
||||||
@ -64,15 +54,16 @@ export function findMapping(input) {
|
|||||||
.split("");
|
.split("");
|
||||||
|
|
||||||
for (const l of seq) {
|
for (const l of seq) {
|
||||||
possibility[l] = _.intersection( possibility[l], p);
|
possibility[l] = _.intersection(possibility[l], p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// feel cute, might delete later
|
||||||
const one = input.find((i) => i.length === 2);
|
const one = input.find((i) => i.length === 2);
|
||||||
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));
|
||||||
|
|
||||||
possibility = fp.mapValues( fp.reject('a'), possibility);
|
possibility = fp.mapValues(fp.reject("a"), possibility);
|
||||||
|
|
||||||
possibility[a] = ["a"];
|
possibility[a] = ["a"];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user