-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd02.go
78 lines (69 loc) · 1.27 KB
/
d02.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func step(state *[]int, pos int) (int, bool) {
st := (*state)
switch st[pos] {
case 1:
st[st[pos+3]] = st[st[pos+1]] + st[st[pos+2]]
return pos + 4, false
case 2:
st[st[pos+3]] = st[st[pos+1]] * st[st[pos+2]]
return pos + 4, false
case 99:
return pos, true
}
return -1, true
}
func main() {
raw, _ := ioutil.ReadFile(os.Args[1])
input := strings.Split(strings.Trim(string(raw), "\n "), ",")
prog := make([]int, 0)
for i := 0; i < len(input); i++ {
n, err := strconv.Atoi(input[i])
if err != nil {
panic(err)
}
prog = append(prog, n)
}
pos := 0
halted := false
state := make([]int, len(prog))
copy(state, prog)
state[1] = 12
state[2] = 2
for halted == false {
pos, halted = step(&state, pos)
}
fmt.Println("Result1:", state[0])
var noun, verb int
found := false
for noun = 0; noun < 100; noun++ {
for verb = 0; verb < 100; verb++ {
state = make([]int, len(prog))
copy(state, prog)
state[1] = noun
state[2] = verb
pos = 0
for {
pos, halted = step(&state, pos)
if halted {
break
}
}
if pos > -1 && state[0] == 19690720 {
found = true
break
}
}
if found {
break
}
}
fmt.Println("Result2:", 100*noun+verb)
}