å
é±ãé²æãã£ã³ãã¨ããç¥ãåãã§éã¾ã£ã¦é²æãåºãä¼ã«è¡ã£ã¦ãã¦ããã㧠@haya14busa ããã¨è¡¨é¡ã®ä»¶ã«ã¤ãã¦è²ã
話ããã
ãã®å¾å人çã«ææ³ãã«ã¤ã¼ã³ãããããã®ã§ãçµæãã¾ã¨ãã¦ããã
AtCoder ã¨ã¯
http://atcoder.jp/
ç§èªèº«ããã¨ã¯ãã¨è¨ã£ã¦èªããã»ã©è©³ããã¯ãªãã®ã§ç°¡åã«èª¬æããã¨ã主ã«æ¥æ¬äººåãã®ç«¶æããã°ã©ãã³ã°ãµã¤ãã
åé¡ã¯å
¨ã¦æ¥æ¬èªã§ãå®æçã«æ§ã
ãªã³ã³ãã¹ããéå¬ããã¦ãããåé¡ã®é£æ度ãä½ãåå¿è
åãã®ã³ã³ãã¹ã(AtCoder Beginner Contest é称 ABC)ãªãããããã®ã§ã競æããã°ã©ãã³ã°ã®å
¥å£ã¨ãã¦ã¯æé©ã ã
AtCoder ã®å¯¾å¿è¨èª
競æããã°ã©ãã³ã°ã§ã¯ãã½ã¼ã¹ã³ã¼ããæåºããæåºãããã³ã¼ããæ£ããåããã©ãããµã¼ãå´ã§å®éã«åããã¦æ¤è¨¼ãè¡ããã¤ã¾ãããµã¼ãå´ã«åè¨èªã®å¦çç³»ãå¿
è¦ã§ããã競æããã°ã©ãã³ã°ã§ä½¿ç¨ã§ããè¨èªã¯ãã®ç¹ã§å¶ç´ããããã
ã©ã®è¨èªã使ãããã¯ç«¶æããã°ã©ãã³ã°ã«ãã£ã¦æ§ã
ã§ããããAtCoder ã¯ããªãå¤ãã®è¨èªã«å¯¾å¿ãã¦ããã
ããããVim script ã¯å¯¾å¿è¨èªã«ã¯å«ã¾ãã¦ããªãã
ããããã®ã¯ããVim script ã¯è¨èªã¨ãã¦ãã¤ãã¼ã¨ããç¹ãé¤ãã¦ããã¨ããåé¡ãããã
競æããã°ã©ãã³ã°ã®åé¡å½¢å¼ã¨ Vim script
競æããã°ã©ãã³ã°ã«ãããåé¡ã¯ãå¤ãã®å ´åãæ¨æºå
¥åã¨æ¨æºåºåã使ãã
æ¨æºå
¥åããåé¡ã®ãã¼ã¿ãåãåããåé¡ã解ãã¦ãçµæãæ¨æºåºåã«åºåãããã
AtCoder ãä¾ã«æ¼ãããã®å½¢å¼ã§ããã
æ¨æºå
¥åºåã¯å¤ãã®è¨èªãæ¨æºã§æ±ããã®ã§ããã®å½¢å¼ã«ãããã¨ã§å¤ãã®è¨èªã«å¯¾å¿ãããã¨ãã§ããããã ã¨æãããã
ããããVim script ã¯ããã¹ãã¨ãã£ã¿ Vim ãæ¡å¼µããããã®è¨èªãæ¨æºå
¥åºåãç´æ¥æ±ããã¨ã¯ãã§ããªãã
ã§ã¯ã©ãããã
AtCoder ã®å¯¾å¿è¨èªã®1ã¤ã«ãBash ãããã
Bash ã¯ãããèªèº«ã«ããããªãã®æ¼ç®è½åã¯ããããã©ã¡ããã¨è¨ãã¨å¤é¨ããã°ã©ã ãå¼ã³åºãè½åã«é·ãã¦ããã
å®éãawk ã bc ãªããããã使ããããããããã(ããç¥ããªããã©)ã
ã¤ã¾ãããã® Bash ã®ç°å¢ã« Vim ãå
¥ã£ã¦ããã°ããã¨ã¯æ¨æºå
¥åºåã Bash å´ã§é¢åãè¦ããã¨ã«ãã£ã¦ãVim script ã使ãããã¨ã«ãªãã
試ããã¨ãã Vim ã¯ã¡ããã¨å
¥ã£ã¦ããããã ã£ã*1ããã㧠Vim script ã§åé¡ã解ããã
Vim script ã§åé¡ã解ã
AtCoder ã«ã¯ç·´ç¿ç¨ã®ã³ã³ãã¹ããããã®ã§ãããã«ããç·´ç¿åé¡ã解ãã¦ã¿ãã
http://practice.contest.atcoder.jp/tasks/practice_1
vim -u NONE -i NONE -N -n -e -s -S <(cat <<EOF function! s:main(input) abort let a = a:input[0] let [b, c] = split(a:input[1], ' ') let s = a:input[2] return (a + b + c) . ' ' . s endfunction let s:input = getline(1, '$') enew put =s:main(s:input) 1 delete _ %print EOF ) <(cat)
çµæã¯こちらã
ã¡ããã¨è§£ãã¦ãã
ä½ããã¦ããã®ã
ã¾ãããã®ã³ã¼ãèªä½ã¯ Bash ã§ããããäºå®ä¸ãã®å¤§é¨å㯠Vim script ã§ããã
ã¡ãã£ã¨ãã¤å解ãã¦ããã¨ãã¾ã vim ãèµ·åããã³ã¼ã㯠Vim script ã®é¨åãçç¥ããã¨ä»¥ä¸ã®ããã«ãªã(... ã®é¨åãçç¥é¨å)ã
vim -u NONE -i NONE -N -n -e -s -S <(...) <(cat)
- -u NONE
- vimrc ãã¡ã¤ã«ãèªã¿è¾¼ã¾ãªãã
- -i NONE
- vininfo ãã¡ã¤ã«ãä½æããªãã
- -N
- vi äºæã¢ã¼ããOFFã«ãã¦(ã¤ã¾ãVimã¨ãã¦)èµ·åããã
- -n
- ã¹ã¯ãããã¡ã¤ã«ãä½ããªãã
- -e -s
- ãããã¢ã¼ãã§èµ·åããã詳細ã¯å²æã(æ°ã«ãªã人㯠:help -s-ex ãåç §)
- -S {ãã¡ã¤ã«}
- èµ·åå¾ã«ãã¡ã¤ã«ã Vim script ã¨ãã¦å®è¡ããã
- <(cat)
ãã¦ãVim ãèµ·åãã¦ãããã¨ã¯ããã£ãã®ã§ã次㯠Vim script é¨åã
function! s:main(input) abort let a = a:input[0] let [b, c] = split(a:input[1], ' ') let s = a:input[2] return (a + b + c) . ' ' . s endfunction let s:input = getline(1, '$') enew put =s:main(s:input) 1 delete _ %print
ã¡ãã£ã¨ãã¤è¦ã¦ããã
function! s:main(input) abort let a = a:input[0] let [b, c] = split(a:input[1], ' ') let s = a:input[2] return (a + b + c) . ' ' . s endfunction
ãããããããããããåé¡ã解ãé¨åãé¢æ°ã«åããã
å¼æ°ã® input ã¯ãå
¥åã®ããã¹ããè¡åä½ã§é
åã§æ¸¡ã£ã¦ããã
çµæãæååããè¡åä½ã®é
åã§è¿ãã
let s:input = getline(1, '$')
å
¥åã¯ãã¡ã¤ã«ã¨ãã¦æ¸¡ã£ã¦ãã¦ããããããã¡ã«éããã¦ããã
ãããã¡ã®å
¨è¡ãé
åã¨ã㦠s:input ã«ä¿åãã¦ããã
enew
output ç¨ã®æ°ãããããã¡ãéãã
put =s:main(s:input) 1 delete _
çµæããããã¡ã«å±éãã¦ããã
空ã®ãããã¡ã« put ã§ããã¹ããç½®ãã¨1è¡ç®ã«ç©ºè¡ãæ®ã£ã¦ãã¾ãã®ã§ãdelete ã§ãã®ä½è¨ãªç©ºè¡ãæ¶ãã¦ããã
ãããã¢ã¼ãã® Vim ã§ã¯ãprint ãªã©ã®ããã¤ãã®ã³ãã³ãã®çµæã¯æ¨æºåºåã¸åºãããã
%print ã§ãããã¡å
¨ä½ãæ¨æºåºåã¸çµæã¨ãã¦åºåãã¦ããã
ã¡ãªã¿ã«ãã®æ¹æ³ã ã¨ãæå¾ã«æ¹è¡ã®ãªãåºåãè¡ããªã*2ãã大æµã®ç«¶æããã°ã©ãã³ã°ã®åé¡ã¯æå¾ã«æ¹è¡ãåºåãããã®ã§ãåé¡ã«ãªããã¨ã¯ãªãã ããã
解ãç°å¢ãæ´ãã
ãã³ãã¬ã¼ã
ãããã¦è¦ãã¨ãããéããå®éã«è§¦ãã®ã¯ s:main() é¢æ°ã®é¨åã ãã§ãããä»ã®é¨åã¯ãã³ãã¬ã¼ãã ã
ãã³ãã¬ã¼ãç³»ã®ãã©ã°ã¤ã³ã使ã£ã¦ããã³ãã¬ã¼ãåãã¦ããã¨ä¾¿å©ã ããã
ã¡ãªã¿ã«ç§ã¯ template.vim ã使ã£ã¦ãã(宣ä¼)ã
Vim script é¨åã ãç·¨éãã
ãã¡ã¤ã«å
¨ä½ã¨ãã¦ã¯ Bash ã ãã©ãç·¨éãããã®ã¯ Vim script ã ãmain ã®é¨åã ãããfiletype=vim ã§ç·¨éãããã
ãããªæã«ä¾¿å©ãªã®ã partedit.vim ã ã
ãããã¡ã®ä¸é¨ãå¥ã®ãããã¡ã§éããå¥ã®ãã¡ã¤ã«ã¿ã¤ãã§ç·¨éã§ãããä¿åããã¨å
ã®ãããã¡ã«é©ç¨ãããã
ä¸ã®åä½ä¾ã§ã¯ :Partedit ã³ãã³ããå¼æ°ãªãã§åããã¦ããããããããããã«ã¯ãªãã·ã§ã³ã®è¨å®ãå¿ è¦ã ã
" é¨åãç·¨éãããããã¡ãéãã³ãã³ãã®æå®ãããã¯ç¸¦åå²ãããå ´åã®ä¾ (ããã©ã«ãã ã¨ç¾å¨ã®ã¦ã£ã³ãã¦ã«ãã®ã¾ã¾éããã) let g:partedit#opener = 'vsplit' " é¨åãç·¨éãããããã¡ã® filetypeãããã©ã«ãã ã¨å ã®ãããã¡ã¨åã filetype ãé©ç¨ãããã let g:partedit#filetype = 'vim'
ãã¡ã¤ã«ã¿ã¤ãã¯å¸¸ã«åãã ã¨å¥ã®ã·ã¼ã³ã§ä½¿ãããå ´åã«å°ãã¨æãã®ã§ãlocalrc.vim ãªã©ãå©ç¨ãã¦ãb:partedit_filetype ãè¨å®ããã¨ããã ããã
ãã¹ãã±ã¼ã¹ãå®è¡ããã ãã®1
çããæ£ããããæåºããåã«æå
ã§å®è¡ãã¦ç¢ºèªãããã ããã
éãã¦ãããã¡ã¤ã«ãå®è¡ããããã¨æ¥ãã°ãquickrun.vim ã便å©ã ã
åé¡ã®å
¥åãæ¨æºå
¥åã§ä¸ããå¿
è¦ããããã幸ã quickrun.vim ã¯æ¨æºå
¥åã«ã対å¿ãã¦ãããinput ãªãã·ã§ã³ãè¨å®ããã°ããã
" input.txt ãã¡ã¤ã«ã®ä¸èº«ãæ¨æºå ¥åã¨ãã¦ä½¿ã QuickRun -input input.txt " = ã§å§ãããã¨ã§ãã¡ã¤ã«ã§ã¯ãªãå ¥åæååãç´æ¥ä¸ãããã QuickRun -input =input-text " i ã¬ã¸ã¹ã¿ã®ä¸èº«ãæ¨æºå ¥åã¨ãã¦ä½¿ã QuickRun -input =@i " b:input å¤æ°ã®ä¸èº«ãæ¨æºå ¥åã¨ãã¦ä½¿ã QuickRun -input =%{b:input} " äºåã«è¨å®ãã¦ããã°æ¯åæå®ããå¿ è¦ã¯ãªã let g:quickrun_config = {'_': {}} let g:quickrun_config._.input = '=@i' let g:quickrun_config._.input = '=%{b:input}' " å®è¡ããéã«å¤æ°ã«å¤ãå ¥ããã° OK let b:input = "1\n2 3\ntest\n"
print ãããã°ããã«ã¯ãput ã³ãã³ãã使ãã°ããã
æçµçãªãããã¡ã®å
容ãåºåçµæã«ãªãã®ã§ãput ã³ãã³ãã§ãããã¡ã«è¡ã足ãã¦ããã
let hoge = 'debug' put =hoge " put ã®ä¸ã§ã¯ " ã¯ã³ã¡ã³ãæ±ãã«ãªã使ããªãã®ã§æ³¨æ put ="foo" ããã¯ã³ã¡ã³ãã«ãªãã®ã§åããªã put =\"foo\" " ãããªãåã
ãã¹ãã±ã¼ã¹ãå®è¡ããã ãã®2
å®ã¯ããã£ã¨æ¥½ãªæ¹æ³ãããããã㧠s:main() ãåãåãã¦ãããã®ãå½¹ã«ç«ã¤ã
åç´ã« s:main() ãå¼ãã§ãã¾ãã°è¯ãã
function! s:main(input) abort let a = a:input[0] let [b, c] = split(a:input[1], ' ') let s = a:input[2] return (a + b + c) . ' ' . s endfunction echo s:main(['1', '2 3', 'test'])
ãã㧠OK ã ããã ãããã®ç¶æ
㧠Bash ã¨ãã¦ãããå®è¡ãã¦ãçµæãè¦ããã¨ã¯ã§ããªãã
ãããå
ã»ã©ã® partedit.vim ã§ããã®ãããã¡ã¯ Vim script ã¨ãã¦ç¬ç«ãã¦ããããããquickrun.vim ã使ãã°ããããã®ãããã¡ã¯ãã¡ã¤ã«ã¨ãã¦åå¨ãã¦ããªãããquickrun.vim ã§ããã°å®è¡å¯è½ã ã
ãã¡ãã®æ¹æ³ã§ print ãããã°ãããå ´åã¯ãecho ã³ãã³ãã使ãã° OK ã ãããããããã
ã¡ãªã¿ã«ãããã® echo ã®è¡ã¯ãBash ã¨ãã¦ã®å®è¡çµæã«ã¯ä¸åå½±é¿ãåã¼ããªãã®ã§ããã®ã¾ã¾æ稿ãããã¨ãå¯è½ã ããå½ç¶å®è¡æéã¯é·ããªã£ã¦ãã¾ãã®ã§æ³¨æãå¿ è¦ã ã
æå¾ã«
Vim script 㧠AtCoder ã«åå ããæ¹æ³ã«ã¤ãã¦ç´¹ä»ããã
ä»åã®ä¾ã§ã¯åºã¦ããªãã£ãããå½ç¶ main 以å¤ã®é¢æ°ãå®ç¾©ãããã¨ãã§ããã
ã¾ããããããæä½ã«ã¤ãã¦ã¯ vital.vim ã®ã³ã¼ããåèã«ãªããData.List ã Data.StringãMath 辺ãã«ã¯ä¾¿å©é¢æ°ãæã£ã¦ãããNYSL ãªã®ã§ãã³ãããã¦ä½¿ã£ã¦ãã¾ã£ããåé¡ãªããæ¯éæ´»ç¨ãã¦æ¬²ããã
Enjoy Programming!
*1:軽ã調ã¹ã¦ã¿ãã¨ãããç·´ç¿ç¨ã³ã³ãã¹ãã® Vim ã®ãã¼ã¸ã§ã³ã¯ 7.3.429 ã ã£ããUbuntu 12.04 ã® Vim ããã®ãã¼ã¸ã§ã³ãªã®ã§ããã®ç°å¢ã使ã£ã¦ããå¯è½æ§ãé«ããã³ã³ãã¹ãã«ãã£ã¦ã¯ç°å¢ãç°ãªãå¯è½æ§ãããã®ã§æ³¨æã
*2:ä¸å¿å¥ã®æ¹æ³ã§åé¿ã¯ã§ããã