-
Notifications
You must be signed in to change notification settings - Fork 429
/
10-sudoku-board.exs
58 lines (48 loc) · 1.17 KB
/
10-sudoku-board.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
defmodule SudokuBoard do
@moduledoc "Functions to interrogate a board."
import Enum
@doc "Returns true if the board is solved."
def solved?(board) do
rows_solved?(board) and cols_solved?(board)
end
@doc "Returns true if all rows are solved."
def rows_solved?([row | rest]) do
max = count(row)
sort(row) == to_list(1..max) and rows_solved?(rest)
end
def rows_solved?([]), do: true
@doc "Returns true if all columns are solved."
def cols_solved?(board) do
do_cols_solved?(board, count(board)-1)
end
defp do_cols_solved?(board, index) when index >= 0 do
max = count(board)
col = map board, fn row -> at(row, index) end
sort(col) == to_list(1..max) and do_cols_solved?(board, index-1)
end
defp do_cols_solved?(_, -1), do: true
end
ExUnit.start
defmodule SudokuBoardTest do
use ExUnit.Case
import SudokuBoard
test "solved? on solved board" do
board = [
[1, 2],
[2, 1]
]
assert solved?(board)
end
test "solved? on incorrectly solved board" do
board = [
[1, 1],
[2, 1]
]
assert not solved?(board)
board = [
[2, 1],
[2, 1]
]
assert not solved?(board)
end
end