/
22b.rb
106 lines (98 loc) · 1.94 KB
/
22b.rb
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require_relative 'skim'
mapp = Skim.read(rec: false).rectangularize(' ')
traced = mapp.dup
dirs = ARGF.gets
DIRS = [
[1, 0], # right
[0, 1], # down
[-1, 0], # left
[0, -1] # up
]
MARKS = %w[> v < ^]
y = 0
x = mapp.data[0].find_index{_1 != ' '}
a = 0
# this is not at all generalized, sry. won't even work on the example
def wrap_around_cube(x, y, a)
qx, rx = x.divmod 50
qy, ry = y.divmod 50
case [qx, qy]
when [1, 0]
case a
when 2 then [0, 149 - ry, 0]
when 3 then [0, 150 + rx, 0]
else
raise "nuts!"
end
when [2, 0]
case a
when 0 then [99, 149 - ry, 2]
when 1 then [99, 50 + rx, 2]
when 3 then [rx, 199, 3]
else
raise "zounds"
end
when [1, 1]
case a
when 0 then [100 + ry, 49, 3]
when 2 then [ry, 100, 1]
else
raise "oook"
end
when [0, 2]
case a
when 2 then [50, 49 - ry, 0]
when 3 then [50, 50 + rx, 0]
else
raise "crap"
end
when [1, 2]
case a
when 0 then [149, 49 - ry, 2]
when 1 then [49, 150 + rx, 2]
else
raise ":("
end
when [0, 3]
case a
when 0 then [50 + ry, 149, 3]
when 1 then [100 + rx, 0, 1]
when 2 then [50 + ry, 0, 1]
else
raise "foo"
end
else
puts "!! #{x}, #{y}, #{a} #{MARKS[a]}"
raise "whoops"
end
end
dirs.scan(/\d+|L|R/).each do |dir|
print dir + ": "
case dir
when 'L'
a = (a - 1) % 4
when 'R'
a = (a + 1) % 4
else
dist = dir.to_i
dist.times do
a0 = a
dx, dy = DIRS[a]
if !mapp.in_bounds?(x + dx, y + dy) || mapp[x + dx, y + dy] == ' '
tx, ty, a = wrap_around_cube(x, y, a)
else
tx, ty = x + dx, y + dy
end
if mapp[tx, ty] == '#'
a = a0
break
end
traced[x, y] = MARKS[a0]
x, y = tx, ty
end
end
traced[x, y] = MARKS[a]
#traced.print
puts "#{x}, #{y}, #{a} #{MARKS[a]}"
end
puts "x=#{x}, y=#{y}, a=#{a} ans=#{1000*(y+1)+4*(x+1)+a}"