diff --git a/parser/instruction.go b/parser/instruction.go index 5915c52..b1fcce5 100644 --- a/parser/instruction.go +++ b/parser/instruction.go @@ -46,11 +46,6 @@ func (inst Instruction) SameOp(instTwo Instruction) bool { return inst.operator == instTwo.operator } -// Complement compares Instructions operator and operand -func (inst Instruction) Complement(instTwo Instruction) bool { - return inst.SameOp(instTwo) && inst.operand+instTwo.operand == 0 -} - // IsZeroOp returns true for ops that have left the pointer on a zero func (inst Instruction) IsZeroOp() bool { return !(inst.operator == opAddDp || diff --git a/parser/instruction_test.go b/parser/instruction_test.go index 80a23e2..99f0878 100644 --- a/parser/instruction_test.go +++ b/parser/instruction_test.go @@ -140,26 +140,3 @@ func TestSameOp(t *testing.T) { } } - -func TestComplement(t *testing.T) { - opsList := []Opcode{ - opAddDp, - opAddVal, - } - - for row, rval := range opsList { - for col, cval := range opsList { - t.Run(fmt.Sprintf("%s-%s", opName[rval], opName[cval]), func(t *testing.T) { - for operand := range [6]int{} { - rinst := Instruction{rval, operand} - cinst := Instruction{cval, operand - 6} - want := row == col && operand == 3 - - if rinst.Complement(cinst) != want { - t.Errorf("testing %v vs %v want %v", rinst, cinst, want) - } - } - }) - } - } -} diff --git a/parser/tokenise.go b/parser/tokenise.go index 5612101..4711120 100644 --- a/parser/tokenise.go +++ b/parser/tokenise.go @@ -111,14 +111,16 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) { func evalFactors(program []Instruction) (pointers, factors []int, ok bool) { ok = false + invert := false collect := 0 pointers = make([]int, 0) factors = make([]int, 0) for _, inst := range program { if inst.SameOp(NewInstruction('>')) { collect += inst.operand - } else if collect == 0 && inst.Complement(NewInstruction('+')) { - ok = true + } else if collect == 0 && inst.SameOp(NewInstruction('-')) { + ok = inst.operand == 1 || inst.operand == -1 + invert = inst.operand == 1 } else if inst.SameOp(NewInstruction('-')) { pointers = append(pointers, collect) factors = append(factors, inst.operand) @@ -129,5 +131,10 @@ func evalFactors(program []Instruction) (pointers, factors []int, ok bool) { if collect != 0 || !ok { return pointers, factors, false } + if invert { + for i := range len(factors) { + factors[i] = 0 - factors[i] + } + } return }