Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change digits/0 into a -define

  • Loading branch information...
commit ac9d7b8cdb47a5ad9ef9637d6011278765e32378 1 parent 1852c68
@apauley authored
Showing with 10 additions and 11 deletions.
  1. +7 −8 sudoku.erl
  2. +3 −3 unittests.erl
View
15 sudoku.erl
@@ -2,16 +2,16 @@
-import(lists, [member/2, filter/2, map/2, flatmap/2, sort/1, all/2, sum/1]).
-compile(export_all).
+-define(digits, "123456789").
+
cross(SeqA, SeqB) ->
%% Cross product of elements in SeqA and elements in SeqB.
[[X,Y] || X <- SeqA, Y <- SeqB].
-digits() ->
- "123456789".
rows() ->
"ABCDEFGHI".
cols() ->
- digits().
+ ?digits.
squares() ->
%% Returns a list of 81 square names, including "A1" etc.
@@ -54,14 +54,14 @@ parse_grid(GridString) ->
clean_grid(GridString) ->
%% Return a string with only digits, 0 and .
- ValidChars = digits() ++ "0.",
+ ValidChars = ?digits ++ "0.",
filter(fun(E) -> member(E, ValidChars) end, GridString).
parse_puzzle(Puzzle, [], []) ->
Puzzle;
parse_puzzle(Puzzle, [Square|Squares], [Value|GridString]) ->
{_,_} = Puzzle,
- IsDigit = member(Value, digits()),
+ IsDigit = member(Value, ?digits),
NewPuzzle = assign_if_digit(Puzzle, Square, Value, IsDigit),
{_,_} = NewPuzzle,
parse_puzzle(NewPuzzle, Squares, GridString).
@@ -76,8 +76,7 @@ assign_if_digit(Puzzle, _, _, false) ->
empty_puzzle() ->
{empty_dict(), 0}.
empty_dict() ->
- Digits = digits(),
- dict:from_list([{Square, Digits} || Square <- squares()]).
+ dict:from_list([{Square, ?digits} || Square <- squares()]).
assign(Puzzle, Square, Digit) ->
%% Assign by eliminating all values except the assigned value.
@@ -204,7 +203,7 @@ is_solved(Puzzle) ->
is_unit_solved(Puzzle, Unit) ->
UnitValues = flatmap(fun(S) -> values(Puzzle, S) end, Unit),
(length(UnitValues) == 9)
- and (gb_sets:from_list(UnitValues) == gb_sets:from_list(digits())).
+ and (gb_sets:from_list(UnitValues) == gb_sets:from_list(?digits)).
to_string(Puzzle) ->
{ValuesDict, _} = Puzzle,
View
6 unittests.erl
@@ -1,6 +1,6 @@
-module(unittests).
-import(lists, [all/2, member/2, sort/1]).
--import(sudoku, [cross/2, digits/0, values/2,
+-import(sudoku, [cross/2, values/2,
squares/0, col_squares/0, row_squares/0, box_squares/0,
unitlist/0, units/1, peers/1, search/1,
least_valued_unassigned_square/1, stats/1,
@@ -87,7 +87,7 @@ test_empty_puzzle() ->
%% The values of all keys should start with all possible values.
Squares = squares(),
- Digits = digits(),
+ Digits = "123456789",
true = all(fun(Values) -> Values == Digits end,
[values(Puzzle, Square) || Square <- Squares]),
ok.
@@ -132,7 +132,7 @@ test_eliminate() ->
"2457" = values(NewPuzzle, "A2"),
%% Eliminating the last value from a square should indicate an error
- {false, _} = eliminate_digits(Puzzle, "A2", digits()),
+ {false, _} = eliminate_digits(Puzzle, "A2", "123456789"),
ok.
test_search_bails_out_early() ->
Please sign in to comment.
Something went wrong with that request. Please try again.