-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
159 lines (144 loc) · 4.96 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
part1()
part2()
}
func part1() {
opstr := strings.Split(input, ",")
ops := make([]int64, len(opstr))
for i, s := range opstr {
ops[i], _ = strconv.ParseInt(s, 10, 64)
}
var pos int64
next := func() int64 {
val := ops[pos]
pos++
return val
}
val := func(p int64, fullOp int64, off int64) int64 {
mode := (fullOp / off) % 10
if mode == 0 {
return ops[p]
} else {
return p
}
}
input := []int64{1}
outer:
for {
fullOp := next()
op := fullOp % 100
var p1, p2, p3 int64
switch op {
case 1:
p1 = next()
p2 = next()
p3 = next()
ops[p3] = val(p1, fullOp, 100) + val(p2, fullOp, 1000)
case 2:
p1 = next()
p2 = next()
p3 = next()
ops[p3] = val(p1, fullOp, 100) * val(p2, fullOp, 1000)
case 3:
p1 = next()
p2, input = input[0], input[1:]
ops[p1] = p2
case 4:
p1 = next()
fmt.Println("OUTPUT", val(p1, fullOp, 100))
// output
case 99:
break outer
default:
panic(op)
}
}
}
func part2() {
opstr := strings.Split(input, ",")
ops := make([]int64, len(opstr))
for i, s := range opstr {
ops[i], _ = strconv.ParseInt(s, 10, 64)
}
var pos int64
next := func() int64 {
val := ops[pos]
pos++
return val
}
val := func(p int64, fullOp int64, off int64) int64 {
mode := (fullOp / off) % 10
if mode == 0 {
return ops[p]
} else {
return p
}
}
input := []int64{5}
outer:
for {
fullOp := next()
op := fullOp % 100
var p1, p2, p3 int64
switch op {
case 1:
p1 = next()
p2 = next()
p3 = next()
ops[p3] = val(p1, fullOp, 100) + val(p2, fullOp, 1000)
case 2:
p1 = next()
p2 = next()
p3 = next()
ops[p3] = val(p1, fullOp, 100) * val(p2, fullOp, 1000)
case 3:
p1 = next()
p2, input = input[0], input[1:]
ops[p1] = p2
case 4:
p1 = next()
fmt.Println("OUTPUT", val(p1, fullOp, 100))
case 5:
p1 = next()
p2 = next()
if val(p1, fullOp, 100) != 0 {
pos = val(p2, fullOp, 1000)
}
case 6:
p1 = next()
p2 = next()
if val(p1, fullOp, 100) == 0 {
pos = val(p2, fullOp, 1000)
}
case 7:
p1 = next()
p2 = next()
p3 = next()
if val(p1, fullOp, 100) < val(p2, fullOp, 1000) {
ops[p3] = 1
} else {
ops[p3] = 0
}
case 8:
p1 = next()
p2 = next()
p3 = next()
if val(p1, fullOp, 100) == val(p2, fullOp, 1000) {
ops[p3] = 1
} else {
ops[p3] = 0
}
case 99:
break outer
default:
panic(op)
}
}
}
const input = `3,225,1,225,6,6,1100,1,238,225,104,0,1102,7,85,225,1102,67,12,225,102,36,65,224,1001,224,-3096,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1001,17,31,224,1001,224,-98,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,1101,86,19,225,1101,5,27,225,1102,18,37,225,2,125,74,224,1001,224,-1406,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,1102,13,47,225,1,99,14,224,1001,224,-98,224,4,224,102,8,223,223,1001,224,2,224,1,224,223,223,1101,38,88,225,1102,91,36,224,101,-3276,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1101,59,76,224,1001,224,-135,224,4,224,102,8,223,223,1001,224,6,224,1,223,224,223,101,90,195,224,1001,224,-112,224,4,224,102,8,223,223,1001,224,7,224,1,224,223,223,1102,22,28,225,1002,69,47,224,1001,224,-235,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,107,226,226,224,102,2,223,223,1006,224,329,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,344,101,1,223,223,108,677,226,224,102,2,223,223,1006,224,359,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,374,101,1,223,223,1008,677,226,224,1002,223,2,223,1006,224,389,1001,223,1,223,7,226,677,224,102,2,223,223,1005,224,404,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,419,101,1,223,223,7,226,226,224,102,2,223,223,1005,224,434,1001,223,1,223,8,226,226,224,1002,223,2,223,1006,224,449,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,464,101,1,223,223,1007,226,677,224,1002,223,2,223,1006,224,479,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,494,1001,223,1,223,1108,677,677,224,102,2,223,223,1005,224,509,1001,223,1,223,107,226,677,224,1002,223,2,223,1005,224,524,101,1,223,223,1108,677,226,224,1002,223,2,223,1005,224,539,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,554,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,569,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,584,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,599,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,614,101,1,223,223,1107,226,677,224,102,2,223,223,1006,224,629,101,1,223,223,108,677,677,224,1002,223,2,223,1005,224,644,1001,223,1,223,1107,226,226,224,102,2,223,223,1005,224,659,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226`