public static void CalcPart2() { Console.Clear(); var mem = new long[1000000]; Day17Input.Day17Code.CopyTo(mem, 0); mem[0] = 2; bool wantCamera = false; string program = "A,B,A,C,A,B,C,B,C,B\nR,8,L,10,L,12,R,4\nR,8,L,12,R,4,R,4\nR,8,L,10,R,8\n"; var st = new MachineStatus { ProgramCounter = 0 }; foreach (char c in program) { st = Machine.RunToInput(st, mem, c, OutputConsole); } st = new MachineStatus { ProgramCounter = 0 }; do { st = Machine.RunToOutput(st, mem, OutputConsole); } while (!st.RanToHalt); if (wantCamera) { var x = 0; var y = 0; st = new MachineStatus { ProgramCounter = 0 }; do { st = Machine.RunToOutput(st, mem, OutputConsole); if (!st.RanToHalt) { switch ((char)st.Result) { case '\n': y++; x = 0; break; default: x++; break; } Console.Write((char)st.Result); } } while (!st.RanToHalt); } st = Machine.RunToOutput(st, mem, OutputConsole); Console.WriteLine($"Final Num dust = {st.Result}"); }
public static long CalcWithInput(int verb, int noun, long[] memory, Action <string, long, long> outputFunc) { memory[1] = verb; memory[2] = noun; var st = new MachineStatus { ProgramCounter = 0, RelativeBase = 0 }; return(Calc(st, memory, new long[0], outputFunc).Result); }
public static void Calc() { var arr = new char[43, 49]; var mem = new long[10000]; MachineStatus st = new MachineStatus { ProgramCounter = 0 }; Day17Input.Day17Code.CopyTo(mem, 0); { var x = 0; var y = 0; do { st = Machine.RunToOutput(st, mem, OutputConsole); if (!st.RanToHalt) { switch ((char)st.Result) { case '\n': y++; x = 0; break; default: arr[x, y] = (char)st.Result; x++; break; } Console.Write((char)st.Result); } } while (!st.RanToHalt); } var checksum = 0; for (int y = 0; y < arr.GetLength(1); y++) { for (int x = 0; x < arr.GetLength(0); x++) { Console.SetCursorPosition(x, y); Console.Write(arr[x, y]); if (x == 0 || x == arr.GetLength(0) - 1 || y == 0 || y == arr.GetLength(1) - 1) { continue; } if (arr[x - 1, y] == '#' && arr[x + 1, y] == '#' && arr[x, y - 1] == '#' && arr[x, y + 1] == '#') { checksum += x * y; } } } Console.WriteLine($"Checksum: {checksum}"); Console.ReadLine(); //part2 Day17Input.Day17Code.CopyTo(mem, 0); mem[0] = 2; bool wantCamera = false; //R,8,L,10,L,12,R,4,R,8,L,12,R,4,R,4,R,8,L,10,L,12,R,4,R,8,L,10,R,8,R,8,L,10,L,12,R,4,R,8,L,12,R,4,R,4,R,8,L,10,R,8,R,8,L,12,R,4,R,4,R,8,L,10,R,8,R,8,L,12,R,4,R,4 // string program = "A,B,A,C,A,B,C,B,C,B\nR,8,L,10,L,12,R,4\nR,8,L,12,R,4,R,4\nR,8,L,10,R,8\ny\n"; st.ProgramCounter = 0; foreach (char c in program) { st = Machine.RunToInput(st, mem, c, OutputConsole); } do { st = Machine.RunToOutput(st, mem, OutputConsole); if (Console.CursorLeft >= 43 && Console.CursorTop >= 49) { Console.Clear(); } if (st.Result < 256) { Console.Write((char)st.Result); } else { Console.WriteLine(st.Result); } } while (!st.RanToHalt); return; }
public static MachineStatus Calc(MachineStatus st, long[] memory, long[] Inputs, Action <string, long, long> outputFunc, CalcMode calcMode = default) { int inputCounter = 0; long relativeBase = st.RelativeBase; long pc = st.ProgramCounter; while (pc < memory.Length) { var opcode = memory[pc] % 100; var arg1Mode = ArgMode(memory[pc], 100); var arg2Mode = ArgMode(memory[pc], 1000); var arg3Mode = ArgMode(memory[pc], 10000); long getArg1() { switch (opcode) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: var addr = memory[pc + 1]; long arg = addr; if (arg1Mode == 0) { arg = memory[addr]; } if (arg1Mode == 2) { arg = memory[addr + relativeBase]; } return(arg); } return(0); } long getArg2() { switch (opcode) { case 1: case 2: case 5: case 6: case 7: case 8: var addr = memory[pc + 2]; long arg = addr; if (arg2Mode == 0) { arg = memory[addr]; } if (arg2Mode == 2) { arg = memory[addr + relativeBase]; } return(arg); } return(0); } long getArg3() { switch (opcode) { case 7: case 8: var addr = memory[pc + 3]; long arg = addr; if (arg3Mode == 2) { arg = memory[addr + relativeBase]; } return(arg); } return(0); } var arg1 = getArg1(); var arg2 = getArg2(); var arg3 = getArg3(); switch (opcode) { case 1: var addrt = memory[pc + 3]; if (arg3Mode == 2) { addrt = addrt + relativeBase; } memory[addrt] = arg1 + arg2; pc = pc + 4; break; case 2: addrt = memory[pc + 3]; if (arg3Mode == 2) { addrt = addrt + relativeBase; } memory[addrt] = arg1 * arg2; pc = pc + 4; break; case 3: var addr31 = memory[pc + 1]; if (arg1Mode == 2) { addr31 = relativeBase + addr31; } memory[addr31] = Inputs[inputCounter++]; pc = pc + 2; if (calcMode == CalcMode.RunToFirstInput) { return new MachineStatus { Result = -240174, ProgramCounter = pc, RelativeBase = relativeBase, RanToHalt = false } } ; break; case 4: arg1 = getArg1(); outputFunc("Output {0} {1}", pc, arg1); pc = pc + 2; if (calcMode == CalcMode.RunToFirstOutput) { return new MachineStatus { Result = arg1, ProgramCounter = pc, RelativeBase = relativeBase, RanToHalt = false } } ; break; case 5: //JNZ if (arg1 != 0) { pc = arg2; } else { pc = pc + 3; } break; case 6: //JZ if (arg1 == 0) { pc = arg2; } else { pc = pc + 3; } break; case 7: //LT addrt = memory[pc + 3]; if (arg3Mode == 2) { addrt = addrt + relativeBase; } if (arg1 < arg2) { memory[addrt] = 1; } else { memory[addrt] = 0; } pc = pc + 4; break; case 8: //EQ addrt = memory[pc + 3]; if (arg3Mode == 2) { addrt = addrt + relativeBase; } if (arg1 == arg2) { memory[addrt] = 1; } else { memory[addrt] = 0; } pc = pc + 4; break; case 9: //Set Relative Base relativeBase += arg1; pc += 2; break; case 99: return(new MachineStatus { Result = memory[0], ProgramCounter = pc, RelativeBase = relativeBase, RanToHalt = true }); } } return(new MachineStatus { Result = -230771, ProgramCounter = pc, RelativeBase = relativeBase, RanToHalt = false }); }
public static MachineStatus RunToOutput(MachineStatus st, long[] memory, Action <string, long, long> outputFunc) { return(Calc(st, memory, new long[0], outputFunc, CalcMode.RunToFirstOutput)); }
public static MachineStatus RunToInput(MachineStatus st, long[] memory, int singleInput, Action <string, long, long> outputFunc) { return(Calc(st, memory, new long[] { singleInput }, outputFunc, CalcMode.RunToFirstInput)); }