Last active
August 29, 2015 14:14
-
-
Save torazuka/7a9755dc4bf944977f6c to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書く#28 十字の壁がそそり立つ世界の中を君は螺旋状に歩く Ruby解答
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
#!/usr/bin/ruby | |
# http://nabetani.sakura.ne.jp/hena/ord28spirwa/ | |
def parse(input) | |
wall, day = input.split(":") | |
n, e, s, w = wall.split(",") | |
rs = [0,0] | |
1.upto(n.to_i) { |y| rs << [0, y] } | |
1.upto(e.to_i) { |x| rs << [x, 0] } | |
1.upto(s.to_i) { |y| rs << [0, -y] } | |
1.upto(w.to_i) { |x| rs << [-x, 0] } | |
return rs, day | |
end | |
def check(wall, n, e, s, w) | |
if ([e,w,n] - wall).empty? | |
return s, "S" | |
elsif ([n,e,s] - wall).empty? | |
return w, "W" | |
elsif ([e,s,w] - wall).empty? | |
return n, "N" | |
elsif ([n,s,w] - wall).empty? | |
return e, "E" | |
elsif ([s,w] - wall).empty? | |
return e, "E" | |
elsif ([n,w] - wall).empty? | |
return s, "S" | |
elsif ([n,e] - wall).empty? | |
return w, "W" | |
elsif ([e,s] - wall).empty? | |
return n, "N" | |
elsif wall.include?(n) | |
return w, "W" | |
elsif wall.include?(e) | |
return n, "N" | |
elsif wall.include?(s) | |
return e, "E" | |
elsif wall.include?(w) | |
return s, "S" | |
end | |
return nill | |
end | |
def walk(wall, posi) | |
x, y = posi[0], posi[1] | |
n, e, s, w = [x, y+1], [x+1, y], [x, y-1], [x-1, y] | |
nxt, ch = check(wall, n, e, s, w) | |
wall << nxt | |
return wall, nxt, ch | |
end | |
def solve(wall, day) | |
posi = [1, 1] | |
wall << posi | |
ch = "" | |
0.upto(day) {|d| | |
wall, posi, ch = walk(wall, posi) | |
} | |
return ch | |
end | |
DATA.each{|line| | |
num, input, expected = line.split( /\s+/ ) | |
wall, day = parse(input) | |
actual = solve(wall, day.to_i) | |
error = (actual == expected) ? "" : " ** expected is : #{expected} **" | |
puts "%s -> %s %s" % [input, actual, error] | |
} | |
__END__ | |
#0 2,3,5,4:85 S | |
#1 1,2,3,4:1 E | |
#2 1,2,3,4:2 S |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment