-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzles_test.go
109 lines (92 loc) · 2.61 KB
/
puzzles_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package puzzle_test
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/dnnrly/puzzle-template"
)
func TestPuzzleHasCorrectCount(t *testing.T) {
p := puzzle.Puzzle{
Parts: []puzzle.Solution{
func() int { return 1 },
func() int { return 2 },
func() int { return 3 },
},
}
assert.Equal(t, 3, p.Size())
}
func ExamplePuzzle_create_a_simple_puzzle_solution() {
// A new puzzle constructor will be generated when you run the 'next' command. Simply
// fill it in to generate your solution
NewPuzzleExample := func() puzzle.Puzzle {
return puzzle.Puzzle{
Parts: []puzzle.Solution{
// Add functions here for all of the different puzzle parts for your challenge
func() int {
// You can do your calculation here
result := 10 * 10
return result
},
},
}
}
// Here endeth the example, the tooling will handle plumbing in the constructor
// automatically.
p := NewPuzzleExample()
for i, s := range p.Parts {
fmt.Printf("Solution %d is %d\n", i, s())
}
// Output:
// Solution 0 is 100
}
func ExamplePuzzle_complex_initialisation_with_helper_functions() {
NewPuzzleExample := func() puzzle.Puzzle {
// In this function, you can add large strings (your input data, perhaps) so
// that they can be parsed in the initialisation phase of the puzzle and won't
// count to the time taken for your algorithm.
data := `1
2
3`
// You can add multiple variables that you use later on if you wish.
values := []int{}
// And you can even add helpers that can be called only from inside your
// puzzle solution.
convert := func(s string) int {
v, _ := strconv.Atoi(s)
return v
}
return puzzle.Puzzle{
Init: func() {
// You can do the heavy pre-processing here
lines := strings.Split(data, "\n")
for _, l := range lines {
values = append(values, convert(l))
}
},
Parts: []puzzle.Solution{
// You can consume the pre-processed data in the solutions here
func() int { return values[0] + 100 },
func() int { return values[1] + 100 },
func() int { return values[2] + 100 },
},
Tidy: func() {
// In this function you can clear down any items that you've created
// in memory. In this example, setting the slice to nil allows the
// garbage collector to release it. Hopefully this function will allow
// you to be able to run ALL of your puzzles together.
values = nil
},
}
}
p := NewPuzzleExample()
p.Init()
for i, s := range p.Parts {
fmt.Printf("Solution %d is %d\n", i, s())
}
// Output:
// Solution 0 is 101
// Solution 1 is 102
// Solution 2 is 103
}