Browse Source

Ugly day 11

master
Chris Smith 4 years ago
parent
commit
adbf8fd1ca
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
4 changed files with 88 additions and 3 deletions
  1. 2
    0
      11/answers.txt
  2. 1
    0
      11/input.txt
  3. 82
    0
      11/main.go
  4. 3
    3
      intcode/vm.go

+ 2
- 0
11/answers.txt View File

@@ -0,0 +1,2 @@
1
+1883
2
+APUGURFH

+ 1
- 0
11/input.txt View File

@@ -0,0 +1 @@
1
+3,8,1005,8,301,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,28,1006,0,98,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,54,2,1001,6,10,1,108,1,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,84,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,105,1006,0,94,2,7,20,10,2,5,7,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,139,1006,0,58,2,1003,16,10,1,6,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,172,2,107,12,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,197,1006,0,34,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,102,1,8,223,1006,0,62,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,248,1,7,7,10,1006,0,64,2,1008,5,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,280,101,1,9,9,1007,9,997,10,1005,10,15,99,109,623,104,0,104,1,21102,1,387508351636,1,21101,318,0,0,1106,0,422,21102,1,838480007948,1,21101,0,329,0,1106,0,422,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,235190525123,1,21101,0,376,0,1105,1,422,21101,0,106505084123,1,21101,0,387,0,1106,0,422,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,838324605292,1,21102,1,410,0,1105,1,422,21102,709496668940,1,1,21102,421,1,0,1105,1,422,99,109,2,22101,0,-1,1,21102,1,40,2,21101,0,453,3,21102,443,1,0,1106,0,486,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,448,449,464,4,0,1001,448,1,448,108,4,448,10,1006,10,480,1102,1,0,448,109,-2,2106,0,0,0,109,4,2101,0,-1,485,1207,-3,0,10,1006,10,503,21102,0,1,-3,22102,1,-3,1,21201,-2,0,2,21101,1,0,3,21102,1,522,0,1106,0,527,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,550,2207,-4,-2,10,1006,10,550,21202,-4,1,-4,1106,0,618,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,569,1,0,1106,0,527,21202,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,588,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,610,22101,0,-1,1,21101,0,610,0,106,0,485,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0

+ 82
- 0
11/main.go View File

@@ -0,0 +1,82 @@
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 run(memory []int, startWhite bool) (int, [500][500]bool) {
10
+	input := make([]int, len(memory))
11
+	copy(input, memory)
12
+	vm := intcode.NewVirtualMachine(input)
13
+	vm.Input = make(chan int, 1)
14
+	vm.Output = make(chan int, 1)
15
+	go vm.Run()
16
+
17
+	var grid [500][500]bool
18
+	var painted [500][500]bool
19
+
20
+	paintCount := 0
21
+	x := 250
22
+	y := 250
23
+	dir := 0
24
+	grid[y][x] = startWhite
25
+
26
+	for {
27
+		if grid[y][x] {
28
+			vm.Input <- 1
29
+		} else {
30
+			vm.Input <- 0
31
+		}
32
+
33
+		paint, more := <-vm.Output
34
+		if !more {
35
+			break
36
+		}
37
+		turn := <-vm.Output
38
+
39
+		grid[y][x] = paint == 1
40
+		if !painted[y][x] {
41
+			paintCount++
42
+			painted[y][x] = true
43
+		}
44
+
45
+		if turn == 1 {
46
+			dir = (dir + 1) % 4
47
+		} else {
48
+			dir = (dir + 3) % 4
49
+		}
50
+
51
+		switch dir {
52
+		case 0:
53
+			y--
54
+		case 1:
55
+			x++
56
+		case 2:
57
+			y++
58
+		case 3:
59
+			x--
60
+		}
61
+	}
62
+
63
+	return paintCount, grid
64
+}
65
+
66
+func main() {
67
+	input := common.ReadCsvAsInts("11/input.txt")
68
+	paintCount, _ := run(input, false)
69
+	_, grid := run(input, true)
70
+
71
+	println(paintCount)
72
+	for _, line := range grid {
73
+		for _, b := range line {
74
+			if b {
75
+				fmt.Print("█")
76
+			} else {
77
+				fmt.Print(" ")
78
+			}
79
+		}
80
+		fmt.Println()
81
+	}
82
+}

+ 3
- 3
intcode/vm.go View File

@@ -29,21 +29,21 @@ func (vm *VirtualMachine) arg(pos int) *int {
29 29
 	mask := uint8(1) << uint8(pos)
30 30
 	if vm.parameterModes&mask == mask {
31 31
 		// Parameter mode - the value of the argument is just treated as an int
32
-		if vm.ip+1+pos > cap(vm.Memory) {
32
+		for vm.ip+1+pos > len(vm.Memory) {
33 33
 			vm.Memory = append(vm.Memory, make([]int, 1024)...)
34 34
 		}
35 35
 
36 36
 		return &vm.Memory[vm.ip+1+pos]
37 37
 	} else if vm.relativeModes&mask == mask {
38 38
 		// Relative mode - the value of the argument is treated as a memory offset from the relative base
39
-		if vm.ip+1+pos > cap(vm.Memory) || vm.rb+vm.Memory[vm.ip+1+pos] > cap(vm.Memory) {
39
+		for vm.ip+1+pos > len(vm.Memory) || vm.rb+vm.Memory[vm.ip+1+pos] > len(vm.Memory) {
40 40
 			vm.Memory = append(vm.Memory, make([]int, 1024)...)
41 41
 		}
42 42
 
43 43
 		return &vm.Memory[vm.rb+vm.Memory[vm.ip+1+pos]]
44 44
 	} else {
45 45
 		// Position mode - the value of the argument is treated as a memory offset from the start of the memory
46
-		if vm.ip+1+pos > cap(vm.Memory) || vm.Memory[vm.ip+1+pos] > cap(vm.Memory) {
46
+		for vm.ip+1+pos > len(vm.Memory) || vm.Memory[vm.ip+1+pos] > len(vm.Memory) {
47 47
 			vm.Memory = append(vm.Memory, make([]int, 1024)...)
48 48
 		}
49 49
 

Loading…
Cancel
Save