Skip to content

Instantly share code, notes, and snippets.

@jeantil
Created September 30, 2010 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeantil/605224 to your computer and use it in GitHub Desktop.
Save jeantil/605224 to your computer and use it in GitHub Desktop.
use("ispec")
;
; [0,0] => 0 bon bien place, 0 bon mal place
; [1,0] => 1 bon bien place, 0 bon mal place
; [1,1] => 1 bon bien place, 1 bon mal place
;
describe("evalm",
it("should return 0,0 when no pegs are good",
evalm([]("B","B","B","B"), []("G","G","G","G")) should == [](0,0)
)
; all non mentionned pegs are bad
it("should return 1,0 when the first peg is good and well placed",
evalm([]("G", "B","B","B"), []("G","G", "G","G")) should == [](1,0)
)
it("should return 1,0 when the second peg is good and well placed",
evalm([]("B", "G","B","B"), []("G","G", "G","G")) should == [](1,0)
)
it("should return 2,0 when both the first and second peg are good and well placed",
evalm([]("G", "G","B","B"), []("G","G", "G","G")) should == [](2,0)
)
; Stage 2
it("should return 0,1 when the first peg is misplaced",
evalm([]("M", "B","B","B"), []("G","M", "G","G")) should == [](0,1)
)
it("should return 0,2 when two pegs diff colors are misplaced",
evalm([]("M", "B","N","B"), []("G","M", "G","N")) should == [](0,2)
)
it("should return 0,2 when two pegs diff colors are misplaced",
evalm([]("M", "B","N","B"), []("G","M", "M","N")) should == [](0,2)
)
)
;("M","B","N","B") [0]
;("G","M","M","N") [1]
evalm=method(guess, secret,
sorted=guess zip(secret) groupBy(inject(==))
good= (sorted[true]||[]) count
notgood=sorted[false]||[]
colors=notgood map:set(x,x[0])
bad=colors map( c, countC(c,notgood) min ) sum
[](good,bad)
)
countC=method(c, list,
[](
list filter([0]==c) count,
list filter([1]==c) count
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment