Solve a Holy Knight's tour: Difference between revisions

→‎JS ES6: Replaced startPosn function with a more generic (and slight better typed) charColRow function
(→‎JS ES6: Replaced startPosn function with a more generic (and slight better typed) charColRow function)
Line 1,726:
const problems = [
[
" 000 " //
, " 0 00 " //
, " 0000000" //
Line 1,736:
],
[
"-----1-0-----" //
, "-----0-0-----" //
, "----00000----" //
Line 1,769:
return unit.concat.apply(unit, xs);
})() : [];
 
// startPosncharColRow :: Char -> [String] -> Maybe (Int, Int)
const startPosncharColRow = (c, rows) => {
}foldr((a, :xs, ((iRow) => {
a.nothing: true? (() => {
const iRowmbiCol = mb.justelemIndex(c, xs);
return mbmbiCol.nothing ? mbiCol : {
just: [mbiCol.just, iRow],
nothing: false
] };
})(); : a, {
nothing: true
return}, [rows);
 
// 2 or more arguments
Line 1,782 ⟶ 1,795:
// elem :: Eq a => a -> [a] -> Bool
const elem = (x, xs) => xs.indexOf(x) !== -1;
 
// elemIndex :: Eq a => a -> [a] -> Maybe Int
const elemIndex = (x, xs) => {
const mbi = findIndex(s => sxs.indexOf('1') !== -1, rowsx);
return {
nothing: i === -1,
just: i
};
};
 
// enumFromTo :: Int -> Int -> [Int]
Line 1,807 ⟶ 1,829:
// foldl :: (b -> a -> b) -> b -> [a] -> b
const foldl = (f, a, xs) => xs.reduce(f, a);
 
// foldr (a -> b -> b) -> b -> [a] -> b
const foldr = (f, a, xs) => xs.reduceRight(f, a);
 
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
Line 1,898 ⟶ 1,923:
return v;
};
 
// zip :: [a] -> [b] -> [(a,b)]
const zip = (xs, ys) =>
xs.slice(0, Math.min(xs.length, ys.length))
.map((x, i) => [x, ys[i]]);
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Line 1,932 ⟶ 1,962:
const hash = ([col, row]) => col.toString() + '.' + row.toString();
 
 
// startPosn :: [String] -> Maybe (Int, Int)
const startPosn = rows => {
const mb = findIndex(s => s.indexOf('1') !== -1, rows);
return mb.nothing ? {
nothing: true
} : (() => {
const iRow = mb.just;
return [
findIndex(s => s === '1', rows[iRow])
.just, iRow
];
})();
};
 
// Start node, and degree-sorted cache of moves from each node
Line 1,958 ⟶ 1,976:
);
return {
start: hash(startPosncharColRow('1', boardLines)),
.just),
boardWidth: boardLines.length > 0 ? boardLines[0].length : 0,
stepCount: steps.length,
9,655

edits