|
@@ -0,0 +1,84 @@
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "github.com/csmith/aoc-2019/common"
|
|
5
|
+ "github.com/csmith/aoc-2019/intcode"
|
|
6
|
+ "runtime/debug"
|
|
7
|
+ "sync"
|
|
8
|
+ "testing"
|
|
9
|
+)
|
|
10
|
+
|
|
11
|
+func findOutputMany(pool *sync.Pool, input []int, target int) int {
|
|
12
|
+ res := make(chan int)
|
|
13
|
+
|
|
14
|
+ for n := 0; n < 100; n++ {
|
|
15
|
+ for v := 0; v < 100; v++ {
|
|
16
|
+ go func(n, v int) {
|
|
17
|
+ if run(pool, input, n, v) == target {
|
|
18
|
+ res <- 100*n + v
|
|
19
|
+ }
|
|
20
|
+ }(n, v)
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ return <-res
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+func findOutputLinear(pool *sync.Pool, input []int, target int) int {
|
|
29
|
+ for n := 0; n < 100; n++ {
|
|
30
|
+ for v := 0; v < 100; v++ {
|
|
31
|
+ if run(pool, input, n, v) == target {
|
|
32
|
+ return 100*n + v
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ panic("Should've returned")
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+func Benchmark_goroutine_per_pair(b *testing.B) {
|
|
40
|
+ debug.SetGCPercent(-1)
|
|
41
|
+ for i := 0; i < b.N; i++ {
|
|
42
|
+ input := common.ReadCsvAsInts("input.txt")
|
|
43
|
+
|
|
44
|
+ pool := &sync.Pool{
|
|
45
|
+ New: func() interface{} {
|
|
46
|
+ return intcode.NewVirtualMachine(make([]int, len(input)))
|
|
47
|
+ },
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ _ = run(pool, input, 12, 2)
|
|
51
|
+ _ = findOutputMany(pool, input, 19690720)
|
|
52
|
+ }
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+func Benchmark_goroutine_per_noun(b *testing.B) {
|
|
56
|
+ debug.SetGCPercent(-1)
|
|
57
|
+ for i := 0; i < b.N; i++ {
|
|
58
|
+ input := common.ReadCsvAsInts("input.txt")
|
|
59
|
+
|
|
60
|
+ pool := &sync.Pool{
|
|
61
|
+ New: func() interface{} {
|
|
62
|
+ return intcode.NewVirtualMachine(make([]int, len(input)))
|
|
63
|
+ },
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ _ = run(pool, input, 12, 2)
|
|
67
|
+ _ = findOutput(pool, input, 19690720)
|
|
68
|
+ }
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+func Benchmark_no_goroutines(b *testing.B) {
|
|
72
|
+ for i := 0; i < b.N; i++ {
|
|
73
|
+ input := common.ReadCsvAsInts("input.txt")
|
|
74
|
+
|
|
75
|
+ pool := &sync.Pool{
|
|
76
|
+ New: func() interface{} {
|
|
77
|
+ return intcode.NewVirtualMachine(make([]int, len(input)))
|
|
78
|
+ },
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ _ = run(pool, input, 12, 2)
|
|
82
|
+ _ = findOutputLinear(pool, input, 19690720)
|
|
83
|
+ }
|
|
84
|
+}
|