|
| 1 | +/** |
| 2 | + * @typedef {string[]} Rail |
| 3 | + * @typedef {Rail[]} Fence |
| 4 | + * @typedef {number} Direction |
| 5 | + */ |
| 6 | + |
1 | 7 | /**
|
2 | 8 | * @constant DIRECTIONS
|
3 | 9 | * @type {object}
|
4 |
| - * @property {number} UP |
5 |
| - * @property {number} DOWN |
| 10 | + * @property {Direction} UP |
| 11 | + * @property {Direction} DOWN |
6 | 12 | */
|
7 |
| -export const DIRECTIONS = { UP: -1, DOWN: 1 }; |
| 13 | +const DIRECTIONS = { UP: -1, DOWN: 1 }; |
8 | 14 |
|
9 | 15 | /**
|
10 |
| - * @param {number} rows |
| 16 | + * Builds a fence with a specific number of rows. |
11 | 17 | *
|
12 |
| - * @returns {Array} |
| 18 | + * @param {number} rowsNum |
| 19 | + * @returns {Fence} |
13 | 20 | */
|
14 |
| -export const buildFence = (rows) => Array(rows) |
15 |
| - .fill() |
| 21 | +const buildFence = (rowsNum) => Array(rowsNum) |
| 22 | + .fill(null) |
16 | 23 | .map(() => []);
|
17 | 24 |
|
18 | 25 | /**
|
19 |
| - * @param {object} params |
20 |
| - * @param {number} params.railCount |
21 |
| - * @param {number} params.currentRail |
22 |
| - * @param {number} params.direction |
| 26 | + * Get next direction to move (based on the current one) while traversing the fence. |
23 | 27 | *
|
24 |
| - * @returns {number} |
| 28 | + * @param {object} params |
| 29 | + * @param {number} params.railCount - Number of rows in the fence |
| 30 | + * @param {number} params.currentRail - Current row that we're visiting |
| 31 | + * @param {Direction} params.direction - Current direction |
| 32 | + * @returns {Direction} - The next direction to take |
25 | 33 | */
|
26 |
| -export const getNextDirection = ({ railCount, currentRail, direction }) => { |
| 34 | +const getNextDirection = ({ railCount, currentRail, direction }) => { |
27 | 35 | switch (currentRail) {
|
28 |
| - case 0: return DIRECTIONS.DOWN; |
29 |
| - case railCount - 1: return DIRECTIONS.UP; |
30 |
| - default: return direction; |
| 36 | + case 0: |
| 37 | + // Go down if we're on top of the fence. |
| 38 | + return DIRECTIONS.DOWN; |
| 39 | + case railCount - 1: |
| 40 | + // Go up if we're at the bottom of the fence. |
| 41 | + return DIRECTIONS.UP; |
| 42 | + default: |
| 43 | + // Continue with the same direction if we're in the middle of the fence. |
| 44 | + return direction; |
31 | 45 | }
|
32 | 46 | };
|
33 | 47 |
|
34 | 48 | /**
|
35 |
| - * Given a rail, adds a char to it |
36 |
| - * if it matches a targetIndex. |
37 |
| - * @callback charAdder |
38 |
| - * @param {number} rail |
39 |
| - * @param {currentRail} number |
| 49 | + * @param {number} targetRailIndex |
| 50 | + * @param {string} letter |
| 51 | + * @returns {Function} |
40 | 52 | */
|
| 53 | +const addCharToRail = (targetRailIndex, letter) => { |
| 54 | + /** |
| 55 | + * Given a rail, adds a char to it if it matches a targetIndex. |
| 56 | + * |
| 57 | + * @param {Rail} rail |
| 58 | + * @param {number} currentRail |
| 59 | + * @returns {Rail} |
| 60 | + */ |
| 61 | + function onEachRail(rail, currentRail) { |
| 62 | + return currentRail === targetRailIndex |
| 63 | + ? [...rail, letter] |
| 64 | + : rail; |
| 65 | + } |
| 66 | + return onEachRail; |
| 67 | +}; |
41 | 68 |
|
42 | 69 | /**
|
43 |
| - * @param {number} targetIndex |
44 |
| - * @param {string} letter |
| 70 | + * Hangs the characters on the fence. |
45 | 71 | *
|
46 |
| - * @returns {charAdder} |
| 72 | + * @param {object} params |
| 73 | + * @param {Fence} params.fence |
| 74 | + * @param {number} params.currentRail |
| 75 | + * @param {Direction} params.direction |
| 76 | + * @param {string[]} params.chars |
| 77 | + * @returns {Fence} |
| 78 | + */ |
| 79 | +const fillEncodeFence = ({ |
| 80 | + fence, |
| 81 | + currentRail, |
| 82 | + direction, |
| 83 | + chars, |
| 84 | +}) => { |
| 85 | + if (chars.length === 0) { |
| 86 | + // All chars have been placed on a fence. |
| 87 | + return fence; |
| 88 | + } |
| 89 | + |
| 90 | + const railCount = fence.length; |
| 91 | + |
| 92 | + // Getting the next character to place on a fence. |
| 93 | + const [letter, ...nextChars] = chars; |
| 94 | + const nextDirection = getNextDirection({ |
| 95 | + railCount, |
| 96 | + currentRail, |
| 97 | + direction, |
| 98 | + }); |
| 99 | + |
| 100 | + return fillEncodeFence({ |
| 101 | + fence: fence.map(addCharToRail(currentRail, letter)), |
| 102 | + currentRail: currentRail + nextDirection, |
| 103 | + direction: nextDirection, |
| 104 | + chars: nextChars, |
| 105 | + }); |
| 106 | +}; |
| 107 | + |
| 108 | +/** |
| 109 | + * @param {object} params |
| 110 | + * @param {number} params.strLen |
| 111 | + * @param {string[]} params.chars |
| 112 | + * @param {Fence} params.fence |
| 113 | + * @param {number} params.targetRail |
| 114 | + * @param {Direction} params.direction |
| 115 | + * @param {number[]} params.coords |
| 116 | + * @returns {Fence} |
47 | 117 | */
|
48 |
| -export const addChar = (targetIndex, letter) => (rail, currentRail) => { |
49 |
| - return (currentRail === targetIndex ? [...rail, letter] : rail); |
| 118 | +const fillDecodeFence = (params) => { |
| 119 | + const { |
| 120 | + strLen, chars, fence, targetRail, direction, coords, |
| 121 | + } = params; |
| 122 | + |
| 123 | + const railCount = fence.length; |
| 124 | + |
| 125 | + if (chars.length === 0) { |
| 126 | + return fence; |
| 127 | + } |
| 128 | + |
| 129 | + const [currentRail, currentColumn] = coords; |
| 130 | + const shouldGoNextRail = currentColumn === strLen - 1; |
| 131 | + const nextDirection = shouldGoNextRail |
| 132 | + ? DIRECTIONS.DOWN |
| 133 | + : getNextDirection( |
| 134 | + { railCount, currentRail, direction }, |
| 135 | + ); |
| 136 | + const nextRail = shouldGoNextRail ? targetRail + 1 : targetRail; |
| 137 | + const nextCoords = [ |
| 138 | + shouldGoNextRail ? 0 : currentRail + nextDirection, |
| 139 | + shouldGoNextRail ? 0 : currentColumn + 1, |
| 140 | + ]; |
| 141 | + |
| 142 | + const shouldAddChar = currentRail === targetRail; |
| 143 | + const [currentChar, ...remainderChars] = chars; |
| 144 | + const nextString = shouldAddChar ? remainderChars : chars; |
| 145 | + const nextFence = shouldAddChar ? fence.map(addCharToRail(currentRail, currentChar)) : fence; |
| 146 | + |
| 147 | + return fillDecodeFence({ |
| 148 | + strLen, |
| 149 | + chars: nextString, |
| 150 | + fence: nextFence, |
| 151 | + targetRail: nextRail, |
| 152 | + direction: nextDirection, |
| 153 | + coords: nextCoords, |
| 154 | + }); |
| 155 | +}; |
| 156 | + |
| 157 | +/** |
| 158 | + * @param {object} params |
| 159 | + * @param {number} params.strLen |
| 160 | + * @param {Fence} params.fence |
| 161 | + * @param {number} params.currentRail |
| 162 | + * @param {Direction} params.direction |
| 163 | + * @param {number[]} params.code |
| 164 | + * @returns {string} |
| 165 | + */ |
| 166 | +const decodeFence = (params) => { |
| 167 | + const { |
| 168 | + strLen, |
| 169 | + fence, |
| 170 | + currentRail, |
| 171 | + direction, |
| 172 | + code, |
| 173 | + } = params; |
| 174 | + |
| 175 | + if (code.length === strLen) { |
| 176 | + return code.join(''); |
| 177 | + } |
| 178 | + |
| 179 | + const railCount = fence.length; |
| 180 | + |
| 181 | + const [currentChar, ...nextRail] = fence[currentRail]; |
| 182 | + const nextDirection = getNextDirection( |
| 183 | + { railCount, currentRail, direction }, |
| 184 | + ); |
| 185 | + |
| 186 | + return decodeFence({ |
| 187 | + railCount, |
| 188 | + strLen, |
| 189 | + currentRail: currentRail + nextDirection, |
| 190 | + direction: nextDirection, |
| 191 | + code: [...code, currentChar], |
| 192 | + fence: fence.map((rail, idx) => (idx === currentRail ? nextRail : rail)), |
| 193 | + }); |
| 194 | +}; |
| 195 | + |
| 196 | +/** |
| 197 | + * Encodes the message using Rail Fence Cipher. |
| 198 | + * |
| 199 | + * @param {string} string - The string to be encoded |
| 200 | + * @param {number} railCount - The number of rails in a fence |
| 201 | + * @returns {string} - Encoded string |
| 202 | + */ |
| 203 | +export const encodeRailFenceCipher = (string, railCount) => { |
| 204 | + const fence = buildFence(railCount); |
| 205 | + |
| 206 | + const filledFence = fillEncodeFence({ |
| 207 | + fence, |
| 208 | + currentRail: 0, |
| 209 | + direction: DIRECTIONS.DOWN, |
| 210 | + chars: string.split(''), |
| 211 | + }); |
| 212 | + |
| 213 | + return filledFence.flat().join(''); |
| 214 | +}; |
| 215 | + |
| 216 | +/** |
| 217 | + * Decodes the message using Rail Fence Cipher. |
| 218 | + * |
| 219 | + * @param {string} string - Encoded string |
| 220 | + * @param {number} railCount - The number of rows in a fence |
| 221 | + * @returns {string} - Decoded string. |
| 222 | + */ |
| 223 | +export const decodeRailFenceCipher = (string, railCount) => { |
| 224 | + const strLen = string.length; |
| 225 | + const emptyFence = buildFence(railCount); |
| 226 | + const filledFence = fillDecodeFence({ |
| 227 | + strLen, |
| 228 | + chars: string.split(''), |
| 229 | + fence: emptyFence, |
| 230 | + targetRail: 0, |
| 231 | + direction: DIRECTIONS.DOWN, |
| 232 | + coords: [0, 0], |
| 233 | + }); |
| 234 | + |
| 235 | + return decodeFence({ |
| 236 | + strLen, |
| 237 | + fence: filledFence, |
| 238 | + currentRail: 0, |
| 239 | + direction: DIRECTIONS.DOWN, |
| 240 | + code: [], |
| 241 | + }); |
50 | 242 | };
|
0 commit comments