-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathday07.go
111 lines (105 loc) · 2.43 KB
/
day07.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
110
111
package main
import (
"flag"
"fmt"
"github.com/lizthegrey/adventofcode/2019/intcode"
"sync"
)
var inputFile = flag.String("inputFile", "inputs/day07.input", "Relative file path to use as input.")
var debug = flag.Bool("debug", false, "Print debug info as we go along.")
var partB = flag.Bool("partB", false, "Use part B logic.")
func main() {
flag.Parse()
tape := intcode.ReadInput(*inputFile)
if tape == nil {
fmt.Println("Failed to parse input.")
return
}
var phaseList []int
if !*partB {
phaseList = []int{0, 1, 2, 3, 4}
} else {
phaseList = []int{5, 6, 7, 8, 9}
}
highestOutput := -1
permutations := permute(phaseList)
if len(permutations) != 120 {
fmt.Printf("Failed to get right permutations: %d\n", len(permutations))
}
for _, phases := range permutations {
inputVal := 0
inputs := make([]chan int, len(phases))
outputs := make([]chan int, len(phases))
dones := make([]chan bool, len(phases))
for i, p := range phases {
workingTape := tape.Copy()
input := make(chan int, 1)
output, done := workingTape.Process(input)
input <- p
if !*partB || i == 0 {
input <- inputVal
}
if !*partB {
inputVal = <-output
} else {
inputs[i] = input
outputs[i] = output
dones[i] = done
}
}
if !*partB && (inputVal > highestOutput) {
highestOutput = inputVal
}
if *partB {
var wg sync.WaitGroup
for i := range inputs {
wg.Add(1)
go func(idx int) {
defer wg.Done()
oIdx := idx - 1
if idx == 0 {
oIdx = len(inputs) - 1
}
for val := range outputs[oIdx] {
select {
case <-dones[idx]:
if highestOutput < val {
highestOutput = val
}
return
default:
if *debug {
fmt.Printf("Feeding %d from %d to %d\n", val, oIdx, idx)
}
inputs[idx] <- val
}
}
if *debug {
fmt.Printf("Finished forwarder from %d to %d\n", oIdx, idx)
}
}(i)
}
wg.Wait()
}
}
fmt.Println(highestOutput)
}
func permute(in []int) [][]int {
ret := make([][]int, 0)
if len(in) == 1 {
return [][]int{{in[0]}}
}
for i, v := range in {
// Put v at the front, then use all the permutations of the rest.
rest := make([]int, 0)
rest = append(rest, in[0:i]...)
rest = append(rest, in[i+1:]...)
for _, tail := range permute(rest) {
candidate := make([]int, 1)
candidate[0] = v
candidate = append(candidate, tail...)
ret = append(ret, candidate)
}
}
return ret
}