Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'pp'
- a = %w(2 3 5 1 0 8 4 7 6)
- b = %w(2 3 5 0 1 8 4 7 6)
- def findMove(beforeArray, afterArray)
- initialindex = beforeArray.index('0')
- afterindex = afterArray.index('0')
- puts initialindex
- puts afterindex
- if afterindex < initialindex
- if afterindex == (initialindex - 1) #left move
- return "r#{beforeArray[afterindex]}"
- else #more than 1 less, up move
- return "d#{beforeArray[afterindex]}"
- end
- else
- if afterindex == (initialindex + 1) #right move
- return "l#{beforeArray[afterindex]}"
- else #more than one more, down move
- return "u#{beforeArray[afterindex]}"
- end
- end
- end
- #puts findMove(a, b)
- #pass a file; method reads through file and creates a hash of arrays
- def createHash(file)
- i = 0
- hash = {}
- fileinput = IO.readlines(file)
- array = []
- fileinput.each do |line|
- if line.length > 1
- line.split("\n")[0].split.each do |number|
- array.push(number.to_i)
- end
- else #newline, end of puzzle
- hash[i] = array
- array = []
- i++
- end
- end
- return hash
- end
- pp createHash("solvedpuzzle")
Advertisement
Add Comment
Please, Sign In to add comment