-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-2.go
61 lines (57 loc) · 1.01 KB
/
2-2.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
r := bufio.NewReader(os.Stdin)
line, err := r.ReadString('\n')
if err != nil {
panic(err)
}
codes := strings.Split(strings.TrimRight(line, "\n"), ",")
program := make([]int, len(codes))
for i, code := range codes {
program[i], err = strconv.Atoi(code)
if err != nil {
panic(err)
}
}
fmt.Println(solve(program))
}
func solve(program []int) int {
for a := 0; a < 100; a++ {
for b := 0; b < 100; b++ {
p := make([]int, len(program))
copy(p, program)
if run(p, a, b) == 19690720 {
return a*100 + b
}
}
}
return -1
}
func run(program []int, a, b int) int {
program[1] = a
program[2] = b
pc := 0
for pc < len(program) {
op := program[pc]
src1 := program[pc+1]
src2 := program[pc+2]
dst := program[pc+3]
pc += 4
switch op {
case 1:
program[dst] = program[src1] + program[src2]
case 2:
program[dst] = program[src1] * program[src2]
default:
return program[0]
}
}
return program[0]
}