Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* rot.ls

  • Loading branch information...
commit 330698b01ef9b7ca0f5315de7aff12c2af275f96 0 parents
@audreyt authored
Showing with 41 additions and 0 deletions.
  1. +41 −0 rot.ls
41 rot.ls
@@ -0,0 +1,41 @@
+#!/usr/bin/env lsc
+
+require! fs
+
+c2p = {}; p2c = []
+pin-p = {}
+p-count = 0
+
+for l in "#{fs.read-file-sync 'Mandarin.txt'}" / /\n/
+ [code, pin] = l / /\s+/
+ continue unless code is /^....$/ # BMP
+ char = String.fromCharCode parseInt code, 16
+ p = (pin-p[pin] ?= p-count++)
+ c2p[char] = p
+ p2c[p] ||= ""
+ p2c[p] += char
+
+ring = ''
+for p, cs of p2c
+ if cs.length is 1
+ p2c[p] = 0
+ delete c2p[cs.0]
+ else
+ ring += p2c[p] + p2c[p].0
+
+#console.log ring
+rev = ''
+``for (var i = ring.length - 1; i >= 0; rev += ring[i--]) {}``
+
+encode = -> (* '') <| for c in it
+ idx = ring.index-of c
+ if idx is -1 then c else ring.char-at idx + 1
+
+decode = -> (* '') <| for c in it
+ idx = rev.index-of c
+ if idx is -1 then c else rev.char-at idx + 1
+
+#console.log encode "主席:黃委員還是堅持刪減200萬?但我們委員從原先的全刪,改為一成,後來又改為5%,現在又改為500萬,而經過本席折 衝,建議減列300萬。我再最後一次詢問各位,是否互相折衝一下,減列300萬?"
+#console.log decode "嘱棤:黄娓园鍰昰奸歭剼湕200蟃?僤捰们娓园悰厡嘕的啳剼,絠煟伊承,洉俫右絠煟5%,県扗右絠煟500蟃,耏经㳀楍棤摺 蹖,徤讛湕劣300蟃。捰在栬洉伊絘询妏嗰卫,昰殕冱箱摺蹖伊吓,湕劣300蟃?"
+console.log encode "林書豪真的沒有講過這句話"
+

2 comments on commit 330698b

@vendethiel
  i = ring.length - 1
  while i >= 0, rev += ring[i--] then

?

@audreyt
Owner

Makes sense. 8d1ae98

Please sign in to comment.
Something went wrong with that request. Please try again.