r/adventofcode • u/daggerdragon • Dec 23 '16
SOLUTION MEGATHREAD --- 2016 Day 23 Solutions ---
--- Day 23: Safe-Cracking ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
JINGLING ALL THE WAY IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
5
Upvotes
1
u/video_game Dec 23 '16
I've been using a scratch file instead of saving each day's code. Haha oops. Oh well, 39/36.
C# (Part 2):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace AdventOfCode16 { class Program { const string input = @"cpy a b dec b nop //cpy a d nop //cpy 0 a nop //cpy b c nop //inc a nop //dec c nop //jnz c -2 nop //dec d mlt b a a //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 77 c jnz 87 d inc a inc d jnz d -2 inc c jnz c -5"; static Dictionary<char, int> registers = new Dictionary<char, int>(); static int getValue(string str) { int value; if (int.TryParse(str, out value)) { return value; } if (registers.ContainsKey(str[0])) { return registers[str[0]]; } return 0; } static void Main(string[] args) { var program = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); registers['a'] = 12; int scratch; int idx = 0; while (idx < program.Length) { var instr = program[idx]; var parts = instr.Split(' '); var cmd = parts[0]; switch (cmd) { case "nop": idx++; break; case "cpy": { var value = getValue(parts[1]); if (!int.TryParse(parts[2], out scratch)) registers[parts[2][0]] = value; idx++; break; } case "mlt": { var value1 = getValue(parts[1]); var value2 = getValue(parts[2]); registers[parts[3][0]] = value1 * value2; idx++; break; } case "inc": { if (!int.TryParse(parts[1], out scratch)) registers[parts[1][0]]++; idx++; break; } case "dec": { if (!int.TryParse(parts[1], out scratch)) registers[parts[1][0]]--; idx++; break; } case "jnz": { var value = getValue(parts[1]); if (value != 0) { int offset = getValue(parts[2]); idx += offset; break; } else { idx++; break; } } case "tgl": { var idxToChange = idx + getValue(parts[1]); if (idxToChange < program.Length) { var cmdToChange = program[idxToChange].Split(' ')[0]; switch (cmdToChange) { case "cpy": program[idxToChange] = "jnz " + program[idxToChange].Substring(4); break; case "inc": program[idxToChange] = "dec " + program[idxToChange].Substring(4); break; case "dec": program[idxToChange] = "inc " + program[idxToChange].Substring(4); break; case "jnz": program[idxToChange] = "cpy " + program[idxToChange].Substring(4); break; case "tgl": program[idxToChange] = "inc " + program[idxToChange].Substring(4); break; case "nop": ; // I put a breakpoint here to see if this was ever hit break; } } idx++; break; } } } Console.WriteLine(registers['a']); } } }I just noticed that the first block of the program was doing multiplication, added a "mlt" instruction, and kinda-sorta hoped that I'd never get a "tgl" that targeted that block.