day23
This commit is contained in:
parent
cffbd23a11
commit
4328f27544
65
day23/day.go
Normal file
65
day23/day.go
Normal file
@ -0,0 +1,65 @@
|
||||
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"]
|
||||
}
|
||||
47
inputs/23
Normal file
47
inputs/23
Normal file
@ -0,0 +1,47 @@
|
||||
jio a, +18
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
tpl a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
tpl a
|
||||
tpl a
|
||||
inc a
|
||||
jmp +22
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
jio a, +8
|
||||
inc b
|
||||
jie a, +4
|
||||
tpl a
|
||||
inc a
|
||||
jmp +2
|
||||
hlf a
|
||||
jmp -7
|
||||
4
inputs/23_test
Normal file
4
inputs/23_test
Normal file
@ -0,0 +1,4 @@
|
||||
inc a
|
||||
jio a, +2
|
||||
tpl a
|
||||
inc a
|
||||
2
main.go
2
main.go
@ -23,6 +23,7 @@ import (
|
||||
"aoc2015/day20"
|
||||
"aoc2015/day21"
|
||||
"aoc2015/day22"
|
||||
"aoc2015/day23"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -62,6 +63,7 @@ func initDayFunctions() {
|
||||
20: {1: day20.Part1, 2: day20.Part2},
|
||||
21: {1: day21.Part1, 2: day21.Part2},
|
||||
22: {1: day22.Part1, 2: day22.Part2},
|
||||
23: {1: day23.Part1, 2: day23.Part2},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user