Skip to content

Instantly share code, notes, and snippets.

@tkych
Last active December 29, 2015 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkych/7618254 to your computer and use it in GitHub Desktop.
Save tkych/7618254 to your computer and use it in GitHub Desktop.
;;;; Last modified: 2013-11-24 15:25:47 tkych
;;====================================================================
;; electric noticeboard
;;====================================================================
;; [第15回オフラインリアルタイムどう書くの問題](http://qiita.com/Nabetani/items/cba03c96d1ea55f6e861)
;; [異星の電光掲示板](http://nabetani.sakura.ne.jp/hena/ord15elebubo/)
;; L R J T U N S Z
;; (decode "2ed8aeed/34b0ea5b") => "LTRSUNTSJ"
;;--------------------------------------------------------------------
(in-package :cl-user)
(defpackage #:lrjtunsz (:use #:cl))
(in-package #:lrjtunsz)
(defun decode (string)
(multiple-value-bind (up down) (parse-integers string)
(parse-data up down)))
(defun parse-integers (string)
(values (parse-integer string :radix 16 :junk-allowed t)
(parse-integer string :start 9 :radix 16 :junk-allowed t)))
(defun logbitp* (i n)
(unless (minusp i)
(logbitp i n)))
(defun parse-data (up down)
(loop :with result := '()
:with i := 31
:while (plusp i)
:for u := (logbitp* i up)
:for d := (logbitp* i down)
:do (cond
;; Space
((not (or u d))
(decf i))
;; L R U N
((and u d)
(decf i)
(if (logbitp* i up)
;; R N
(progn
(decf i)
(if (logbitp* i up)
(progn
(push #\N result)
(decf i))
(push #\R result)))
;; L U
(progn
(decf i)
(if (logbitp* i down)
(progn
(push #\U result)
(decf i))
(push #\L result)))))
;; T Z
((and u (not d))
(decf i 2)
(if (logbitp* i up)
(push #\T result)
(push #\Z result))
(decf i))
;; J S
((and (not u) d)
(decf i 2)
(if (and (logbitp* i up)
(not (logbitp* i down)))
(push #\S result)
(push #\J result))
(decf i))
(t
(error "bug")))
:finally (return (coerce (nreverse result) 'string))))
;;--------------------------------------------------------------------
;; Tests
;;--------------------------------------------------------------------
(defun =>? (got want)
(assert (string= got want)))
(progn
(=>? (decode "2ed8aeed/34b0ea5b") "LTRSUNTSJ")
(=>? (decode "00000200/00000300") "L")
(=>? (decode "00018000/00010000") "R")
(=>? (decode "00002000/00006000") "J")
(=>? (decode "00000700/00000200") "T")
(=>? (decode "01400000/01c00000") "U")
(=>? (decode "00003800/00002800") "N")
(=>? (decode "000c0000/00180000") "S")
(=>? (decode "00003000/00001800") "Z")
(=>? (decode "132eae6c/1a64eac6") "LRJTUNSZ")
(=>? (decode "637572d0/36572698") "ZSNUTJRL")
(=>? (decode "baddb607/d66b6c05") "LTJZTSSSN")
(=>? (decode "db74cd75/6dac6b57") "ZZZTJZRJNU")
(=>? (decode "3606c2e8/1b0d8358") "ZZSSLTJ")
(=>? (decode "ad98c306/e6cc6183") "UZZZZZZ")
(=>? (decode "4a4aaee3/db6eeaa6") "JJLLUUNNS")
(=>? (decode "ecd9bbb6/598cd124") "TSSZZTTRR")
(=>? (decode "e0000002/40000003") "TL")
(=>? (decode "a0000007/e0000005") "UN")
(=>? (decode "c0000003/80000006") "RS")
(=>? (decode "40000006/c0000003") "JZ")
(=>? (decode "01da94db/00b3b6b2") "TSUJLRSR")
(=>? (decode "76eeaaea/24aaeeae") "TRNNUUNU")
(=>? (decode "1dacaeee/1566e444") "NRJZUTTT")
(=>? (decode "26c9ac60/6c6d66c0") "JSZLRJZS")
(=>? (decode "6c977620/36da5360") "ZZLLTNZJ")
(=>? (decode "069aeae6/0db34eac") "SJSLTUNS")
(=>? (decode "06d53724/049da56c") "RRULRNJJ")
(=>? (decode "069b58b0/04d66da0") "RLRSLZJR")
(=>? (decode "1b6eced4/11b46a9c") "RZZTZNRU")
(=>? (decode "522e8b80/db6ad900") "JLLJNLJT")
(=>? (decode "6546cdd0/376c6898") "ZULSZRTL")
(=>? (decode "4e6d5b70/6ad9d620") "LNSSURST")
(=>? (decode "37367772/65635256") "SNSZNTNJ")
(=>? (decode "25535d58/377669cc") "LUUSLTUZ")
(=>? (decode "0ae6a55d/0eacedcb") "UNSUJUTJ")
(=>? (decode "76762edc/23536a88") "TZNZJNRT")
)
;;====================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment