-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21_keypad_conundrum.rb
174 lines (166 loc) · 3.37 KB
/
21_keypad_conundrum.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
nrobots = if narg = ARGV.find { |x| x.start_with?('-n') }
ARGV.delete(narg)
narg[2..].split(?,).map(&method(:Integer)).freeze
else
[2, 25].freeze
end
@dir_cache = {}
def expand_dir(seq, levels_left)
robot = :A
seq.sum { |button|
(@dir_cache[[robot, button, levels_left].freeze] ||= begin
subseq = DIRECTIONAL.fetch(robot).fetch(button)
if levels_left == 1
subseq.size
else
expand_dir(subseq, levels_left - 1)
end
end).tap { robot = button }
}
end
def expand_num(seq, levels)
robot = :A
v = seq.flat_map { |button|
(NUMERIC.fetch(robot).fetch(button)).tap { robot = button }
}.freeze
expand_dir(v, levels)
end
# we actually can just use one sequence of instructions for each source/destination:
# l should be pressed before u
# l should be pressed before d
# u should be pressed before r
# d should be pressed before r
#
# this is because l is expensive to reach from A.
# this affects robots two levels up:
#
# dllArruAldAlArruAdAAluArA
# l A d l A rr u A
# u l A
# as the two l's were separate (l A d l A),
# the robot two levels up had to use more moves.
#
# ldAlAArruAdAluArAdAuA
# d ll A r u A r A
# l u A
# as the two l's were together (d l l A),
# the robot two levels up could use fewer moves.
#
# ldAurAdllArruAlAdrAuA
# d A l A u r A
# r d A
#
# dllArAurAdAuAlArA
# l d A r A u A
# d r A
DIRECTIONAL = {
u: {
u: %i(),
d: %i(d),
l: %i(d l),
r: %i(d r),
A: %i(r),
},
d: {
u: %i(u),
d: %i(),
l: %i(l),
r: %i(r),
A: %i(u r),
},
l: {
u: %i(r u),
d: %i(r),
l: %i(),
r: %i(r r),
A: %i(r r u),
},
r: {
u: %i(l u),
d: %i(l),
l: %i(l l),
r: %i(),
A: %i(u),
},
A: {
u: %i(l),
d: %i(l d),
l: %i(d l l),
r: %i(d),
A: %i(),
},
}.each_value { |v| v.each_value { |v2| (v2 << :A).freeze }.freeze }.freeze
# Anything *not* in here is unverified;
# I think it would be a bad idea to add in unverified data.
NUMERIC = {
0 => {
2 => %i(u),
8 => %i(u u u),
A: %i(r),
},
1 => {
2 => %i(r),
7 => %i(u u),
},
2 => {
4 => %i(l u),
8 => %i(u u),
9 => %i(u u r),
},
3 => {
7 => %i(l l u u),
8 => %i(l u u),
A: %i(d),
},
4 => {
5 => %i(r),
6 => %i(r r),
},
5 => {
0 => %i(d d),
2 => %i(d),
6 => %i(r),
9 => %i(u r),
A: %i(d d r),
},
6 => {
A: %i(d d),
},
7 => {
0 => %i(r d d d),
6 => %i(d r r),
9 => %i(r r),
},
8 => {
0 => %i(d d d),
5 => %i(d),
6 => %i(d r),
A: %i(d d d r),
},
9 => {
3 => %i(d d),
8 => %i(l),
A: %i(d d d),
},
A: {
0 => %i(l),
1 => %i(u l l),
2 => %i(l u),
3 => %i(u),
4 => %i(u u l l),
5 => %i(l u u),
9 => %i(u u u),
},
}.each_value { |v| v.each_value { |v2| (v2 << :A).freeze }.freeze }.freeze
verbose = ARGV.delete('-v')
codes = (ARGV.all?(/\A\d+A\z/) ? ARGV.map(&:dup) : ARGF).map { |line|
line.chomp!
raise "bad #{line}" unless line.match?(/\A\d+A\z/)
line.delete_suffix!(?A)
[Integer(line, 10), (line.each_char.map(&method(:Integer)) << :A).freeze].freeze
}.freeze
nrobots.each { |levels|
len = codes.map { |n, c| expand_num(c, levels) }.freeze
p len.zip(codes).map { |l, (n, _)| [l, n, l * n] } if verbose
puts len.zip(codes).sum { |l, (n, _)| l * n }
}