Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Emacs spacing

  • Loading branch information...
commit 0cd5cc4b631c905e69e686acee54f1f517d02b48 1 parent 070c6e6
Andreas Pauley authored
Showing with 49 additions and 49 deletions.
  1. +49 −49 sudoku.erl
98 sudoku.erl
View
@@ -1,8 +1,8 @@
-module(sudoku).
-import(lists,
- [member/2, filter/2, map/2, flatmap/2, sort/1, all/2,
- sum/1]).
+ [member/2, filter/2, map/2, flatmap/2, sort/1, all/2,
+ sum/1]).
-compile(export_all).
@@ -17,20 +17,20 @@
squares() ->
%% Returns a list of 81 square names, including "a1" etc.
ct_expand:term([list_to_atom([X, Y])
- || X <- ?rows, Y <- ?cols]).
+ || X <- ?rows, Y <- ?cols]).
unitlist() ->
%% A list of all units (columns, rows, boxes) in a grid.
ct_expand:term([[list_to_atom([X, Y])
- || X <- ?rows, Y <- [C]]
- || C <- ?cols]
- ++
- [[list_to_atom([X, Y]) || X <- [R], Y <- ?cols]
- || R <- ?rows]
- ++
- [[list_to_atom([X, Y]) || X <- R, Y <- C]
- || R <- ["abc", "def", "ghi"],
- C <- ["123", "456", "789"]]).
+ || X <- ?rows, Y <- [C]]
+ || C <- ?cols]
+ ++
+ [[list_to_atom([X, Y]) || X <- [R], Y <- ?cols]
+ || R <- ?rows]
+ ++
+ [[list_to_atom([X, Y]) || X <- R, Y <- C]
+ || R <- ["abc", "def", "ghi"],
+ C <- ["123", "456", "789"]]).
units(Square) ->
%% A list of units for a specific square
@@ -40,7 +40,7 @@ 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)]),
+ || S <- units(Square)]),
lists:delete(Square, lists:usort(NonUniquePeers)).
values(Puzzle, Square) ->
@@ -60,10 +60,10 @@ clean_grid(GridString) ->
parse_puzzle(Puzzle, [], []) -> Puzzle;
parse_puzzle(Puzzle, [Square | Squares],
- [Value | GridString]) ->
+ [Value | GridString]) ->
IsDigit = lists:member(Value, ?digits),
NewPuzzle = assign_if_digit(Puzzle, Square, Value,
- IsDigit),
+ IsDigit),
parse_puzzle(NewPuzzle, Squares, GridString).
assign_if_digit(Puzzle, Square, Value, true) ->
@@ -77,12 +77,12 @@ empty_puzzle() -> {empty_dict(), 0}.
empty_dict() ->
dict:from_list([{Square, ?digits}
- || Square <- squares()]).
+ || Square <- squares()]).
assign(Puzzle, Square, Digit) ->
%% Assign by eliminating all values except the assigned value.
OtherValues = exclude_from(values(Puzzle, Square),
- Digit),
+ Digit),
eliminate_digits(Puzzle, Square, OtherValues).
eliminate_digits({false, _Count} = False, _, _) ->
@@ -99,7 +99,7 @@ eliminate(Puzzle, [Square | T], Digit) ->
OldValues = values(Puzzle, Square),
NewValues = exclude_from(OldValues, Digit),
NewPuzzle = eliminate(Puzzle, Square, Digit, NewValues,
- OldValues),
+ OldValues),
eliminate(NewPuzzle, T, Digit).
eliminate({_, Count}, _, _, [], _) ->
@@ -109,10 +109,10 @@ eliminate(Puzzle, _, _, Vs, Vs) ->
%% NewValues and OldValues are the same, already eliminated.
Puzzle;
eliminate({ValuesDict, Eliminations}, Square, Digit,
- NewValues, _) ->
+ NewValues, _) ->
NewDict = dict:store(Square, NewValues, ValuesDict),
NewPuzzle = peer_eliminate({NewDict, Eliminations + 1},
- Square, NewValues),
+ Square, NewValues),
%% Digit have been eliminated from this Square.
%% Now see if the elimination has created a unique place for a digit
%% to live in the surrounding units of this Square.
@@ -134,14 +134,14 @@ assign_unique_place(Puzzle, [Unit | T], Digit) ->
%% assign it.
Places = places_for_value(Puzzle, Unit, Digit),
NewPuzzle = assign_unique_place_for_digit(Puzzle,
- Places, Digit),
+ Places, Digit),
assign_unique_place(NewPuzzle, T, Digit).
assign_unique_place_for_digit({_, Count}, [], _) ->
%% Contradiction: no place for Digit found
{false, Count};
assign_unique_place_for_digit(Puzzle, [Square],
- Digit) ->
+ Digit) ->
%% Unique place for Digit found, assign
assign(Puzzle, Square, Digit);
assign_unique_place_for_digit(Puzzle, _, _) ->
@@ -151,7 +151,7 @@ assign_unique_place_for_digit(Puzzle, _, _) ->
places_for_value(Puzzle, Unit, Digit) ->
[Square
|| Square <- Unit,
- lists:member(Digit, values(Puzzle, Square))].
+ lists:member(Digit, values(Puzzle, Square))].
solve(GridString) -> search(parse_grid(GridString)).
@@ -163,29 +163,29 @@ search(Puzzle, true) ->
Puzzle;
search(Puzzle, false) ->
{Square, Values} =
- least_valued_unassigned_square(Puzzle),
+ least_valued_unassigned_square(Puzzle),
first_valid_result(Puzzle, Square, Values).
%% Returns the first valid puzzle, otherwise the last puzzle
first_valid_result({_, Count}, _, []) -> {false, Count};
first_valid_result(Puzzle, Square,
- [Digit | _T] = Digits) ->
+ [Digit | _T] = Digits) ->
PuzzleOrFalse = search(assign(Puzzle, Square, Digit)),
first_valid_result(Puzzle, Square, Digits,
- PuzzleOrFalse).
+ PuzzleOrFalse).
first_valid_result({Dict, ValidCount}, Square, [_ | T],
- {false, InvalidCount}) ->
+ {false, InvalidCount}) ->
first_valid_result({Dict,
- ValidCount + (InvalidCount - ValidCount)},
- Square, T);
+ ValidCount + (InvalidCount - ValidCount)},
+ Square, T);
first_valid_result(_, _, _, Puzzle) -> Puzzle.
least_valued_unassigned_square({ValuesDict, _}) ->
Lengths = [least_valued_unassigned_square_2(V)
- || V <- dict:to_list(ValuesDict)],
+ || V <- dict:to_list(ValuesDict)],
Unassigned = [V1
- || V1 <- Lengths, least_valued_unassigned_square_1(V1)],
+ || V1 <- Lengths, least_valued_unassigned_square_1(V1)],
{_, Square, Values} = lists:min(Unassigned),
{Square, Values}.
@@ -197,7 +197,7 @@ least_valued_unassigned_square_2({S, Values}) ->
solve_all(GridList) ->
PidGrids = [{spawn(fun server/0), Grid}
- || Grid <- GridList],
+ || Grid <- GridList],
[solve_all_1(V) || V <- PidGrids],
[receiveSolution(V1) || V1 <- PidGrids].
@@ -209,27 +209,27 @@ receiveSolution({Pid, Grid}) ->
server() ->
receive
{From, solve, GridString} ->
- From ! {self(), GridString, solve(GridString)}
+ From ! {self(), GridString, solve(GridString)}
end.
is_solved(Puzzle) ->
lists:all(fun (Unit) -> is_unit_solved(Puzzle, Unit)
- end,
- unitlist()).
+ end,
+ unitlist()).
is_unit_solved(Puzzle, Unit) ->
UnitValues = lists:flatmap(fun (S) -> values(Puzzle, S)
- end,
- Unit),
+ end,
+ Unit),
lists:sort(UnitValues) =:= (?digits).
to_string(Puzzle) ->
{ValuesDict, _} = Puzzle,
Fun = fun ({_, [V]}) -> [V];
- ({_, _}) -> "."
- end,
+ ({_, _}) -> "."
+ end,
lists:flatmap(Fun,
- lists:sort(dict:to_list(ValuesDict))).
+ lists:sort(dict:to_list(ValuesDict))).
from_file(Filename, Seperator) ->
{ok, BinData} = file:read_file(Filename),
@@ -238,12 +238,12 @@ from_file(Filename, Seperator) ->
to_file(Filename, Solutions) ->
GridStrings = [[to_string(S) | "\n"] || S <- Solutions],
ok = file:write_file(Filename,
- list_to_binary(GridStrings)).
+ list_to_binary(GridStrings)).
solve_file(Filename, Seperator) ->
Solutions = solve_all(from_file(Filename, Seperator)),
OutFilename = [filename:basename(Filename, ".txt")
- | ".out"],
+ | ".out"],
ok = to_file(OutFilename, Solutions),
Solutions.
@@ -252,20 +252,20 @@ print_results(Filename) ->
print_results(Filename, Seperator) ->
{Time, Solutions} = timer:tc(sudoku, solve_file,
- [Filename, Seperator]),
+ [Filename, Seperator]),
Solved = [Puzzle
- || Puzzle <- Solutions, is_solved(Puzzle)],
+ || Puzzle <- Solutions, is_solved(Puzzle)],
TimeInSeconds = Time / 1000000,
Eliminations = [Count || {_, Count} <- Solutions],
{Total, Avg, Med, Max, Min, NumberPuzzles} =
- stats(Eliminations),
+ stats(Eliminations),
Hz = NumberPuzzles / TimeInSeconds,
Msg = "Solved ~p of ~p puzzles from ~s in ~f "
- "secs (~.2f Hz)\n (~p total eliminations, "
- "avg ~.2f, median ~p, max ~p, min ~p).~n",
+ "secs (~.2f Hz)\n (~p total eliminations, "
+ "avg ~.2f, median ~p, max ~p, min ~p).~n",
io:format(Msg,
- [length(Solved), NumberPuzzles, Filename, TimeInSeconds,
- Hz, Total, Avg, Med, Max, Min]).
+ [length(Solved), NumberPuzzles, Filename, TimeInSeconds,
+ Hz, Total, Avg, Med, Max, Min]).
stats(List) ->
Total = lists:sum(List),
Please sign in to comment.
Something went wrong with that request. Please try again.