Browse Source

Day 7

master
Chris Smith 4 years ago
parent
commit
e1c1247332
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
6 changed files with 89 additions and 30 deletions
  1. 2
    0
      07/answers.txt
  2. 1
    0
      07/input.txt
  3. 64
    0
      07/main.go
  4. 0
    28
      common/geometry.go
  5. 21
    0
      common/itertools.go
  6. 1
    2
      intcode/vm.go

+ 2
- 0
07/answers.txt View File

@@ -0,0 +1,2 @@
1
+255590
2
+58285150

+ 1
- 0
07/input.txt View File

@@ -0,0 +1 @@
1
+3,8,1001,8,10,8,105,1,0,0,21,42,63,76,101,114,195,276,357,438,99999,3,9,101,2,9,9,102,5,9,9,1001,9,3,9,1002,9,5,9,4,9,99,3,9,101,4,9,9,102,5,9,9,1001,9,5,9,102,2,9,9,4,9,99,3,9,1001,9,3,9,1002,9,5,9,4,9,99,3,9,1002,9,2,9,101,5,9,9,102,3,9,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,3,9,9,102,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99

+ 64
- 0
07/main.go View File

@@ -0,0 +1,64 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"github.com/csmith/aoc-2019/common"
6
+	"github.com/csmith/aoc-2019/intcode"
7
+)
8
+
9
+func copyOf(memory []int) []int {
10
+	res := make([]int, len(memory))
11
+	copy(res, memory)
12
+	return res
13
+}
14
+
15
+func runPipeline(program []int, ps []int, feedback bool) int {
16
+	// Create a series of VMs for our amplifiers
17
+	vms := make([]*intcode.VirtualMachine, len(ps))
18
+	for i := 0; i < len(ps); i++ {
19
+		vms[i] = intcode.NewVirtualMachine(copyOf(program), true)
20
+	}
21
+
22
+	// Link all the inputs and outputs
23
+	for i, vm := range vms {
24
+		if i > 0 {
25
+			vm.Input = vms[i-1].Output
26
+		} else if feedback {
27
+			vm.Input = vms[len(vms)-1].Output
28
+		}
29
+	}
30
+
31
+	// Seed the phase settings
32
+	for i, vm := range vms {
33
+		vm.Input <- ps[i]
34
+	}
35
+
36
+	// Background run all but the last VM
37
+	for _, vm := range vms[:len(vms)-1] {
38
+		go vm.Run()
39
+	}
40
+
41
+	// Kick off the first VM and then wait for the last VM to finish
42
+	vms[0].Input <- 0
43
+	vms[len(vms)-1].Run()
44
+
45
+	// Snag the left over value from the last VM's output
46
+	return <-vms[len(vms)-1].Output
47
+}
48
+
49
+func maxOutput(input []int, ps []int, feedback bool) int {
50
+	max := 0
51
+	for _, p := range common.Permutations(ps) {
52
+		val := runPipeline(input, p, feedback)
53
+		if val > max {
54
+			max = val
55
+		}
56
+	}
57
+	return max
58
+}
59
+
60
+func main() {
61
+	input := common.ReadCsvAsInts("07/input.txt")
62
+	fmt.Println(maxOutput(input, []int{0, 1, 2, 3, 4}, false))
63
+	fmt.Println(maxOutput(input, []int{5, 6, 7, 8, 9}, true))
64
+}

+ 0
- 28
common/geometry.go View File

@@ -1,28 +0,0 @@
1
-package common
2
-
3
-import (
4
-	"fmt"
5
-)
6
-
7
-// Point represents a point on a 2D grid.
8
-type Point struct {
9
-	X, Y int64
10
-}
11
-
12
-// Plus returns a new Point which is the sum of this point and the other point.
13
-func (p Point) Plus(p2 Point) Point {
14
-	return Point{
15
-		X: p.X + p2.X,
16
-		Y: p.Y + p2.Y,
17
-	}
18
-}
19
-
20
-// String returns a user-friendly string for debugging purposes.
21
-func (p Point) String() string {
22
-	return fmt.Sprintf("(%d, %d)", p.X, p.Y)
23
-}
24
-
25
-// Manhattan calculates the Manhattan distance between this point and the other point.
26
-func (p Point) Manhattan(other Point) int64 {
27
-	return Abs(p.X-other.X) + Abs(p.Y-other.Y)
28
-}

+ 21
- 0
common/itertools.go View File

@@ -0,0 +1,21 @@
1
+package common
2
+
3
+// Permutations returns all possible permutations of the given ints.
4
+// Source: https://github.com/dolotech/datastructures-algorithms/blob/master/NUMERICSLICEPERMUTATIONGENERATOR.md
5
+func Permutations(xs []int) (res [][]int) {
6
+	var rc func([]int, int)
7
+	rc = func(a []int, k int) {
8
+		if k == len(a) {
9
+			res = append(res, append([]int{}, a...))
10
+		} else {
11
+			for i := k; i < len(xs); i++ {
12
+				a[k], a[i] = a[i], a[k]
13
+				rc(a, k+1)
14
+				a[k], a[i] = a[i], a[k]
15
+			}
16
+		}
17
+	}
18
+	rc(xs, 0)
19
+
20
+	return res
21
+}

+ 1
- 2
intcode/vm.go View File

@@ -65,8 +65,7 @@ func (vm *VirtualMachine) Run() {
65 65
 
66 66
 		vm.opcodes[opcode].(OpcodeFunc)(vm)
67 67
 	}
68
-	if vm.Input != nil {
69
-		close(vm.Input)
68
+	if vm.Output != nil {
70 69
 		close(vm.Output)
71 70
 	}
72 71
 }

Loading…
Cancel
Save