diff --git a/day12/day12 b/day12/day12 new file mode 100755 index 0000000..2d0dbcf Binary files /dev/null and b/day12/day12 differ diff --git a/day12/input b/day12/input new file mode 100644 index 0000000..7b0f5e2 --- /dev/null +++ b/day12/input @@ -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 19 c +cpy 14 d +inc a +dec d +jnz d -2 +dec c +jnz c -5 diff --git a/day12/main.go b/day12/main.go new file mode 100644 index 0000000..0f3c757 --- /dev/null +++ b/day12/main.go @@ -0,0 +1,78 @@ +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 +} diff --git a/day12/problem b/day12/problem new file mode 100644 index 0000000..7c4c69b --- /dev/null +++ b/day12/problem @@ -0,0 +1,78 @@ +Advent of Code + +--- Day 12: Leonardo's Monorail --- + + You finally reach the top floor of this building: a garden with a slanted glass ceiling. Looks like there are no more + stars to be had. + + While sitting on a nearby bench amidst some tiger lilies, you manage to decrypt some of the files you extracted from + the servers downstairs. + + According to these documents, Easter Bunny HQ isn't just this building - it's a collection of buildings in the nearby + area. They're all connected by a local monorail, and there's another building not far from here! Unfortunately, being + night, the monorail is currently not operating. + + You remotely connect to the monorail control systems and discover that the boot sequence expects a password. The + password-checking logic (your puzzle input) is easy to extract, but the code it uses is strange: it's assembunny code + designed for the new computer you just assembled. You'll have to execute the code and get the password. + + The assembunny code you've extracted operates on four registers (a, b, c, and d) that start at 0 and can hold any + integer. However, it seems to make use of only a few instructions: + + • cpy x y copies x (either an integer or the value of a register) into register y. + • inc x increases the value of register x by one. + • dec x decreases the value of register x by one. + • jnz x y jumps to an instruction y away (positive means forward; negative means backward), but only if x is not + zero. + + The jnz instruction moves relative to itself: an offset of -1 would continue at the previous instruction, while an + offset of 2 would skip over the next instruction. + + For example: + + cpy 41 a + inc a + inc a + dec a + jnz a 2 + dec a + + The above code would set register a to 41, increase its value by 2, decrease its value by 1, and then skip the last + dec a (because a is not zero, so the jnz a 2 skips it), leaving register a at 42. When you move past the last + instruction, the program halts. + + After executing the assembunny code in your puzzle input, what value is left in register a? + + Your puzzle answer was __________. + +--- Part Two --- + + As you head down the fire escape to the monorail, you notice it didn't start; register c needs to be initialized to + the position of the ignition key. + + If you instead initialize register c to be 1, what value is now left in register a? + + Your puzzle answer was ___________. + +References + + Visible links + . http://adventofcode.com/ + . http://adventofcode.com/2016/about + . http://adventofcode.com/2016/support + . http://adventofcode.com/2016/events + . http://adventofcode.com/2016/settings + . http://adventofcode.com/2016/auth/logout + . http://adventofcode.com/2016 + . http://adventofcode.com/2016 + . http://adventofcode.com/2016/leaderboard + . http://adventofcode.com/2016/stats + . http://adventofcode.com/2016/sponsors + . http://adventofcode.com/2016/sponsors + . https://www.google.com/search?q=tiger+lilies&tbm=isch + . http://adventofcode.com/2016/day/11 + . https://en.wikipedia.org/wiki/Processor_register + . https://en.wikipedia.org/wiki/Integer + . https://en.wikipedia.org/wiki/Instruction_set + . http://adventofcode.com/2016 + . http://adventofcode.com/2016/day/12/input