-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.go
51 lines (39 loc) · 1.35 KB
/
algo.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
41
42
43
44
45
46
47
48
49
50
51
package main
import "fmt"
// AlgorithmFn is the function type that will be used by all the implementations.
type AlgorithmFn func(board *Board, solutions chan []int, done chan void, debug bool) ([]int, error)
// ColorPickerFn is the function type returning the color to play at the next step.
type ColorPickerFn func(board *Board) int
// Linear implementation using the provided color picker function to select the color to play at the next step.
func linearImpl(board *Board, solutions chan []int, done chan void, colorPickerFn ColorPickerFn, debug bool) ([]int, error) {
var solution []int
// Loop until the board is solved.
for {
// Print the board status as CSV.
if debug {
fmt.Printf("Step #%d (color %d)\n", len(solution), board.cells[0])
boardCsv, err := serializeBoardToCsv(board)
if err != nil {
return nil, fmt.Errorf("unable to serialize the board as CSV: %w", err)
}
fmt.Println(boardCsv)
}
// Check if the board is solved.
if board.isSolved() {
break
}
// Pick a color from the frontier.
color := colorPickerFn(board)
// Update the board.
board.playStep(color)
// Append the chosen color to the solution.
solution = append(solution, color)
}
// Push the new solution to the channel.
solutions <- solution
// Notify that the execution is finished.
if done != nil {
done <- void{}
}
return solution, nil
}