-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (65 loc) · 1.05 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"github.com/danvolchek/AdventOfCode/2019/13/optimized/2/vm"
"io"
"os"
"path"
"strconv"
)
func parse(reader io.Reader) []int {
csvReader := csv.NewReader(reader)
items, err := csvReader.Read()
if err != nil {
panic(err)
}
ret := make([]int, len(items))
for i, item := range items {
val, err := strconv.Atoi(item)
if err != nil {
panic(err)
}
ret[i] = val
}
return ret
}
func one(tape []int) {
v := vm.VM{
Tape: tape,
In: nil,
debug: false,
}
v.Run()
blocks := 0
for i := 0; i < len(v.Out); i += 3 {
if v.Out[i+2] == 2 {
blocks += 1
}
}
fmt.Println(blocks)
}
func two(tape []int) {
tape[0] = 2
v := vm.VM{
Tape: tape,
In: []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
debug: false,
}
v.Run()
fmt.Println(v.Out)
}
func main() {
input, err := os.Open(path.Join("2019", "13", "input.txt"))
if err != nil {
panic(err)
}
tape := parse(input)
one(cp(tape))
two(cp(tape))
}
func cp(t []int) []int {
ret := make([]int, len(t))
copy(ret, t)
return ret
}