-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (65 loc) · 1.66 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
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func main() {
content, err := ioutil.ReadFile("input.txt")
if err != nil {
panic(fmt.Errorf("Error opening file: %s", err))
}
program := string(content)
program = strings.TrimSpace(program)
starting_opcodes, err := ParseProgram(program)
if err != nil {
panic(fmt.Errorf("Error parsing program: %s", err))
}
desired_output := 19690720
// Iterate through looking for desired result
for noun := 0; noun < 100; noun += 1 {
for verb := 0; verb < 100; verb += 1 {
opcodes := make([]int, len(starting_opcodes))
copy(opcodes, starting_opcodes)
// Enter trial noun and verb
opcodes[1] = noun
opcodes[2] = verb
err = ExecuteProgram(opcodes)
if err != nil {
panic(fmt.Errorf("Error executing program: %s", err))
}
if opcodes[0] == desired_output {
fmt.Printf("Noun = %d, Verb = %d, Solution = %d\n", noun, verb, noun*100+verb)
return
}
}
}
}
func ParseProgram(program string) ([]int, error) {
positions := strings.Split(program, ",")
opcodes := make([]int, len(positions))
for i, s := range positions {
op, err := strconv.Atoi(s)
if err != nil {
return nil, err
}
opcodes[i] = op
}
return opcodes, nil
}
func ExecuteProgram(opcodes []int) error {
for ptr := 0; ptr < len(opcodes); ptr += 4 {
switch opcodes[ptr] {
case 1:
opcodes[opcodes[ptr+3]] = opcodes[opcodes[ptr+1]] + opcodes[opcodes[ptr+2]]
case 2:
opcodes[opcodes[ptr+3]] = opcodes[opcodes[ptr+1]] * opcodes[opcodes[ptr+2]]
case 99:
return nil
default:
return fmt.Errorf("Unexpected opcode: %d", opcodes[ptr])
}
}
return fmt.Errorf("Ran out of program without halt")
}