This commit is contained in:
Stefan Harmuth 2021-10-26 08:15:03 +02:00
parent 712cab8120
commit 3ed3fb78f7
4 changed files with 133 additions and 1 deletions

98
day23/day.go Normal file
View File

@ -0,0 +1,98 @@
package day23
import (
"strconv"
"strings"
"tools"
)
func run(code []string, register map[string]int) map[string]int {
index := 0
for index < len(code) {
//fmt.Println("Executing line", index, ":", code[index])
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, err := strconv.Atoi(instr[2])
if err != nil {
index += register[instr[2]]
} else {
index += jump
}
}
case "tgl":
toChange := index + register[instr[1]]
if toChange < len(code) && toChange >= 0 {
switch len(strings.Split(code[toChange], " ")) {
case 2:
if code[toChange][:3] == "inc" {
code[toChange] = "dec" + code[toChange][3:]
} else {
code[toChange] = "inc" + code[toChange][3:]
}
case 3:
if code[toChange][:3] == "jnz" {
code[toChange] = "cpy" + code[toChange][3:]
} else {
code[toChange] = "jnz" + code[toChange][3:]
}
}
}
index++
}
/*
fmt.Println("New registers", register)
fmt.Println("New code", code)
bufio.NewReader(os.Stdin).ReadBytes('\n')
*/
}
return register
}
func Part1(puzzle tools.AoCPuzzle) interface{} {
register := map[string]int{
"a": 7,
"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": 12,
"b": 0,
"c": 1,
"d": 0,
}
register = run(puzzle.GetInputArray(), register)
return register["a"]
}

26
inputs/23 Normal file
View File

@ -0,0 +1,26 @@
cpy a b
dec b
cpy a d
cpy 0 a
cpy b c
inc a
dec c
jnz c -2
dec d
jnz d -5
dec b
cpy b c
cpy c d
dec d
inc c
jnz d -2
tgl c
cpy -16 c
jnz 1 c
cpy 84 c
jnz 71 d
inc a
inc d
jnz d -2
inc c
jnz c -5

7
inputs/23_test Normal file
View File

@ -0,0 +1,7 @@
cpy 2 a
tgl a
tgl a
tgl a
cpy 1 a
dec a
dec a

View File

@ -20,6 +20,7 @@ import (
"aoc2016/day18"
"aoc2016/day19"
"aoc2016/day20"
"aoc2016/day23"
"flag"
"fmt"
"os"
@ -59,7 +60,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},
23: {1: day23.Part1, 2: day23.Part2},
// 24: {1: day24.Part1, 2: day24.Part2},
// 25: {1: day25.Part1, 2: day25.Part2},
}