-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrupted.go
More file actions
38 lines (33 loc) · 1004 Bytes
/
Copy pathcorrupted.go
File metadata and controls
38 lines (33 loc) · 1004 Bytes
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
package game
// ComputeCorruptedAcc computes the accumulator in the corrupted game
// ends when an Operation is seen twice
func (ops Operations) ComputeCorruptedAcc() (idx int, accCounter int) {
o := ops[idx]
for ; !o.seen; o = ops[idx] {
switch o.name {
case nopOp:
idx = ops.nop(idx)
case accOp:
idx, accCounter = ops.acc(idx, accCounter)
case jmpOp:
idx = ops.jump(idx)
}
}
return idx, accCounter
}
// nop for no Operation does nothing, go to the next instruction
func (ops Operations) nop(idx int) int {
ops[idx].seen = true
return (idx + 1) % len(ops)
}
// acc for accumulator, increase the current value of the acc counter
// and go to the next instruction
func (ops Operations) acc(idx int, acc int) (int, int) {
ops[idx].seen = true
return (idx + 1) % len(ops), ops[idx].value + acc
}
// jump add the offset to the current index and go to this instruction
func (ops Operations) jump(idx int) int {
ops[idx].seen = true
return (idx + ops[idx].value) % len(ops)
}