70 lines
1.1 KiB
Go
70 lines
1.1 KiB
Go
package day12
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"tools"
|
|
)
|
|
|
|
func run(code []string, register map[string]int) map[string]int {
|
|
index := 0
|
|
for index < len(code) {
|
|
instr := strings.Split(code[index], " ")
|
|
switch instr[0] {
|
|
case "cpy":
|
|
value, err := strconv.Atoi(instr[1])
|
|
if err != nil {
|
|
register[instr[2]] = register[instr[1]]
|
|
} else {
|
|
register[instr[2]] = value
|
|
}
|
|
index++
|
|
case "inc":
|
|
register[instr[1]]++
|
|
index++
|
|
case "dec":
|
|
register[instr[1]]--
|
|
index++
|
|
case "jnz":
|
|
value, err := strconv.Atoi(instr[1])
|
|
if err != nil {
|
|
value = register[instr[1]]
|
|
}
|
|
if value == 0 {
|
|
index++
|
|
} else {
|
|
jump, _ := strconv.Atoi(instr[2])
|
|
index += jump
|
|
}
|
|
}
|
|
}
|
|
|
|
return register
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
register := map[string]int{
|
|
"a": 0,
|
|
"b": 0,
|
|
"c": 0,
|
|
"d": 0,
|
|
}
|
|
|
|
register = run(puzzle.GetInputArray(), register)
|
|
|
|
return register["a"]
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
register := map[string]int{
|
|
"a": 0,
|
|
"b": 0,
|
|
"c": 1,
|
|
"d": 0,
|
|
}
|
|
|
|
register = run(puzzle.GetInputArray(), register)
|
|
|
|
return register["a"]
|
|
}
|