aoc2015/day23/day.go
Stefan Harmuth 4328f27544 day23
2021-01-15 07:37:05 +01:00

66 lines
1.3 KiB
Go

package day23
import (
"strconv"
"strings"
"tools"
)
func run(code []string, registers map[string]int) {
instptr := 0
for instptr < len(code) {
instrparts := strings.Split(code[instptr], " ")
switch instrparts[0] {
case "hlf": // half a register
registers[instrparts[1]] /= 2
instptr++
case "tpl": // tripple a register
registers[instrparts[1]] *= 3
instptr++
case "inc": // increase register by 1
registers[instrparts[1]]++
instptr++
case "jmp": // jump to instruction
jumpamount, _ := strconv.Atoi(instrparts[1])
instptr += jumpamount
case "jie": // jump to instruction if register is even
if registers[string(instrparts[1][0])]%2 == 0 {
jumpamount, _ := strconv.Atoi(instrparts[2])
instptr += jumpamount
} else {
instptr++
}
case "jio": // jump to instruction if register is one
if registers[string(instrparts[1][0])] == 1 {
jumpamount, _ := strconv.Atoi(instrparts[2])
instptr += jumpamount
} else {
instptr++
}
}
}
}
func Part1(puzzle tools.AoCPuzzle) interface{} {
code := puzzle.GetInputArray()
registers := map[string]int{
"a": 0,
"b": 0,
}
run(code, registers)
return registers["b"]
}
func Part2(puzzle tools.AoCPuzzle) interface{} {
code := puzzle.GetInputArray()
registers := map[string]int{
"a": 1,
"b": 0,
}
run(code, registers)
return registers["b"]
}