From 293df027348307de896232f1b867dd220ae8caa3 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 1 Apr 2025 11:45:48 +0100 Subject: [bracket] rename --- static/bs/puzzleencoder.js | 83 ---------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 static/bs/puzzleencoder.js (limited to 'static/bs/puzzleencoder.js') diff --git a/static/bs/puzzleencoder.js b/static/bs/puzzleencoder.js deleted file mode 100644 index fcba62c..0000000 --- a/static/bs/puzzleencoder.js +++ /dev/null @@ -1,83 +0,0 @@ -// Utility functions for compact puzzle encoding/decoding -const PuzzleEncoder = { - // Simple substitution cipher using base64 - cipher: { - encode(str) { - return btoa(encodeURIComponent(str)).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_'); - }, - decode(str) { - str = str.replace(/-/g, '+').replace(/_/g, '/'); - // Add back padding if needed - while (str.length % 4) str += '='; - try { - return decodeURIComponent(atob(str)); - } catch (e) { - console.error('Failed to decode:', e); - return null; - } - } - }, - - // Compress puzzle structure by replacing common patterns - compressPuzzle(puzzle) { - return puzzle - .replace(/\[\[/g, '<<') // Replace double brackets - .replace(/\]\]/g, '>>') // with angle brackets - .replace(/\[/g, '<') // Replace single brackets - .replace(/\]/g, '>') // with single angle brackets - .replace(/\s+/g, ' '); // Normalize spaces - }, - - // Decompress puzzle structure - decompressPuzzle(compressed) { - return compressed - .replace(/<<|〈〈/g, '[[') - .replace(/>>|〉〉/g, ']]') - .replace(/[<〈]/g, '[') - .replace(/[>〉]/g, ']'); - }, - - // Compress solutions map into compact format - compressSolutions(solutions) { - const pairs = Object.entries(solutions) - .map(([expr, sol]) => `${expr}:${sol}`) - .join('|'); - return pairs; - }, - - // Decompress solutions back into object - decompressSolutions(compressed) { - if (!compressed) return {}; - return Object.fromEntries( - compressed.split('|') - .map(pair => pair.split(':')) - .filter(([k, v]) => k && v) // Filter out any invalid pairs - ); - }, - - // Encode entire puzzle into compact URL-safe string - encodePuzzle(puzzleData) { - const compressed = { - p: this.compressPuzzle(puzzleData.initialPuzzle), - s: this.compressSolutions(puzzleData.solutions) - }; - return this.cipher.encode(JSON.stringify(compressed)); - }, - - // Decode puzzle from compact string - decodePuzzle(encoded) { - try { - const decoded = this.cipher.decode(encoded); - if (!decoded) return null; - - const compressed = JSON.parse(decoded); - return { - initialPuzzle: this.decompressPuzzle(compressed.p), - solutions: this.decompressSolutions(compressed.s) - }; - } catch (e) { - console.error('Failed to decode puzzle:', e); - return null; - } - } - }; \ No newline at end of file -- cgit v1.2.3