day12
This commit is contained in:
parent
0876397abe
commit
6dc56b38da
69
day12/day.go
Normal file
69
day12/day.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
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"]
|
||||||
|
}
|
||||||
23
inputs/12
Normal file
23
inputs/12
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
cpy 1 a
|
||||||
|
cpy 1 b
|
||||||
|
cpy 26 d
|
||||||
|
jnz c 2
|
||||||
|
jnz 1 5
|
||||||
|
cpy 7 c
|
||||||
|
inc d
|
||||||
|
dec c
|
||||||
|
jnz c -2
|
||||||
|
cpy a c
|
||||||
|
inc a
|
||||||
|
dec b
|
||||||
|
jnz b -2
|
||||||
|
cpy c b
|
||||||
|
dec d
|
||||||
|
jnz d -6
|
||||||
|
cpy 16 c
|
||||||
|
cpy 17 d
|
||||||
|
inc a
|
||||||
|
dec d
|
||||||
|
jnz d -2
|
||||||
|
dec c
|
||||||
|
jnz c -5
|
||||||
6
inputs/12_test
Normal file
6
inputs/12_test
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
cpy 41 a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
dec a
|
||||||
|
jnz a 2
|
||||||
|
dec a
|
||||||
3
main.go
3
main.go
@ -11,6 +11,7 @@ import (
|
|||||||
"aoc2016/day08"
|
"aoc2016/day08"
|
||||||
"aoc2016/day09"
|
"aoc2016/day09"
|
||||||
"aoc2016/day10"
|
"aoc2016/day10"
|
||||||
|
"aoc2016/day12"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -39,7 +40,7 @@ func initDayFunctions() {
|
|||||||
9: {1: day09.Part1, 2: day09.Part2},
|
9: {1: day09.Part1, 2: day09.Part2},
|
||||||
10: {1: day10.Part1, 2: day10.Part2},
|
10: {1: day10.Part1, 2: day10.Part2},
|
||||||
// 11: {1: day11.Part1, 2: day11.Part2},
|
// 11: {1: day11.Part1, 2: day11.Part2},
|
||||||
// 12: {1: day12.Part1, 2: day12.Part2},
|
12: {1: day12.Part1, 2: day12.Part2},
|
||||||
// 13: {1: day13.Part1, 2: day13.Part2},
|
// 13: {1: day13.Part1, 2: day13.Part2},
|
||||||
// 14: {1: day14.Part1, 2: day14.Part2},
|
// 14: {1: day14.Part1, 2: day14.Part2},
|
||||||
// 15: {1: day15.Part1, 2: day15.Part2},
|
// 15: {1: day15.Part1, 2: day15.Part2},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user