Cheryl's birthday: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Promote to task, little controversy (about the actual task at least) and lots of examples) |
|||
Line 1,663: | Line 1,663: | ||
Cheryl's birthday is July 16 |
Cheryl's birthday is July 16 |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
<lang lua>-- Cheryl's Birthday in Lua 6/15/2020 db |
|||
local function Date(mon,day) |
|||
return { mon=mon, day=day, valid=true } |
|||
end |
|||
local choices = { |
|||
Date(5,15), Date(5,16), Date(5,19), |
|||
Date(6,17), Date(6,18), |
|||
Date(7,14), Date(7,16), |
|||
Date(8,14), Date(8,15), Date(8,17) |
|||
} |
|||
local function apply(t,f) |
|||
for k,v in pairs(t) do |
|||
f(k,v) |
|||
end |
|||
end |
|||
local function filter(t,f) |
|||
local result = {} |
|||
for k,v in pairs(t) do |
|||
if f(k,v) then |
|||
result[#result+1] = v |
|||
end |
|||
end |
|||
return result |
|||
end |
|||
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() |
|||
io.write(" ") |
|||
apply(filter(choices, isvalid), function(k,v) io.write(v.mon.."/"..v.day..", ") end) |
|||
print("\n") |
|||
end |
|||
print("Cheryl offers these ten choices:") |
|||
remaining() |
|||
print("1a) Albert knows month but not day, so month cannot be unique, obviously/redundantly, leaving still:") |
|||
apply(filter(choices, isvalid), function(k,v) |
|||
if count(filter(choices, function(k2,v2) return v2.valid and v.mon==v2.mon end)) == 1 then |
|||
apply(filter(choices, function(k2,v2) return v2.valid and v.mon==v2.mon end), invalidate) |
|||
end |
|||
end) |
|||
remaining() |
|||
print("1b) Albert also knows that Bernard cannot yet know, so cannot be a month with a unique day, leaving:") |
|||
apply(filter(choices, isvalid), function(k,v) |
|||
if count(filter(choices, function(k2,v2) return v.day==v2.day end)) == 1 then |
|||
apply(filter(choices, function(k2,v2) return v2.valid and v.mon==v2.mon end), invalidate) |
|||
end |
|||
end) |
|||
remaining() |
|||
print("2) After Albert's revelation, Bernard now knows, so day must be unique, leaving:") |
|||
apply(filter(choices, isvalid), function(k,v) |
|||
if count(filter(choices, function(k2,v2) return v2.valid and v.day==v2.day end)) > 1 then |
|||
apply(filter(choices, function(k2,v2) return v2.valid and v.day==v2.day end), invalidate) |
|||
end |
|||
end) |
|||
remaining() |
|||
print("3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only:") |
|||
apply(filter(choices, isvalid), function(k,v) |
|||
if count(filter(choices, function(k2,v2) return v2.valid and v.mon==v2.mon end)) > 1 then |
|||
apply(filter(choices, function(k2,v2) return v2.valid and v.mon==v2.mon end), invalidate) |
|||
end |
|||
end) |
|||
remaining()</lang> |
|||
{{out}} |
|||
<pre>Cheryl offers these ten choices: |
|||
5/15, 5/16, 5/19, 6/17, 6/18, 7/14, 7/16, 8/14, 8/15, 8/17, |
|||
1a) Albert knows month but not day, so month cannot be unique, obviously/redundantly, leaving still: |
|||
5/15, 5/16, 5/19, 6/17, 6/18, 7/14, 7/16, 8/14, 8/15, 8/17, |
|||
1b) Albert also knows that Bernard cannot yet know, so cannot be a month with a unique day, leaving: |
|||
7/14, 7/16, 8/14, 8/15, 8/17, |
|||
2) After Albert's revelation, Bernard now knows, so day must be unique, leaving: |
|||
7/16, 8/15, 8/17, |
|||
3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only: |
|||
7/16,</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |