You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
610 B

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
}