-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsudoku.go
40 lines (34 loc) · 791 Bytes
/
sudoku.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
)
// Sudoku represents a puzzle with a solution and identifying metadata
type Sudoku struct {
ID string `json:"id"`
Puzzle [81]int `json:"puzzle"`
Solution [81]int `json:"solution"`
Name string `json:"name"`
}
func newSudoku(puzzle, solution [81]int) Sudoku {
id := generateID(puzzle, solution)
return Sudoku{
ID: id,
Puzzle: puzzle,
Solution: solution,
Name: id,
}
}
func generateID(puzzle, solution [81]int) string {
var source bytes.Buffer
for _, number := range puzzle {
source.WriteByte(byte(number))
}
for _, number := range solution {
source.WriteByte(byte(number))
}
hasher := sha1.New()
hasher.Write(source.Bytes())
return hex.EncodeToString(hasher.Sum(nil))
}