The twelfth offline real-time writing reference problem. Implementation by python

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

The twelfth offline real-time writing reference problem. Implementation by python
The 10th offline real-time writing reference problem. Implementation example by Python.
The 11th offline real-time writing reference problem. Implementation example by python.
The 14th offline real-time writing reference problem with Python
The 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 15th offline real-time how to write reference problem in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
The 17th offline real-time how to write reference problem implemented in Python
The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
The 17th Offline Real-time How to Solve Writing Problems in Python
Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
The 16th offline real-time how to write problem was solved with Python
The 15th offline real-time how to write problem was solved with python
The 15th offline real-time I tried to solve the problem of how to write with python
Offline real-time how to write Python implementation example of E14
13th Offline Real-time How to Solve Writing Problems in Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
Offline real-time how to write E11 ruby and python implementation example
[Implementation example] Read the file line by line with Cython (Python) from the last line
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
All Python arguments are passed by reference
Read the file line by line in Python
Read the file line by line in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
Python: I tried the traveling salesman problem
Solve the maximum subarray problem in Python