Skip to content

Commit

Permalink
Fix combinations for 11-sudoku-solver
Browse files Browse the repository at this point in the history
  • Loading branch information
itswindtw committed Mar 18, 2016
1 parent 25a8573 commit 15040a2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions 11-sudoku-solver.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule SudokuSolver do
board
|> solutions
|> map(fn s -> apply_solution(board, s) end)
|> find fn b -> SudokuBoard.solved?(b) end
|> find(fn b -> SudokuBoard.solved?(b) end)
end

@doc """
Expand Down Expand Up @@ -115,7 +115,7 @@ defmodule SudokuSolver do
def combinations([list | rest]) do
crest = combinations(rest)
for p <- permutations(list), r <- crest do
flat_map p, fn i -> [i | r] end
List.flatten([p | r])
end
end
def combinations([]), do: [[]]
Expand All @@ -142,6 +142,12 @@ defmodule SudokuSolverTest do

import SudokuSolver

test "correct behavior of combinations" do
assert combinations([[1]]) == [[1]]
assert combinations([[2, 3], [1]]) == [[2, 3, 1], [3, 2, 1]]
assert combinations([[1], [2], [3, 4]]) == [[1, 2, 3, 4], [1, 2, 4, 3]]
end

test "solves a small board" do
board = [[1, nil, 3 ],
[3, nil, 2 ],
Expand Down

0 comments on commit 15040a2

Please sign in to comment.