Created
December 3, 2013 10:54
-
-
Save tkych/7767363 to your computer and use it in GitHub Desktop.
ボールカウント(野球), http://qiita.com/Nabetani/items/ebd8a56b41711ba459f9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; Last modified: 2013-12-02 19:51:05 tkych | |
;;==================================================================== | |
;; ボールカウント(野球) | |
;;==================================================================== | |
;; - [オフラインリアルタイムどう書く第三回の参考問題](http://qiita.com/Nabetani/items/ebd8a56b41711ba459f9) | |
;;-------------------------------------------------------------------- | |
(in-package :cl-user) | |
(eval-when (:compile-toplevel :load-toplevel :execute) | |
(ql:quickload :iterate)) | |
(defpackage :baseball-count | |
(:use :cl :iterate)) | |
(in-package :baseball-count) | |
;;-------------------------------------------------------------------- | |
;; Main | |
;;-------------------------------------------------------------------- | |
(defun main (input) | |
(with-output-to-string (s) | |
(let ((out 0) (strike 0) (ball 0)) | |
(iter (for c :in-string input) | |
(ecase c | |
(#\s (incf strike) | |
(when (= strike 3) | |
(incf out) | |
(setf strike 0 | |
ball 0) | |
(when (= out 3) | |
(setf out 0)))) | |
(#\b (incf ball) | |
(when (= ball 4) | |
(setf strike 0 | |
ball 0))) | |
(#\f (when (<= strike 1) | |
(incf strike) | |
(when (= strike 3) | |
(incf out) | |
(setf strike 0 | |
ball 0) | |
(when (= out 3) | |
(setf out 0))))) | |
(#\h (setf strike 0 | |
ball 0)) | |
(#\p (setf strike 0 | |
ball 0) | |
(incf out) | |
(when (= out 3) | |
(setf out 0)))) | |
(unless (first-time-p) | |
(princ #\, s)) | |
(format s "~D~D~D" out strike ball))))) | |
;;-------------------------------------------------------------------- | |
;; Tests | |
;;-------------------------------------------------------------------- | |
(defun =>? (got expected) | |
(assert (string= got expected))) | |
(progn | |
(=>? (main "s") "010") | |
(=>? (main "sss") "010,020,100") | |
(=>? (main "bbbb") "001,002,003,000") | |
(=>? (main "ssbbbb") "010,020,021,022,023,000") | |
(=>? (main "hsbhfhbh") "000,010,011,000,010,000,001,000") | |
(=>? (main "psbpfpbp") "100,110,111,200,210,000,001,100") | |
(=>? (main "ppp") "100,200,000") | |
(=>? (main "ffffs") "010,020,020,020,100") | |
(=>? (main "ssspfffs") "010,020,100,200,210,220,220,000") | |
(=>? (main "bbbsfbppp") "001,002,003,013,023,000,100,200,000") | |
(=>? (main "sssbbbbsbhsbppp") "010,020,100,101,102,103,100,110,111,100,110,111,200,000,100") | |
(=>? (main "ssffpffssp") "010,020,020,020,100,110,120,200,210,000") | |
) | |
;;==================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment