Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleaned stuff up more.

  • Loading branch information...
commit cf0337a0dd3e0669373026446c87b975ff4d73ae 1 parent e8a6b90
Robert Carlsen authored
35 main.go
@@ -3,33 +3,28 @@ package main
3 3 import "fmt"
4 4 import "./sudoku"
5 5
6   -func main() {
7   - master := make(sudoku.Group, sudoku.NumAgents)
8   - for i := 0; i < sudoku.NumAgents; i++ {
9   - master[i] = sudoku.New(i)
10   - }
11   -
12   - grps := makeGroups(master)
13   - setPuzzle(master)
14   - sudoku.Solve(master, grps)
  6 +const (
  7 + numAgents = 9
  8 + numVals = 3
  9 +)
15 10
16   - for _, a := range master {
17   - fmt.Print(a.Val, ",")
18   - }
19   - fmt.Print("\n")
  11 +func main() {
  12 + p := sudoku.New(numAgents, numVals)
  13 + p.Solve(setPuzzle, makeGroups)
  14 + fmt.Println(p)
20 15 }
21 16
22 17 // user defined
23 18 func makeGroups(master sudoku.Group) (grps []sudoku.Group) {
24   - grps = make([]sudoku.Group, 2 * sudoku.NumVals)
  19 + grps = make([]sudoku.Group, 2 * numVals)
25 20 for i, _ := range grps {
26   - grps[i] = make(sudoku.Group, sudoku.NumVals)
  21 + grps[i] = make(sudoku.Group, numVals)
27 22 }
28   - for i := 0; i < sudoku.NumVals; i++ {
29   - for j := 0; j < sudoku.NumVals; j++ {
30   - n := i * sudoku.NumVals + j
31   - col := n % sudoku.NumVals
32   - row := n / sudoku.NumVals
  23 + for i := 0; i < numVals; i++ {
  24 + for j := 0; j < numVals; j++ {
  25 + n := i * numVals + j
  26 + col := n % numVals
  27 + row := n / numVals
33 28 grps[col][row] = master[n]
34 29 grps[row + 3][col] = master[n]
35 30 }
BIN  sudoku/.sudoku.go.swp
Binary file not shown
66 sudoku/sudoku.go
... ... @@ -1,34 +1,60 @@
1 1
2 2 package sudoku
3 3
  4 +import "fmt"
4 5 import "time"
5 6
6   -const (
7   - NumAgents = 9
8   - NumVals = 3
9   -)
  7 +func DefaultPuzzle() *Problem {
  8 + return New(1, 1)
  9 +}
10 10
11   -func Solve(masterList Group, grps []Group) {
12   - ch := make(tunnel)
  11 +type Problem struct {
  12 + masterList Group
  13 + grps []Group
  14 + numVals int
  15 +}
  16 +
  17 +func New(numSquares , numVals int) *Problem {
  18 + masterList := make(Group, numSquares)
  19 + for i, _ := range masterList {
  20 + masterList[i] = newSquare(i, numSquares, numVals)
  21 + }
  22 + return &Problem{
  23 + masterList: masterList,
  24 + numVals: numVals,
  25 + }
  26 +}
13 27
14   - for _, g := range grps {
  28 +func (p *Problem) Solve(initVals func(Group), makeGroups func(Group) []Group) {
  29 + initVals(p.masterList)
  30 + p.grps = makeGroups(p.masterList)
  31 +
  32 + ch := make(tunnel)
  33 + for _, g := range p.grps {
15 34 for _, a := range g {
16 35 a.done = ch
17 36 }
18 37 }
19 38
20   - assignGroups(grps)
21   - dispatch(masterList)
  39 + p.assignGroups()
  40 + p.dispatch()
22 41
23   - for count := 0; count < NumAgents; {
  42 + for count := 0; count < len(p.masterList); {
24 43 select {
25 44 case <-ch:
26 45 count++
27 46 case <-time.After(10 * time.Second):
28   - count = NumAgents
  47 + count = len(p.masterList)
29 48 }
30 49 }
31 50 }
  51 +func (p *Problem) String() string {
  52 + s := ""
  53 + for _, a := range p.masterList {
  54 + s += fmt.Sprint(a.Val, ",")
  55 + }
  56 + return s
  57 +}
32 58
33 59 type msg struct {
34 60 id int
@@ -59,16 +85,16 @@ type agent struct {
59 85 ops options
60 86 }
61 87
62   -func New(id int) *agent {
  88 +func newSquare(id, numAgents, numVals int) *agent {
63 89 ops := make(options)
64   - for j := 1; j <= NumVals; j++ {
  90 + for j := 1; j <= numVals; j++ {
65 91 ops[j] = true
66 92 }
67 93 return &agent{
68 94 id: id,
69 95 grps: []Group{},
70 96 ops: ops,
71   - ch: make(tunnel, NumAgents),
  97 + ch: make(tunnel, numAgents),
72 98 }
73 99 }
74 100
@@ -77,6 +103,10 @@ func (a *agent) AddGroup(g Group) {
77 103 }
78 104
79 105 func (a *agent) SetVal(val int) {
  106 + if val == 0 {
  107 + return
  108 + }
  109 +
80 110 for v, _ := range a.ops {
81 111 a.ops[v] = false
82 112 }
@@ -135,16 +165,16 @@ func (a *agent) notifyAll() {
135 165 }
136 166 }
137 167
138   -func assignGroups(grps []Group) {
139   - for _, g := range grps {
  168 +func (p *Problem) assignGroups() {
  169 + for _, g := range p.grps {
140 170 for _, a := range g {
141 171 a.AddGroup(g)
142 172 }
143 173 }
144 174 }
145 175
146   -func dispatch(g Group) {
147   - for _, a := range g {
  176 +func (p *Problem) dispatch() {
  177 + for _, a := range p.masterList {
148 178 go a.Run()
149 179 }
150 180 }

0 comments on commit cf0337a

Please sign in to comment.
Something went wrong with that request. Please try again.