refactor: create a execute() function to reuse in part two
This commit is contained in:
@@ -35,15 +35,14 @@ func parseInstructions(data []string) []instruction {
|
|||||||
return instructions
|
return instructions
|
||||||
}
|
}
|
||||||
|
|
||||||
func PartOne(data []string) int {
|
func execute(instructions []instruction) (accumulator int, terminatedNormally bool) {
|
||||||
instructions := parseInstructions(data)
|
|
||||||
visited := make(map[int]bool)
|
visited := make(map[int]bool)
|
||||||
accumulator := 0
|
accumulator = 0
|
||||||
pc := 0
|
pc := 0
|
||||||
|
|
||||||
for pc < len(instructions) {
|
for pc < len(instructions) {
|
||||||
if visited[pc] {
|
if visited[pc] {
|
||||||
return accumulator
|
return accumulator, false
|
||||||
}
|
}
|
||||||
visited[pc] = true
|
visited[pc] = true
|
||||||
|
|
||||||
@@ -59,6 +58,12 @@ func PartOne(data []string) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return accumulator, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func PartOne(data []string) int {
|
||||||
|
instructions := parseInstructions(data)
|
||||||
|
accumulator, _ := execute(instructions)
|
||||||
return accumulator
|
return accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user