Skip to content

Commit

Permalink
Use gb_sets rather than sets, giving a speed improvement and reducing
Browse files Browse the repository at this point in the history
the number of eliminations somewhat.
Thanks to Ahmed Omar for the tip:
http://groups.google.com/group/erlang-programming/browse_thread/thread/c81df3ff14fc6feb/da2cc43998a70053#da2cc43998a70053

New results (on my Macbook with 2GHz Intel Core 2 Duo):
Solved 50 of 50 puzzles from easy50.txt in 1.960654 secs (25.50 Hz)
  (92538 total eliminations, avg 1850.76, median 1811, max 2628, min 1767).
Solved 95 of 95 puzzles from top95.txt in 13.472294 secs (7.05 Hz)
  (901201 total eliminations, avg 9486.33, median 6267, max 56820, min 1792).
Solved 11 of 11 puzzles from hardest.txt in 0.600741 secs (18.31 Hz)
  (33653 total eliminations, avg 3059.36, median 3023, max 5346, min 1786).
  • Loading branch information
Andreas Pauley committed Mar 27, 2011
1 parent 03b79df commit a02fbcd
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sudoku.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ 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),
PeerSet = gb_sets:from_list(NonUniquePeers),
PeersWithSelf = gb_sets:to_list(PeerSet),
lists:delete(Square, PeersWithSelf).

values(Puzzle, Square) ->
Expand Down Expand Up @@ -205,7 +205,8 @@ is_solved(Puzzle) ->
all(fun(Unit) -> is_unit_solved(Puzzle, Unit) end, unitlist()).
is_unit_solved(Puzzle, Unit) ->
UnitValues = flatmap(fun(S) -> values(Puzzle, S) end, Unit),
(length(UnitValues) == 9) and (sets:from_list(UnitValues) == sets:from_list(digits())).
(length(UnitValues) == 9)
and (gb_sets:from_list(UnitValues) == gb_sets:from_list(digits())).

to_string(Puzzle) ->
{ValuesDict, _} = Puzzle,
Expand Down

0 comments on commit a02fbcd

Please sign in to comment.