Last active
December 14, 2015 04:49
-
-
Save daiksy/5031305 to your computer and use it in GitHub Desktop.
ドラクエ7のバロックタワーのパズルを解くためのコード。
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
// コードを読みやすいように名前をつけとこう. | |
val Up = 0 | |
val Right = 1 | |
val Down = 2 | |
val Left = 3 | |
type Direction = Int | |
/* | |
* 石像をあらわすcase class. 像の向きを状態として持つ. | |
* また、一回の動作で向きが90度回転するという振る舞いも定義. | |
* moveメソッドは動きをわかりやすくするためにあえてゴリッと書いてる. | |
*/ | |
case class StoneStatue(var direction: Direction) { | |
def move: Unit = { | |
direction match { | |
case Up => direction = Right | |
case Right => direction = Down | |
case Down => direction = Left | |
case Left => direction = Up | |
} | |
} | |
} | |
// 4体の石像と、その状態の初期化メソッドを定義. | |
val A = StoneStatue(Up) | |
val B = StoneStatue(Up) | |
val C = StoneStatue(Up) | |
val D = StoneStatue(Up) | |
def initializeStatues = { | |
A.direction = Right | |
B.direction = Left | |
C.direction = Up | |
D.direction = Up | |
} | |
// ボタンを押した時の動作を定義. | |
def button0: Unit = { | |
A.move; C.move; D.move | |
} | |
def button1: Unit = { | |
A.move; B.move; D.move | |
} | |
def button2: Unit = { | |
B.move; C.move; D.move | |
} | |
def button3: Unit = { | |
A.move; B.move; C.move | |
} | |
type RunOrder = List[Int] | |
// (0)から(3,3,3,3,3,3)までのすべての組み合わせのリストを作る. | |
// ただし,組み合わせに使用できる数字は"0,1,2,3"の4種. | |
// 我ながら無茶苦茶やなこのコードw | |
val runOrders:List[RunOrder] = (0 to 333333).toList.map(_.toString.toList.filter(c => c == '0' || c == '1' || c == '2' || c == '3').map(_.getNumericValue)).filter(_ != Nil) | |
/* | |
* 実行指示のリストを受け取り、正解の像の向きの組み合わせを抽出する. | |
* 実行指示の中に正解が無ければNone. | |
*/ | |
def solve(runOrders: List[RunOrder]): Option[RunOrder] = { | |
// 最終的に正解となる像の状態を定義. | |
val answer = List(Down, Left, Up, Right) | |
// 4体の像をリストとして表現.(answerとの比較用) | |
val statues = List(A, B, C, D) | |
runOrders.find{ xs => | |
initializeStatues | |
xs.foreach { n => | |
n match { | |
case 0 => button0 | |
case 1 => button1 | |
case 2 => button2 | |
case 3 => button3 | |
} | |
} | |
statues.map(_.direction) == answer | |
} | |
} | |
println(solve(runOrders)) |
Author
daiksy
commented
Feb 25, 2013
- 参考:http://d.hatena.ne.jp/ayato0211/20130221/1361438221
- 問題自体はこちらを参照:http://d.hatena.ne.jp/torazuka/20130221/ddd
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment