adventofcode/2016/day12/main.go

79 lines
1.4 KiB
Go
Raw Normal View History

2016-12-12 14:43:14 +00:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var regs = map[string]int{
"a": 0,
"b": 0,
"c": 1,
"d": 0,
}
func main() {
input := stdinToStringSlice()
curr := 0
for curr < len(input) {
ins := strings.Fields(input[curr])
curr++
switch ins[0] {
case "cpy":
src := ins[1]
dst := ins[2]
// check if the src is a register
if v, ok := regs[src]; ok {
regs[dst] = v
} else {
// It's not, must be an int
regs[dst] = atoi(src)
}
case "inc":
regs[ins[1]] = regs[ins[1]] + 1
case "dec":
regs[ins[1]] = regs[ins[1]] - 1
case "jnz":
v, ok := regs[ins[1]]
if !ok {
v = atoi(ins[1])
}
if v != 0 {
// Subtract 1 from the jump because we incremented already
curr += atoi(ins[2]) - 1
}
}
}
PrintState()
}
// Fancy State Printing
func PrintState() {
datLine := fmt.Sprint("\u2502 a:", regs["a"], " b:", regs["b"], " c:", regs["c"], " d:", regs["d"], " \u2502")
fmt.Println("\u250C" + strings.Repeat("\u2500", len(datLine)-6) + "\u2510")
fmt.Println(datLine)
fmt.Println("\u2514" + strings.Repeat("\u2500", len(datLine)-6) + "\u2518")
}
func stdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi:" + i)
}
return ret
}