Anonymous user
Cheryl's birthday: Difference between revisions
m
→{{header|Lua}}
m (→{{header|Lua}}) |
m (→{{header|Lua}}) |
||
Line 1,672:
local choices = {
Date(
Date(
Date(
Date(
}
local function apply(t, f)
for k, v in
f(k, v)
end
end
local function filter(t, f)
local result = {}
for k, v in
if f(k, v) then
result[#result+1] = v
end
end▼
return result
local function map(t, f)
local result = {}
for k, v in ipairs(t) do
result[#result+1] = f(k, v)
end
return result
Line 1,695 ⟶ 1,703:
local function count(t) return #t end
local function isvalid(k, v) return v.valid end
local function invalidate(k, v) v.valid = false end
local function remaining() return filter(choices, isvalid) end
local function listValidChoices()
▲ apply(remaining(), function(k,v) io.write(v.mon.."/"..v.day..", ") end)
▲ print("\n")
end
Line 1,709 ⟶ 1,716:
print("1) Albert knows that Bernard also cannot yet know, so cannot be a month with a unique day, leaving:")
apply(remaining(), function(k, v)
if count(filter(choices, function(k2, v2) return v.day==v2.day end)) == 1 then
apply(filter(
end
end)
Line 1,717 ⟶ 1,724:
print("2) After Albert's revelation, Bernard now knows, so day must be unique, leaving:")
apply(remaining(), function(k, v)
if
▲ end
end)
listValidChoices()
print("3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only:")
apply(remaining(), function(k, v)
if
▲ end
end)
listValidChoices()</lang>
{{out}}
<pre>Cheryl offers these ten choices:
1) Albert knows that Bernard also cannot yet know, so cannot be a month with a unique day, leaving:
2) After Albert's revelation, Bernard now knows, so day must be unique, leaving:
3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only:
=={{header|Perl}}==
|