diff --git a/day23/day.go b/day23/day.go new file mode 100644 index 0000000..2c6613e --- /dev/null +++ b/day23/day.go @@ -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"] +} diff --git a/inputs/23 b/inputs/23 new file mode 100644 index 0000000..a0444a7 --- /dev/null +++ b/inputs/23 @@ -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 \ No newline at end of file diff --git a/inputs/23_test b/inputs/23_test new file mode 100644 index 0000000..783ce79 --- /dev/null +++ b/inputs/23_test @@ -0,0 +1,4 @@ +inc a +jio a, +2 +tpl a +inc a \ No newline at end of file diff --git a/main.go b/main.go index 28cd575..29ebbc8 100644 --- a/main.go +++ b/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}, } }