49 lines
1.5 KiB
Text
49 lines
1.5 KiB
Text
require "table2"
|
|
local fmt = require "fmt"
|
|
|
|
local btoi = |b| -> b ? 1 : 0
|
|
|
|
local predicates = {
|
|
|s| -> #s == 12,
|
|
|s| -> range(7, 12):count(|i| -> s[i] == "1") == 3,
|
|
|s| -> {2, 4, 6, 8, 10, 12}:count(|i| -> s[i] == "1") == 2,
|
|
|s| -> s[5] == "0" or (s[6] == "1" and s[7] == "1"),
|
|
|s| -> s[2] == "0" and s[3] == "0" and s[4] == "0",
|
|
|s| -> {1, 3, 5, 7, 9, 11}:count(|i| -> s[i] == "1") == 4,
|
|
|s| -> fmt.itob(btoi(s[2] == "1") ~ btoi(s[3] == "1")),
|
|
|s| -> s[7] == "0" or (s[5] == "1" and s[6] == "1"),
|
|
|s| -> range(1, 6):count(|i| -> s[i] == "1") == 3,
|
|
|s| -> s[11] == "1" and s[12] == "1",
|
|
|s| -> range(7, 9):count(|i| -> s[i] == "1") == 1,
|
|
|s| -> range(1, 11):count(|i| -> s[i] == "1") == 4
|
|
}
|
|
|
|
local function show(s, indent)
|
|
if indent then io.write(" ") end
|
|
for i = 1, #s do
|
|
if s[i] == "1" then io.write($"{i} ") end
|
|
end
|
|
print()
|
|
end
|
|
|
|
print("Exact hits:")
|
|
for i = 0, 4095 do
|
|
local s = fmt.lpad(fmt.bin(i), 12, "0")
|
|
local j = 1
|
|
if predicates:checkall(|pred| -> pred(s) == (s[j++] == "1")) then show(s, true) end
|
|
end
|
|
|
|
print("\nNear misses:")
|
|
for i = 0, 4095 do
|
|
local s = fmt.lpad(fmt.bin(i), 12, "0")
|
|
local j = 1
|
|
if predicates:count(|pred| -> pred(s) == (s[j++] == "1")) == 11 then
|
|
local k = 1
|
|
for predicates as pred do
|
|
if pred(s) != (s[k] == "1") then break end
|
|
k += 1
|
|
end
|
|
fmt.write(" (Fails at statement %2d) ", k)
|
|
show(s, false)
|
|
end
|
|
end
|