Solve a Holy Knight's tour: Difference between revisions
→JS ES6: Replaced startPosn function with a more generic (and slight better typed) charColRow function
m (→JS ES6) |
(→JS ES6: Replaced startPosn function with a more generic (and slight better typed) charColRow function) |
||
Line 1,726:
const problems = [
[
, " 0 00 " //
, " 0000000" //
Line 1,736:
],
[
, "-----0-0-----" //
, "----00000----" //
Line 1,769:
return unit.concat.apply(unit, xs);
})() : [];
just: [mbiCol.just, iRow],▼
nothing: false
nothing: true
// 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) => {
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 [
▲ .just, iRow
▲ ];
▲ })();
▲ };
// Start node, and degree-sorted cache of moves from each node
Line 1,958 ⟶ 1,976:
);
return {
start: hash(
.just),
boardWidth: boardLines.length > 0 ? boardLines[0].length : 0,
stepCount: steps.length,
|