Last active
August 29, 2015 14:19
-
-
Save tom-galvin/92b8ec12b5302d655d38 to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge #211i Solution (Ogre Maze)
This file contains hidden or 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
o_swamp = Array.new(10) { gets.strip.chars } | |
swamp = o_swamp.map {|r| r.clone} | |
start=(0..9).map {|y| (0..9).map {|x| [x, y]}}.flatten(1).select{|(x, y)| swamp[y][x]=='@'}.first | |
(puts "No starting location."; exit) if start == nil | |
(0..9).map {|y| (0..9).map {|x| | |
swamp[y][x] = 'N' if (x >= 9 || y >= 9 || [0, 1].product([0, 1]).any? {|(i, j)| | |
swamp[y + j][x + i] == 'O' })}} | |
queue = {start => []} | |
visited = [start] | |
until queue.empty? | |
x, y, *path = queue.shift.flatten | |
if [0, 1].product([0, 1]).any?{|(i, j)| o_swamp[y+j][x+i] == '$'} | |
path_coords = (path << x << y).each_slice(2) | |
o_swamp.each.with_index {|l, y| puts l.map.with_index {|c, x| | |
[0, -1].product([0, -1]).map {|(i, j)| [x + i, y + j]} | |
.any? {|c| path_coords.include?(c)} ? '&' : c }.join ''} | |
exit | |
end | |
[[0, 1], [0, -1], [1, 0], [-1, 0]] | |
.reject {|(i, j)| x+i < 0 || x+i > 9 || y+j < 0 || y+j > 9 || | |
swamp[y + j][x + i] == 'N' || | |
visited.include?([x + i, y + j])} | |
.each {|(i, j)| visited << [x+i, y+j]; queue[[x+i, y+j]] = path + [x, y]} | |
end | |
puts "No path to gold" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment