|
| 1 | +import { |
| 2 | + addChar, |
| 3 | + buildFence, |
| 4 | + DIRECTIONS, |
| 5 | + getNextDirection, |
| 6 | +} from './railFenceCipher'; |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {object} params |
| 10 | + * @param {number} params.railCount |
| 11 | + * @param {number} params.strLen |
| 12 | + * @param {Array} params.string |
| 13 | + * @param {Array} params.fence |
| 14 | + * @param {number} params.targetRail |
| 15 | + * @param {number} params.direction |
| 16 | + * @param {Array} params.coords |
| 17 | + * |
| 18 | + * @returns {Array} |
| 19 | + */ |
| 20 | +const fillDecodeFence = ({ |
| 21 | + railCount, strLen, string, fence, targetRail, direction, coords, |
| 22 | +}) => { |
| 23 | + if (string.length === 0) return fence; |
| 24 | + |
| 25 | + const [currentRail, currentColumn] = coords; |
| 26 | + const shouldGoNextRail = currentColumn === strLen - 1; |
| 27 | + const nextDirection = shouldGoNextRail |
| 28 | + ? DIRECTIONS.DOWN |
| 29 | + : getNextDirection({ railCount, currentRail, direction }); |
| 30 | + const nextRail = shouldGoNextRail ? targetRail + 1 : targetRail; |
| 31 | + const nextCoords = [ |
| 32 | + shouldGoNextRail ? 0 : currentRail + nextDirection, |
| 33 | + shouldGoNextRail ? 0 : currentColumn + 1, |
| 34 | + ]; |
| 35 | + |
| 36 | + const shouldAddChar = currentRail === targetRail; |
| 37 | + const [currentChar, ...remainderString] = string; |
| 38 | + const nextString = shouldAddChar ? remainderString : string; |
| 39 | + const nextFence = shouldAddChar ? fence.map(addChar(currentRail, currentChar)) : fence; |
| 40 | + |
| 41 | + return fillDecodeFence({ |
| 42 | + railCount, |
| 43 | + strLen, |
| 44 | + string: nextString, |
| 45 | + fence: nextFence, |
| 46 | + targetRail: nextRail, |
| 47 | + direction: nextDirection, |
| 48 | + coords: nextCoords, |
| 49 | + }); |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * @param {object} params |
| 54 | + * @param {number} params.railCount |
| 55 | + * @param {number} params.strLen |
| 56 | + * @param {Array} params.fence |
| 57 | + * @param {number} params.currentRail |
| 58 | + * @param {number} params.direction |
| 59 | + * @param {Array} params.code |
| 60 | + * |
| 61 | + * @returns {string} |
| 62 | + */ |
| 63 | +const decodeFence = ({ |
| 64 | + railCount, strLen, fence, currentRail, direction, code, |
| 65 | +}) => { |
| 66 | + if (code.length === strLen) return code.join(''); |
| 67 | + |
| 68 | + const [currentChar, ...nextRail] = fence[currentRail]; |
| 69 | + const nextDirection = getNextDirection({ railCount, currentRail, direction }); |
| 70 | + |
| 71 | + return decodeFence({ |
| 72 | + railCount, |
| 73 | + strLen, |
| 74 | + currentRail: currentRail + nextDirection, |
| 75 | + direction: nextDirection, |
| 76 | + code: [...code, currentChar], |
| 77 | + fence: fence.map((rail, idx) => (idx === currentRail ? nextRail : rail)), |
| 78 | + }); |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * @param {string} string |
| 83 | + * @param {number} railCount |
| 84 | + * |
| 85 | + * @returns {string} |
| 86 | + */ |
| 87 | +export default function decodeRailFenceCipher(string, railCount) { |
| 88 | + const strLen = string.length; |
| 89 | + const emptyFence = buildFence(railCount); |
| 90 | + const filledFence = fillDecodeFence({ |
| 91 | + railCount, |
| 92 | + strLen, |
| 93 | + string: string.split(''), |
| 94 | + fence: emptyFence, |
| 95 | + targetRail: 0, |
| 96 | + direction: DIRECTIONS.DOWN, |
| 97 | + coords: [0, 0], |
| 98 | + }); |
| 99 | + |
| 100 | + return decodeFence({ |
| 101 | + railCount, |
| 102 | + strLen, |
| 103 | + fence: filledFence, |
| 104 | + currentRail: 0, |
| 105 | + direction: DIRECTIONS.DOWN, |
| 106 | + code: [], |
| 107 | + }); |
| 108 | +} |
0 commit comments