blob: 2aab75b383d5a5f35028ac11b751a44a331ccd30 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
def consume(path)
case path[0]
when ?L
[?L, path[1..-1]]
when ?R
[?R, path[1..-1]]
when /\d/
act = path.chars.take_while { |ch| ch =~ /\d/ }.join
l = act.length
[act.to_i, path[l..-1]]
end
end
input = $stdin.readlines.map(&:rstrip)
map = input[0..-3]
path = input[-1]
r = 0
c = input[0].index(?.)
d = ?R
until path.empty?
action, path = consume(path)
case action
when ?L
d = case d
when ?U; ?L
when ?R; ?U
when ?D; ?R
when ?L; ?D
end
when ?R
d = case d
when ?U; ?R
when ?R; ?D
when ?D; ?L
when ?L; ?U
end
else
action.times do
case d
when ?U
nr = r - 1
if nr < 0 || map[nr][c] == ?\s || map[nr][c].nil?
nr = map.length - 1
nr -= 1 until map[nr][c] == ?. || map[nr][c] == ?#
end
break if map[nr][c] == ?#
r = nr
when ?R
nc = c + 1
if nc >= map[r].length || map[r][nc] == ?\s || map[r][nc].nil?
nc = 0
nc += 1 until map[r][nc] == ?. || map[r][nc] == ?#
end
break if map[r][nc] == ?#
c = nc
when ?D
nr = r + 1
if nr >= map.length || map[nr][c] == ?\s || map[nr][c].nil?
nr = 0
nr += 1 until map[nr][c] == ?. || map[nr][c] == ?#
end
break if map[nr][c] == ?#
r = nr
when ?L
nc = c - 1
if nc < 0 || map[r][nc] == ?\s || map[r][nc].nil?
nc = map[r].length - 1
nc -= 1 until map[r][nc] == ?. || map[r][nc] == ?#
end
break if map[r][nc] == ?#
c = nc
end
end
end
end
puts 1000 * (r+1) + 4 * (c + 1) + case d
when ?R; 0
when ?D; 1
when ?L; 2
when ?U; 3
end
|