aoc2020/day08/lib.go

39 lines
610 B
Go

package day08
func runProgram(rows []Operation, useLog bool) int {
// Log of pos => accumulators executed
runlog := make(map[int]int)
pos := 0
acc := 1 // Start accumulator one above than it should be
for true {
if pos >= len(rows) {
break
}
// Otherwise the if here doesn't work
if runlog[pos] > 0 {
if useLog {
break
} else {
return 0
}
}
runlog[pos] = acc
switch rows[pos].Operation {
case "acc":
acc += rows[pos].Argument
case "jmp":
pos += rows[pos].Argument - 1
case "nop":
}
pos++
}
// And substract the accumulator here
return acc - 1
}