Skip to content

Commit

Permalink
Re-order functions to reflect the order of the Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pauley committed Mar 25, 2011
1 parent 400f12a commit faad89c
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions sudoku.erl
Expand Up @@ -2,6 +2,52 @@
-import(lists, [member/2, filter/2, map/2, flatmap/2, sort/1, all/2, sum/1]).
-compile(export_all).

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().

values(Puzzle, Square) ->
{Dict, _} = Puzzle,
dict:fetch(Square, Dict).

squares() ->
%% Returns a list of 81 square names, including "A1" etc.
cross(rows(), cols()).

col_squares() ->
%% All the square names for each column.
[cross(rows(), [C]) || C <- cols()].
row_squares() ->
%% All the square names for each row.
[cross([R], cols()) || R <- rows()].
box_squares() ->
%% All the square names for each box.
[cross(Rows, Cols) || Rows <- ["ABC", "DEF", "GHI"],
Cols <- ["123", "456", "789"]].

unitlist() ->
%% A list of all units (columns, rows, boxes) in a grid.
col_squares() ++ row_squares() ++ box_squares().

units(Square) ->
%% A list of units for a specific square
[S || S <- unitlist(), member(Square, S)].

peers(Square) ->
%% A unique list of squares (excluding this one)
%% that are also part of the units for this square.
NonUniquePeers = shallow_flatten([S || S <- units(Square)]),
PeerSet = sets:from_list(NonUniquePeers),
PeersWithSelf = sets:to_list(PeerSet),
lists:delete(Square, PeersWithSelf).

print_results(Filename, Seperator) ->
{Time, Solutions} = timer:tc(sudoku, solve_file, [Filename, Seperator]),
Solved = filter(fun(Puzzle) -> is_solved(Puzzle) end, Solutions),
Expand Down Expand Up @@ -183,52 +229,6 @@ empty_dict() ->
Digits = digits(),
dict:from_list([{Square, Digits} || Square <- squares()]).

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().

values(Puzzle, Square) ->
{Dict, _} = Puzzle,
dict:fetch(Square, Dict).

squares() ->
%% Returns a list of 81 square names, including "A1" etc.
cross(rows(), cols()).

col_squares() ->
%% All the square names for each column.
[cross(rows(), [C]) || C <- cols()].
row_squares() ->
%% All the square names for each row.
[cross([R], cols()) || R <- rows()].
box_squares() ->
%% All the square names for each box.
[cross(Rows, Cols) || Rows <- ["ABC", "DEF", "GHI"],
Cols <- ["123", "456", "789"]].

unitlist() ->
%% A list of all units (columns, rows, boxes) in a grid.
col_squares() ++ row_squares() ++ box_squares().

units(Square) ->
%% A list of units for a specific square
[S || S <- unitlist(), member(Square, S)].

peers(Square) ->
%% A unique list of squares (excluding this one)
%% that are also part of the units for this square.
NonUniquePeers = shallow_flatten([S || S <- units(Square)]),
PeerSet = sets:from_list(NonUniquePeers),
PeersWithSelf = sets:to_list(PeerSet),
lists:delete(Square, PeersWithSelf).

shallow_flatten([]) -> [];
shallow_flatten([H|T]) ->
H ++ shallow_flatten(T).
Expand Down

0 comments on commit faad89c

Please sign in to comment.