Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow the binary result of file:read_file to be solved

  • Loading branch information...
commit 1f7b472050c272cef24bdcfd7725cf56712ffa18 1 parent 8591a47
@apauley authored
Showing with 24 additions and 11 deletions.
  1. +2 −0  rebar.config
  2. +22 −11 src/sudoku.erl
View
2  rebar.config
@@ -0,0 +1,2 @@
+%%-*- mode: erlang -*-
+{erl_opts, [warnings_as_errors]}.
View
33 src/sudoku.erl
@@ -1,7 +1,9 @@
-module(sudoku).
-export([solve_all/1,
+ solve_file/1,
solve_file/2,
+ solve_binary_string/1,
print_results/1]).
-export([test/0]).
@@ -16,6 +18,26 @@ solve_all(GridList) ->
SolutionDicts = solve_all_return_dicts(GridList),
[to_string(S) || S <- SolutionDicts].
+solve_file(Filename) ->
+ solve_file(Filename, "\n").
+
+solve_file(Filename, Seperator) ->
+ {ok, BinString} = file:read_file(Filename),
+ Solutions = solve_binary_string_return_dicts(BinString, Seperator),
+ OutFilename = [filename:basename(Filename, ".txt")
+ | ".out"],
+ ok = to_file(OutFilename, Solutions),
+ Solutions.
+
+solve_binary_string(BinString) ->
+ SolutionDicts = solve_binary_string_return_dicts(BinString, "\n"),
+ [to_string(S) || S <- SolutionDicts].
+
+solve_binary_string_return_dicts(BinString, Seperator) ->
+ PuzzleGridList = string:tokens(binary_to_list(BinString), Seperator),
+ SolutionDicts = solve_all_return_dicts(PuzzleGridList),
+ SolutionDicts.
+
print_results(Filename) ->
print_results(Filename, "\n").
@@ -259,22 +281,11 @@ to_string(Puzzle) ->
lists:flatmap(Fun,
lists:sort(dict:to_list(ValuesDict))).
-from_file(Filename, Seperator) ->
- {ok, BinData} = file:read_file(Filename),
- string:tokens(binary_to_list(BinData), Seperator).
-
to_file(Filename, Solutions) ->
GridStrings = [to_string(S) ++ "\n" || S <- Solutions],
ok = file:write_file(Filename,
list_to_binary(GridStrings)).
-solve_file(Filename, Seperator) ->
- Solutions = solve_all_return_dicts(from_file(Filename, Seperator)),
- OutFilename = [filename:basename(Filename, ".txt")
- | ".out"],
- ok = to_file(OutFilename, Solutions),
- Solutions.
-
exclude_from(Values, Digit) ->
lists:delete(Digit, Values).
Please sign in to comment.
Something went wrong with that request. Please try again.