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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 | #!/usr/bin/env lua
--[[
USAGE
as a cli program:
$ ./numbers.lua --help
as a module:
local numbers = require("numbers")
numbers.solve(
814, {2, 1, 3, 7, 6, 9, '+', '+', '*', '*', '-', '/'},
function(msg) -- optional
io.stderr:write(msg, '\n')
io.stderr:flush()
end,
true -- equivalent to --hard in cli, optional
) -- returns 813, {9, 7, 2, '-', '*', 6, '*', 1, '+', 3, '*'}
--]]
local function eval(op, vals, strict)
if type(op) == "number" then
return {op, vals}
else
local a, b, o
if not vals or not vals[2] then return end
vals, a, b = vals[2][2], vals[2][1], vals[1]
if strict and a < b then return end
local commutable = false
if op == '+' then
o = a + b
commutable = true
elseif op == '-' then
o = a - b
elseif op == '*' then
o = a * b
commutable = true
elseif op == '/' then
o = a / b
end
o = (math.tointeger and math.tointeger(o)) or o
if commutable and a < b then return end
if strict and o % 1 ~= 0 then return end
return {o, vals}
end
end
local function copy(t, o)
o = o or t
for k, v in pairs(t) do
o[k] = v
end
return o
end
local function flatten(stack)
local reversed = {}
while stack do
table.insert(reversed, stack[1])
stack = stack[2]
end
local out = {}
for n = #reversed, 1, -1 do
table.insert(out, reversed[n])
end
return out
end
local function stacklen(stack)
local n = 0
while stack do
n = n + 1
stack = stack[2]
end
return n
end
local function search(expr, vals, ops, write, strict)
for op, count in pairs(ops) do
local newvals = eval(op, vals, strict)
if newvals then
ops[op] = ops[op] - 1
ops[op] = ops[op] > 0 and ops[op] or nil
local newexpr = {op, expr}
if not newvals[2] then
write(newvals[1], newexpr)
end
search(newexpr, newvals, ops, write, strict)
ops[op] = count
end
end
end
local function findmax(target, log)
local maxdiff = math.huge
local maxlen = math.huge
local maxval = nil
local maxexpr = nil
return function(val, expr)
local diff = math.abs(target - val)
if diff <= maxdiff then
local len = stacklen(expr)
if diff < maxdiff or len <= maxlen then
log(string.format(
"... %s -> %s",
table.concat(flatten(expr), " "), val))
maxval, maxdiff, maxexpr, maxlen = val, diff, expr, len
end
end
end, function()
return maxval, flatten(maxexpr)
end
end
local numbers = {}
function numbers.solve(target, oplist, log, hard)
log = log or function() end
local ops, write, maxfunc = {}, findmax(target, log)
for _, op in pairs(oplist) do
ops[op] = (ops[op] or 0) + 1
end
search(nil, nil, ops, write, not hard)
return maxfunc()
end
do
local ok, info = pcall(function()
return debug.getinfo(5)
end)
if not ok or info then
return numbers
end
end
local usage = [[
Usage: numbers.lua [OPTION]... [FILE]
Solve a game of countdown.
With no FILE, or when FILE is -, read standard input.
-q, --quiet, --silent do not display intermediate results
-h, --hard allow fractions and negative numbers
--help display this help and exit
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/numbers.lua>
or available locally via: info '(coreutils) numbers.lua invocation'
]]
local file = io.stdin
local istty = os.execute("tty >/dev/null")
istty = (type(istty) == "boolean" and {istty} or {istty == 0})[1]
local log = function(msg)
io.stderr:write(msg, '\n')
io.stderr:flush()
end
local hard = false
local options = {
function(arg)
if arg == "-" then
file = io.stdin
else
file = assert(io.open(arg))
istty = false
end
end,
help = function()
io.stdout:write(usage)
os.exit(0)
end,
quiet = function()
log = function() end
istty = false
end,
hard = function()
hard = true
end,
}
local short_options = {
q = options.quiet,
h = options.hard,
}
local args = {...}
local args_done = false
for _, arg in ipairs(args) do
if not args_done then
if arg == "--" then
args_done = true
elseif string.sub(arg,1,2) == "--" and #arg>2 then
assert(options[string.sub(arg,3)], "unknown option: " .. arg)()
elseif string.sub(arg,1,1) == "-" and #arg>1 then
string.gsub(string.sub(arg,2), '.', function(char)
assert(short_options[char], "unknown option: -" .. char)()
end)
else
options[1](arg)
end
else
options[1](arg)
end
end
local function test(target, ops, hard)
local start = os.clock()
local val, expr = numbers.solve(target, ops, log, hard)
log(string.format("done in %.2fs", os.clock() - start))
print(string.format("%s -> %s", table.concat(expr," "), val))
end
local function input(prompt)
if istty then
io.stderr:write(prompt .. "> ")
io.stderr:flush()
end
return (assert(file:read(), "unexpected EOF"))
end
local function parse(str)
local t = {}
for v in string.gmatch(str, "%S+") do
v = tonumber(v) or v
table.insert(t, v)
end
return t
end
test(tonumber(input("target")), parse(input("ops")), hard)
|
post a comment