September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,18 +1,18 @@
func dinesman(problem) {
var lines = problem.split('.');
var names = lines.first.scan(/\b[A-Z]\w*/);
var re_names = Regex(names.join('|'));
var lines = problem.split('.')
var names = lines.first.scan(/\b[A-Z]\w*/)
var re_names = Regex(names.join('|'))
 
# Later on, search for these keywords (the word "not" is handled separately).
var words = %w(first second third fourth fifth sixth seventh eighth ninth tenth
bottom top higher lower adjacent);
var re_keywords = Regex(words.join('|'));
bottom top higher lower adjacent)
var re_keywords = Regex(words.join('|'))
 
# Build an array of lambda's
var predicates = lines.ft(1, lines.end-1).map{ |line|
var keywords = line.scan(re_keywords);
var (name1, name2) = line.scan(re_names)...;
var keywords = line.scan(re_keywords)
var (name1, name2) = line.scan(re_names)...
 
keywords.map{ |keyword|
var l = do {
given(keyword) {
@ -24,11 +24,11 @@ func dinesman(problem) {
default { ->(c) { c[words.index(keyword)] == name1 } }
}
}
line ~~ /\bnot\b/ ? func(c) { l(c) -> not } : l; # handle "not"
line ~~ /\bnot\b/ ? func(c) { l(c) -> not } : l; # handle "not"
}
}.flatten;
names.permutations { |candidate|
predicates.all { |predicate| predicate(candidate) } && return candidate;
}.flat
 
names.permutations { |*candidate|
predicates.all { |predicate| predicate(candidate) } && return candidate
}
}

View file

@ -19,4 +19,4 @@ floor than does Cooper. Smith does not live on a floor
adjacent to Fletcher's. Fletcher does not live on a floor
adjacent to Cooper's. Where does everyone live?"
[demo1, demo2, problem1, problem2].each{|problem| say dinesman(problem).join("\n"); say '' };
[demo1, demo2, problem1, problem2].each{|problem| say dinesman(problem).join("\n"); say '' }

View file

@ -1,15 +1,15 @@
var names = %w(Baker Cooper Fletcher Miller Smith)
 
var predicates = [
->(c){ :Baker != c.last },
->(c){ :Cooper != c.first },
->(c){ (:Fletcher != c.first) && (:Fletcher != c.last) },
->(c){ :Baker != c.last },
->(c){ :Cooper != c.first },
->(c){ (:Fletcher != c.first) && (:Fletcher != c.last) },
->(c){ c.index(:Miller) > c.index(:Cooper) },
->(c){ (c.index(:Smith) - c.index(:Fletcher)).abs != 1 },
->(c){ (c.index(:Cooper) - c.index(:Fletcher)).abs != 1 },
->(c){ (c.index(:Smith) - c.index(:Fletcher)).abs != 1 },
->(c){ (c.index(:Cooper) - c.index(:Fletcher)).abs != 1 },
]
names.permutations { |candidate|
 
names.permutations { |*candidate|
if (predicates.all {|predicate| predicate(candidate) }) {
say candidate.join("\n")
break