3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

オフラむンリアルタむムどう曞くE23の実装䟋

3
Last updated at Posted at 2018-04-07

問題 : http://nabetani.sakura.ne.jp/hena/orde23nokoch/
むベント : https://yhpg.doorkeeper.jp/events/71800
実装リンク集 : https://qiita.com/Nabetani/items/77e7e2c749767f197c0e

で。

最初に曞いた実装

最初に曞いたのは

ruby2.5
def dir_at( seq, pos )
  dirs = seq.reverse.chars.map{ |x| { "a"=>[0,5,1,0], "b"=>[0,5,0,1,0] }[x] }
  r=0
  dirs.each do |d|
    r += d[pos % d.size]
    pos /= d.size
  end
  return pos!=0 ? nil : r % 6
end

def solve(src)
  num, rules = src.split(",")
  dir = dir_at(rules, num.to_i - 1 )
  return "x" unless dir
  return "0-+"[dir % 3 ]
end

DATA.map do |line|
  num, src, expected = line.split(/\s+/ )
  actual = solve(src)
  okay = actual == expected
  p [ (okay ? "ok" : "**NG**"), src, actual, expected ]
  okay
end.all?.yield_self{ |ok| puts( ok ? "ok" : "something wrong" ) }

__END__
0 120,aabb  0 
1 100,a x 
2 3,a - 
3 3,b 0 
4 9,aa  - 

こんな感じ。
テストデヌタの倧半は省略。

5 たたは 4 で割ったあたりが䞀番现かい郚品の、線分の番号になる。
5 たたは 4 で割るこずで、现郚を無芖した図圢の、線分の番号になる。
ずいうこず。

再垰を䜿っお曞き盎した

フラクタルなんだから再垰だよね。ずいうこずで、再垰を䜿っお dir_at を曞き盎した。

先ほどの実装は 现郚→抂芁 ずいう順に蚈算したけど、今回は 抂芁→现郚 ずいう順に蚈算した。

ruby2.5
M={ "a"=>[0,5,1,0], "b"=>[0,5,0,1,0] }
L=M.keys.each.with_object({}){ |k,o| o[k]=M[k].size }

def dir_at( seq, pos )
  return 0 if seq.empty?
  baselen, subpos = pos.divmod( seq.chars.drop(1).map{ |x| L[x] }.inject(1, :*) )
  base = M[seq[0]][baselen]
  base && dir_at(seq[1..-1], subpos )+base
end

こんな感じ。
抂芁を先に蚈算する郜合で、 inject で掛け算しおいる。

䌚堎では、実際に線分の向きのリストを生成しおいる方が倚かった。
凊理系によっお 10秒で終わったり(Haskell)、2時間を超えたり(SQL)、゚ラヌで死んだり(node.js)ずさたざただった。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?