How to write offline in real time http://atnd.org/events/40389 Reference problem (turtle along the road) http://nabetani.sakura.ne.jp/hena/ord12aloroturtle/ Implementation example. ruby.
Examples of answers in other languages http://qiita.com/Nabetani/items/1de39df381dfeee305ab You can follow from.
so. I wrote it in python, which I'm not used to. Works with both python 3.3 and python 2.7.
Like this.
#coding:utf-8
import re
MAP = [
range(i*11, i*11+11) for i in range(0,3)
] + [
range(33+i*3, 33+i*3+3) for i in range(0,9)
]
def chars( start, len ) :
return [ chr(c) for c in range(ord(start),ord(start)+len) ]
NAMES=chars( "A", 26 ) + chars( "a", 26 ) + chars( "0", 10 )
def turn( d, r ):
return [r*d[1],-r*d[0]]
def name( t ):
return NAMES[ MAP[ t[0][1] & 0xff][ t[0][0] & 0xff] & 0xff]
def move(t):
n=name(t)
for warp in [[2,11,"efg"], [10,2,"567"]]:
if n in warp[2] and t[1][1]==1 :
t[0]=[warp[0]-warp[2].index(n),warp[1]]
t[1]=[0,-1]
break
else:
t[0][0]+=t[1][0]
t[0][1]+=t[1][1]
def solve( src ):
t=[[0,0],[1,0]]
r="A"
try:
for c in src :
if c=="L" or c=="R":
t[1]=turn(t[1], 1 if c=="L" else -1)
else:
for i in range(int(c,16)):
move(t)
r+=name(t)
except IndexError:
r+="?"
return r
def test( samples ) :
for line in samples.splitlines():
a=re.split( "\s+", line ) # num, input, expected
if 2<len(a):
actual = solve( a[1] )
ok=actual==a[2]
print( [ "ok" if ok else "***NG***", a[1:3], actual ] )
test( """
0 2RcL3LL22 ABCNYjmpsvy147edcbcdef
1 L3R4L5RR5R3L5 A?
40 6LR2R1LR5LRLRL484L63 ABCDEFGHITe741yxw?""" )
As usual, most of the test data is omitted.
The implementation policy is to read the map so that it has a Cartesian coordinate system and warp it where it is needed.
45 lines excluding tests. It's not like it's long.
The name method & 0xff
is for converting a negative index to an invalid index. Feeling evil.
This time, for the first time, I tried using the else and exception of the for statement. I think the exception is the usage that is not good.
However, no matter how many times I write it, the if expression is unpleasant.
Recommended Posts