Created
October 12, 2014 03:39
-
-
Save nabetani/98e52e7b5ee9dfba987a to your computer and use it in GitHub Desktop.
Yokohama.rb で 2014年10月に出した問題の実装例
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
# problem : http://nabetani.sakura.ne.jp/yokohamarb/2014.10.rotcell/ | |
NEIBOURS = [[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]] | |
BOARD_SIZE = 5 | |
def pos_to_index(s) | |
s[0]+s[1]*BOARD_SIZE | |
end | |
def process( board, cmd ) | |
y,x = board.index( cmd.downcase ).divmod(BOARD_SIZE) | |
move_pos = NEIBOURS | |
.map{ |s| [s[0]+x, s[1]+y] } | |
.select{ |s| s.all?{ |xy| (0...BOARD_SIZE)===xy } } | |
move_names = move_pos.map{ |s| board[pos_to_index(s)] } | |
move_names | |
.rotate(cmd==cmd.upcase ? -1 : 1) | |
.zip(move_pos) | |
.each{ |name,s| board[pos_to_index(s)] =name } | |
move_names.sort.join | |
end | |
def solve( src ) | |
board = [*?a..?y] | |
src.chars.inject(nil) do |_,cmd| | |
process( board, cmd ) | |
end | |
end | |
DATA.map do |line| | |
id,src,expected = line.split(/\s+/) | |
actual = solve( src ) | |
ok = actual==expected | |
puts [(ok ? "ok" : "***NG***"), id, src, actual, expected ].join(" ") | |
ok | |
end.all?.tap{ |ok| | |
puts( ok ? "ok" : "***NG***" ) | |
} | |
__END__ | |
0 YokoHamarb acdfp | |
1 Ruby twx | |
2 ruby nst | |
3 PHP gkluv | |
4 a bfg | |
5 b acfgh | |
6 m ghilnqrs | |
7 mg bcdhilmq | |
8 Mg fhiklmpq | |
9 MS ijmnoqrt | |
10 mG bcdhilmq | |
11 lf abcghklp | |
12 paq lprvw | |
13 bFH abcfg | |
14 Agh abcfgklm | |
15 Msul pruvw | |
16 RSRX jorty | |
17 xTsn ijmos | |
18 FHwnQ lmorsvwx | |
19 jIDIb cfghilmo | |
20 rLGPq ilmnprvx | |
21 WRiFhd ceg | |
22 gkfLfh cfglmpqr | |
23 STydYQ hklmpruv | |
24 spaeAWJ cdehimns | |
25 xTxTXwY mwx | |
26 xyQMkMi cdejlnow | |
27 HJkxpWxA cdhkp | |
28 hgaGDodg djo | |
29 abKBmkBc bdfik | |
30 mCvlhnilm adfgikqr | |
31 StyxTYsIh imnostxy | |
32 HLHnhLMLC ahlmn | |
33 DuHmbFQysI ehj | |
34 cGfGCaLgCq acguv | |
35 OisYOrOXwq ilnstvwx | |
36 wMnYIukHAvO bdefhjmn | |
37 HCfBhKHkhDF abdghikn | |
38 pUPmwrHsYSH morsy | |
39 PnCYYWnPoUxq bfopv | |
40 DiojiYXowowr cilmnstw | |
41 HLNsiNMnbAnn abdkm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment