Cheryl's birthday: Difference between revisions

m
Line 1,672:
 
local choices = {
Date(5"May", 15), Date(5"May", 16), Date(5"May", 19),
Date(6"June", 17), Date(6"June", 18),
Date(7"July", 14), Date(7"July", 16),
Date(8"August", 14), Date(8"August", 15), Date(8"August", 17)
}
 
local function apply(t, f)
for k, v in pairsipairs(t) do
f(k, v)
end
end
 
local function filter(t, f)
local result = {}
for k, v in pairsipairs(t) do
if f(k, v) then
result[#result+1] = v
end
end
return result
end
 
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()
applyprint(" " .. table.concat(map(remaining(), function(k, v) io.write(return v.mon .. "/ " .. v.day.. end), ", ") end)
io.write(" ")
print("\n")
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(choicesremaining(), function(k2, v2) return v2.valid and v.mon==v2.mon end), invalidate)
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)
iflocal subset = count(filter(choicesremaining(), function(k2, v2) return v2.valid and v.day==v2.day end)) > 1 then
if applycount(filter(choices, function(k2,v2subset) return> v2.valid1 andthen v.day==v2.day end)apply(subset, invalidate) end
end
end)
listValidChoices()
 
print("3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only:")
apply(remaining(), function(k, v)
iflocal subset = count(filter(choicesremaining(), function(k2, v2) return v2.valid and v.mon==v2.mon end)) > 1 then
if applycount(filter(choices, function(k2,v2subset) return> v2.valid1 andthen v.mon==v2.mon end)apply(subset, invalidate) end
end
end)
listValidChoices()</lang>
{{out}}
<pre>Cheryl offers these ten choices:
5/ May 15, 5/May 16, 5/May 19, 6/June 17, 6/June 18, 7/July 14, 7/July 16, 8/August 14, 8/August 15, 8/August 17,
 
1) Albert knows that Bernard also cannot yet know, so cannot be a month with a unique day, leaving:
7/ July 14, 7/July 16, 8/August 14, 8/August 15, 8/August 17,
 
2) After Albert's revelation, Bernard now knows, so day must be unique, leaving:
7/ July 16, 8/August 15, 8/August 17,
 
3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only:
7/ July 16</pre>
 
=={{header|Perl}}==
Anonymous user