前準備
- rubyをインストール
- TEXCELL Ruby用クリップボードライブラリをインストール
- clipboard_v100.7z をダウンロード
- lhaplusなどで解凍
- できた clipboard.rb を C:\Ruby\lib\ruby\site_ruby\2.X.X とかにコピー
使い方
- 問題となるテキストを準備
- 空欄には1~9以外の文字を入力
- 以降が空欄の場合省略可
- 問題全体を範囲選択してマクロ実行
- 末尾に回答が書き込まれる
- 解題の過程は tmp_log.txt に書き出される
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | //ナンプレを解く //前準備 //1.rubyをインストール //2.TEXCELLRuby用クリップボードライブラリをインストール // ・https://www.texcell.co.jp/ruby/Lib/clipboard_v100.7zをダウンロード // ・lhaplus(https://forest.watch.impress.co.jp/library/software/lhaplus/)などで解凍 // ・できたclipboard.rbをC:\Ruby\lib\ruby\site_ruby\2.X.Xとかにコピー //使い方 //1.問題となるテキストを準備 // ・空欄には1~9以外の文字を入力 // ・以降が空欄の場合省略可 //2.問題全体を範囲選択してマクロ実行 //3.末尾に回答が書き込まれる //4.解題の過程はtmp_log.txtに書き出される copy; runsync2"rubyw -x "+currentmacrofilename; paste;endmacro; /* #! ruby # encoding: utf-8 $stdout=open("tmp_log.txt","w") $stderr=open("tmp_err.txt","w") require'clipboard' classArray defto_masu e=self [e[0][0..2]+e[1][0..2]+e[2][0..2], e[0][3..5]+e[1][3..5]+e[2][3..5], e[0][6..8]+e[1][6..8]+e[2][6..8], e[3][0..2]+e[4][0..2]+e[5][0..2], e[3][3..5]+e[4][3..5]+e[5][3..5], e[3][6..8]+e[4][6..8]+e[5][6..8], e[6][0..2]+e[7][0..2]+e[8][0..2], e[6][3..5]+e[7][3..5]+e[8][3..5], e[6][6..8]+e[7][6..8]+e[8][6..8]] end defto_n self.map{|e|e.map{|f|f.is_a?(Array)&&f.size==1?f[0]:f}} end defto_p return"error"ifself==["error"] self.map{|e|e.map{|f|f.is_a?(Array)?"[#{f.join}]":f} .join(",")}.join("\n") end defsolve ay=self at=ay.transpose as=ay.to_masu ay.map!{|e| n=e.reject{|f|f.is_a?(Array)}.to_a e.map{|f|f.is_a?(Array)?f-n:f}}.to_n at.map!{|e| n=e.reject{|f|f.is_a?(Array)}.to_a e.map{|f|f.is_a?(Array)?f-n:f}}.to_n as.map!{|e| n=e.reject{|f|f.is_a?(Array)}.to_a e.map{|f|f.is_a?(Array)?f-n:f}}.to_n puts"ay=";putsay.to_p puts"at=";putsat.to_p puts"as=";putsas.to_p jy=ay.map{|e|e.select{|f|e.count(f)==f.size}.uniq} jt=at.map{|e|e.select{|f|e.count(f)==f.size}.uniq} js=as.map{|e|e.select{|f|e.count(f)==f.size}.uniq} ck=(ay+at+as).map{|e|e.reject{|f|f.is_a?(Array)}}. map{|e|e.map{|f|e.count(f)}.uniq}.uniq-[[1]] ifck.flatten.size>0 returnay=["error"] end ay=ay.map.with_index{|e,i|#p jy[i][0] ifjy[i]!=[] e.map{|f|f.is_a?(Array)&&f!=jy[i][0]?f-jy[i][0]:f} else e end} at=at.map.with_index{|e,i|#p jt[i][0] ifjt[i]!=[] e.map{|f|f.is_a?(Array)&&f!=jt[i][0]?f-jt[i][0]:f} else e end} as=as.map.with_index{|e,i|#p js[i][0] ifjs[i]!=[] e.map{|f|f.is_a?(Array)&&f!=js[i][0]?f-js[i][0]:f} else e end} at=at.transpose as=as.to_masu ay=ay.map.with_index{|e,i|e.map.with_index{|f,j| f.is_a?(Array)?f&at[i][j]&as[i][j]:f}}.to_n end defsolves(n) ay=self 100.times{|i| puts"\n#{i+n}回目" ar=ay.solve puts"ar=";putsar.to_p return[ar,i+n]ifar==ay return[ar,i+n+1]ifar==["error"] ay=ar} ay end defhas_arr? self.map{|e|e.map{|f|1iff.is_a?(Array)}.compact}.flatten.uniq[0] end end CBd=ClipBoard.new(__ENCODING__) st=CBd.getText ay=st.split("\r\n").map{|e|e.split("").values_at(0...9). map{|f|/[1-9]/=~f?f.to_i:(1..9).to_a}} arr,ans,cnt=[ay],[],1 arr.each{|ay| ay,cnt=ay.solves(cnt) ifay==["error"] puts"ダメだった orz" next elsifay.has_arr? a0,i=[],1 a1=ay.map{|e|e.map{|f| if(i==1&&f.is_a?(Array)&&f.size==2) i,a0=2,[f,f[0]] f=f[0] else f end}} i=1 a2=ay.map{|e|e.map{|f| if(i==1&&f.is_a?(Array)&&f.size==2) i=2 f=f[1] else f end}} puts"#{a0[0]} が #{a0[1]} として検討" arr<<a1 arr<<a2 cnt+=1 next else ans=ay break end} st="\n"+ans.map.with_index{|e,i| s=e.each_slice(3).map{|f|f.join}.join(" ") i%3==2?s+"\n":s}.join("\n")+"\n" st="\nerror\n"ifst=="\n\n" CBd.setText(st) __END__ */ |
コメント