Преглед на файлове

Make VM users responsible for its I/O channels.

master
Chris Smith преди 4 години
родител
ревизия
1bff4d5af4
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
променени са 5 файла, в които са добавени 17 реда и са изтрити 17 реда
  1. 1
    1
      02/main.go
  2. 4
    1
      05/main.go
  3. 4
    1
      07/main.go
  4. 4
    12
      intcode/vm.go
  5. 4
    2
      intcode/vm_test.go

+ 1
- 1
02/main.go Целия файл

39
 
39
 
40
 	pool := &sync.Pool{
40
 	pool := &sync.Pool{
41
 		New: func() interface{} {
41
 		New: func() interface{} {
42
-			return intcode.NewVirtualMachine(make([]int, len(input)), false)
42
+			return intcode.NewVirtualMachine(make([]int, len(input)))
43
 		},
43
 		},
44
 	}
44
 	}
45
 
45
 

+ 4
- 1
05/main.go Целия файл

22
 	memory := make([]int, len(input))
22
 	memory := make([]int, len(input))
23
 	copy(memory, input)
23
 	copy(memory, input)
24
 
24
 
25
-	vm := intcode.NewVirtualMachine(memory, true)
25
+	vm := intcode.NewVirtualMachine(memory)
26
+	vm.Input = make(chan int, 1)
27
+	vm.Output = make(chan int, 1)
26
 	vm.Input <- 1
28
 	vm.Input <- 1
27
 	go vm.Run()
29
 	go vm.Run()
28
 	fmt.Println(last(vm.Output))
30
 	fmt.Println(last(vm.Output))
29
 
31
 
30
 	vm.Reset(input)
32
 	vm.Reset(input)
33
+	vm.Output = make(chan int, 1)
31
 	vm.Input <- 5
34
 	vm.Input <- 5
32
 	go vm.Run()
35
 	go vm.Run()
33
 	fmt.Println(last(vm.Output))
36
 	fmt.Println(last(vm.Output))

+ 4
- 1
07/main.go Целия файл

12
 	for i := 0; i < len(ps); i++ {
12
 	for i := 0; i < len(ps); i++ {
13
 		memory := memoryBanks[i*len(program) : (i+1)*len(program)]
13
 		memory := memoryBanks[i*len(program) : (i+1)*len(program)]
14
 		copy(memory, program)
14
 		copy(memory, program)
15
-		vms[i] = intcode.NewVirtualMachine(memory, true)
15
+		vms[i] = intcode.NewVirtualMachine(memory)
16
+		vms[i].Output = make(chan int, 2)
16
 	}
17
 	}
17
 
18
 
18
 	// Link all the inputs and outputs
19
 	// Link all the inputs and outputs
21
 			vm.Input = vms[i-1].Output
22
 			vm.Input = vms[i-1].Output
22
 		} else if feedback {
23
 		} else if feedback {
23
 			vm.Input = vms[len(vms)-1].Output
24
 			vm.Input = vms[len(vms)-1].Output
25
+		} else {
26
+			vm.Input = make(chan int, 2)
24
 		}
27
 		}
25
 	}
28
 	}
26
 
29
 

+ 4
- 12
intcode/vm.go Целия файл

10
 	Output chan int
10
 	Output chan int
11
 }
11
 }
12
 
12
 
13
-// NewVirtualMachine creates a new IntCode virtual machine, initialised
14
-// to the given slice of memory.
15
-func NewVirtualMachine(memory []int, hasIO bool) *VirtualMachine {
13
+// NewVirtualMachine creates a new IntCode virtual machine, initialised to the given slice of memory.
14
+// The caller is responsible for initialising the VM's I/O channels if required.
15
+func NewVirtualMachine(memory []int) *VirtualMachine {
16
 	vm := &VirtualMachine{
16
 	vm := &VirtualMachine{
17
 		ip:     0,
17
 		ip:     0,
18
 		Memory: memory,
18
 		Memory: memory,
19
 		Halted: false,
19
 		Halted: false,
20
 	}
20
 	}
21
 
21
 
22
-	if hasIO {
23
-		vm.Input = make(chan int, 1)
24
-		vm.Output = make(chan int, 1)
25
-	}
26
-
27
 	return vm
22
 	return vm
28
 }
23
 }
29
 
24
 
25
+// arg Returns the value of the given argument for the current instruction.
30
 func (vm *VirtualMachine) arg(pos int) int {
26
 func (vm *VirtualMachine) arg(pos int) int {
31
 	mask := uint8(1) << uint8(pos)
27
 	mask := uint8(1) << uint8(pos)
32
 	if vm.modes&mask == mask {
28
 	if vm.modes&mask == mask {
63
 	copy(vm.Memory, memory)
59
 	copy(vm.Memory, memory)
64
 	vm.ip = 0
60
 	vm.ip = 0
65
 	vm.Halted = false
61
 	vm.Halted = false
66
-	if vm.Input != nil {
67
-		vm.Input = make(chan int, 1)
68
-		vm.Output = make(chan int, 1)
69
-	}
70
 }
62
 }

+ 4
- 2
intcode/vm_test.go Целия файл

17
 	}
17
 	}
18
 
18
 
19
 	for _, table := range tables {
19
 	for _, table := range tables {
20
-		vm := NewVirtualMachine(table.given, false)
20
+		vm := NewVirtualMachine(table.given)
21
 		vm.Run()
21
 		vm.Run()
22
 		if !reflect.DeepEqual(table.expected, vm.Memory) {
22
 		if !reflect.DeepEqual(table.expected, vm.Memory) {
23
 			t.Errorf("Evaluation of %v was incorrect, got: %v, want: %v.", table.given, vm.Memory, table.expected)
23
 			t.Errorf("Evaluation of %v was incorrect, got: %v, want: %v.", table.given, vm.Memory, table.expected)
48
 	}
48
 	}
49
 
49
 
50
 	for _, table := range tables {
50
 	for _, table := range tables {
51
-		vm := NewVirtualMachine(table.given, true)
51
+		vm := NewVirtualMachine(table.given)
52
+		vm.Input = make(chan int, 1)
53
+		vm.Output = make(chan int, 1)
52
 
54
 
53
 		for _, v := range table.input {
55
 		for _, v := range table.input {
54
 			vm.Input <- v
56
 			vm.Input <- v

Loading…
Отказ
Запис