1
2

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.

第13回オフラインリアルタイムどう書くの参考問題をFregeで解く

Posted at

第13回 オフラインリアルタイムどう書くの参考問題「増やす減らす二倍する」を、JVM上で動作するHaskellライクな言語Frege(フレーゲ)で解きました。

shortest.fr
shortestPath' :: Int -> Int -> Int
shortestPath' n maxn
  | n==0 = 0
  | n==1 = 1
  | n > maxn = maxBound::Int
  | even(n) = (shortestPath' (n `div` 2) (maxn-2)) + 1
  | otherwise = minimum [(shortestPath' (n-1) (maxn-2)) + 1, (shortestPath' (n+1) (maxn-2)) + 1]

shortestPath :: Int -> Int  
shortestPath n = shortestPath' n (n*2)
  

main args = do
  println (shortestPath 59 ==  9)
  println (shortestPath 10 ==  5)
  println (shortestPath 11 ==  6)
  println (shortestPath 12 ==  5)
  println (shortestPath 13 ==  6)
  println (shortestPath 14 ==  6)
  println (shortestPath 15 ==  6)
  println (shortestPath 16 ==  5)
  println (shortestPath 17 ==  6)
  println (shortestPath 18 ==  6)
  println (shortestPath 27 ==  8)
  println (shortestPath 28 ==  7)
  println (shortestPath 29 ==  8)
  println (shortestPath 30 ==  7)
  println (shortestPath 31 ==  7)
  println (shortestPath 32 ==  6)
  println (shortestPath 33 ==  7)
  println (shortestPath 34 ==  7)
  println (shortestPath 35 ==  8)
  println (shortestPath 41 ==  8)
  println (shortestPath 71 ==  9)
  println (shortestPath 1023 ==  12)
  println (shortestPath 1024 ==  11)
  println (shortestPath 1025 ==  12)
  println (shortestPath 1707 ==  17)
  println (shortestPath 683 ==  15)
  println (shortestPath 123 ==  10)
  println (shortestPath 187 ==  11)
  println (shortestPath 237 ==  12)
  println (shortestPath 5267 ==  18)
  println (shortestPath 6737 ==  18)
  println (shortestPath 14796 ==  20)
  println (shortestPath 18998 ==  20)
  println (shortestPath 23820 ==  20)
  println (shortestPath 30380 ==  21)
  println (shortestPath 31119 ==  21)
  println (shortestPath 33301 ==  20)
  println (shortestPath 33967 ==  21)
  println (shortestPath 35443 ==  22)
  println (shortestPath 35641 ==  22)
  println (shortestPath 43695 ==  23)
  println (shortestPath 44395 ==  23)
  println (shortestPath 44666 ==  22)
  println (shortestPath 987 ==  14)
  println (shortestPath 1021 ==  13)
  println (shortestPath 1019 ==  13)
  println (shortestPath 1015 ==  13)
  println (shortestPath 1007 ==  13)
  println (shortestPath 1011 ==  14)
  println (shortestPath 1003 ==  14)
  println (shortestPath 983 ==  14)
  println (shortestPath 999 ==  14)
  println (shortestPath 2731 ==  18)
  println (shortestPath 6827 ==  20)
  println (shortestPath 10923 ==  21)
  println (shortestPath 27307 ==  23)
  println (shortestPath 43691 ==  24)
  println (shortestPath 109227 ==  26)
  println (shortestPath 174763 ==  27)
  
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?