Permalink
Browse files

Use gb_sets rather than sets, giving a speed improvement and reducing

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...
1 parent 03b79df commit a02fbcd001a9fbd876725008d58c52fcff9872d9 @apauley committed Mar 27, 2011
Showing with 4 additions and 3 deletions.
  1. +4 −3 sudoku.erl
View
@@ -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) ->
@@ -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,

0 comments on commit a02fbcd

Please sign in to comment.