Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,5 +1,5 @@
{{omit from|GUISS}}
The task is to '''solve Dinesman's multiple dwelling [http://www-mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_4.3.2 problem] but in a way that most naturally follows the problem statement given below'''.
The task is to '''solve Dinesman's multiple dwelling [http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_4.3.2 problem] but in a way that most naturally follows the problem statement given below'''.
Solutions are allowed (but not required) to parse and interpret the problem text, but should remain flexible and should state what changes to the problem text are allowed. Flexibility and ease of expression are valued.

View file

@ -0,0 +1,27 @@
defmodule Dinesman do
def problem do
names = ~w( Baker Cooper Fletcher Miller Smith )a
predicates = [fn(c)-> :Baker != List.last(c) end,
fn(c)-> :Cooper != List.first(c) end,
fn(c)-> :Fletcher != List.first(c) && :Fletcher != List.last(c) end,
fn(c)-> floor(c, :Miller) > floor(c, :Cooper) end,
fn(c)-> abs(floor(c, :Smith) - floor(c, :Fletcher)) != 1 end,
fn(c)-> abs(floor(c, :Cooper) - floor(c, :Fletcher)) != 1 end]
permutation(names)
|> Enum.filter(fn candidate ->
Enum.all?(predicates, fn predicate -> predicate.(candidate) end)
end)
|> Enum.each(fn name_list ->
Enum.with_index(name_list)
|> Enum.each(fn {name,i} -> IO.puts "#{name} lives on #{i+1}" end)
end)
end
defp floor(c, name), do: Enum.find_index(c, fn x -> x == name end)
defp permutation([]), do: [[]]
defp permutation(list), do: (for x <- list, y <- permutation(list -- [x]), do: [x|y])
end
Dinesman.problem

View file

@ -1,5 +1,5 @@
# Contains only five floors. 5! = 120 permutations.
for [1..5].permutations -> $b, $c, $f, $m, $s {
for (flat (1..5).permutations) -> $b, $c, $f, $m, $s {
say "Baker=$b Cooper=$c Fletcher=$f Miller=$m Smith=$s"
if $b != 5 # Baker !live on top floor.
and $c != 1 # Cooper !live on bottom floor.

View file

@ -1,15 +1,21 @@
#!/bin/bash
# NAMES is a list of names. It can be changed as needed. It can be more than five names, or less.
NAMES=(Baker Cooper Fletcher Miller Smith)
# CRITERIA are the rules imposed on who lives where. Each criterion must be a valid bash expression
# that will be evaluated. TOP is the top floor; BOTTOM is the bottom floor.
# The CRITERIA can be changed to create different rules.
CRITERIA=(
'Baker != TOP'
'Cooper != BOTTOM'
'Fletcher != TOP'
'Fletcher != BOTTOM'
'Miller > Cooper'
'$(abs $(( Smith - Fletcher )) ) > 1'
'$(abs $(( Fletcher - Cooper )) ) > 1'
'Baker != TOP' # Baker does not live on the top floor
'Cooper != BOTTOM' # Cooper does not live on the bottom floor
'Fletcher != TOP' # Fletcher does not live on the top floor
'Fletcher != BOTTOM' # and Fletch also does not live on the bottom floor
'Miller > Cooper' # Miller lives above Cooper
'$(abs $(( Smith - Fletcher )) ) > 1' # Smith and Fletcher are not on adjacent floors
'$(abs $(( Fletcher - Cooper )) ) > 1' # Fletcher and Cooper are not on adjacent floors
)
# Code below here shouldn't need to change to vary parameters
@ -17,11 +23,7 @@ let BOTTOM=0
let TOP=${#NAMES[@]}-1
# Not available as a builtin
function abs {
let n=$1
if (( n < 0 )); then let n=-n; fi
echo "$n"
}
abs() { local n=$(( 10#$1 )) ; echo $(( n < 0 ? -n : n )) ; }
# Algorithm we use to iterate over the permutations
# requires that we start with the array sorted lexically