public static void TwentyninthPuzzle(string program) { var robot = new PausableLongCodeProgram { Program = ParseIntCode(program), Input = new List <long> { 0 }, Output = new List <long>() }; }
public static void TwentyfifthPuzzle(string program) { var game = new PausableLongCodeProgram { Program = ExtendMemory(ParseIntCode(program)), Input = new List <long>(), Output = new List <long>() }; var board = RunGame(game); Console.WriteLine(board.Values.Count(t => t == 2)); }
public static void TwentysixthPuzzle(string program) { var game = new PausableLongCodeProgram { Program = ExtendMemory(ParseIntCode(program)), Input = new List <long>(), Output = new List <long>() }; game.Program[0] = 2; PlayGame(game); }
public static void TwentyfirstPuzzle(string program) { var robot = new PausableLongCodeProgram { Program = ExtendMemory(ParseIntCode(program)), Input = new List <long> { 0 }, Output = new List <long>() }; var paintedArea = RunRobot(robot); Console.WriteLine(paintedArea.Keys.Count()); }
public static void TwentysecondPuzzle(string program) { var robot = new PausableLongCodeProgram { Program = ExtendMemory(ParseIntCode(program)), Input = new List <long> { 1 }, Output = new List <long>() }; var paintedArea = RunRobot(robot); PrintPaintArea(paintedArea); }
private static Dictionary <Point, int> RunGame(PausableLongCodeProgram game) { var board = new Dictionary <Point, int>(); while (!RunIntCodeProgram(game)) { RunIntCodeProgram(game); RunIntCodeProgram(game); var i = game.Output.Count() - 3; board[new Point((int)game.Output[i], (int)game.Output[i + 1])] = (int)game.Output[i + 2]; } return(board); }
private static void PlayGame(PausableLongCodeProgram game) { var ball = -1; var paddle = -1; var board = new Dictionary <Point, int>(); var length = game.Output.Count(); board[new Point(-1, 0)] = 0; while (!RunIntCodeProgram(game)) { RunIntCodeProgram(game); RunIntCodeProgram(game); var i = game.Output.Count() - 3; var value = (int)game.Output[i + 2]; board[new Point((int)game.Output[i], (int)game.Output[i + 1])] = value; if (value == 3 && game.Output[i] != -1) { paddle = (int)game.Output[i]; } else if (value == 4 && game.Output[i] != -1) { ball = (int)game.Output[i]; } if (length == game.Output.Count()) { if (ball == paddle && game.Input.Last() != 0) { game.Input.Add(0); } else if (ball < paddle) { game.Input.Add(-1); } else if (ball > paddle) { game.Input.Add(1); } } RenderGameBoard(board); length = game.Output.Count(); } }
private static Dictionary <Point, int> RunRobot(PausableLongCodeProgram robot) { var result = new Dictionary <Point, int>(); var currentPoint = new Point(0, 0); var direction = Direction.Up; while (!RunIntCodeProgram(robot)) { RunIntCodeProgram(robot); var length = robot.Output.Count(); result[currentPoint] = (int)robot.Output[length - 2]; direction = direction.Turn((int)robot.Output[length - 1]); currentPoint = TraverseOneStep(currentPoint, direction); var colour = result.TryGetValue(currentPoint, out var c) ? c : 0; robot.Input.Add(colour); } return(result); }
private static bool RunIntCodeProgram(PausableLongCodeProgram pausableProgram) { var program = pausableProgram.Program; var i = pausableProgram.ProgramCounter; var inputPointer = pausableProgram.InputPointer; var relativeBase = pausableProgram.RelativeBase; long a, b; while (program[i] != 99) { int address; switch (program[i] % 100) { case 1: (a, b) = GetParameters(i, relativeBase, program); address = program[i] / 10000 == 2 ? (int)program[i + 3] + relativeBase : (int)program[i + 3]; program[address] = a + b; i += 4; break; case 2: (a, b) = GetParameters(i, relativeBase, program); address = program[i] / 10000 == 2 ? (int)program[i + 3] + relativeBase : (int)program[i + 3]; program[address] = a * b; i += 4; break; case 3: a = pausableProgram.Input[inputPointer++]; address = program[i] / 100 == 2 ? (int)program[i + 1] + relativeBase : (int)program[i + 1]; program[address] = a; i += 2; break; case 4: (a, _) = GetParameters(i, relativeBase, program); pausableProgram.Output.Add(a); i += 2; pausableProgram.Program = program; pausableProgram.ProgramCounter = i; pausableProgram.InputPointer = inputPointer; pausableProgram.RelativeBase = relativeBase; return(false); case 5: (a, b) = GetParameters(i, relativeBase, program); i = (int)(a != 0 ? b : i + 3); break; case 6: (a, b) = GetParameters(i, relativeBase, program); i = (int)(a == 0 ? b : i + 3); break; case 7: (a, b) = GetParameters(i, relativeBase, program); address = program[i] / 10000 == 2 ? (int)program[i + 3] + relativeBase : (int)program[i + 3]; program[address] = a < b ? 1 : 0; i += 4; break; case 8: (a, b) = GetParameters(i, relativeBase, program); address = program[i] / 10000 == 2 ? (int)program[i + 3] + relativeBase : (int)program[i + 3]; program[address] = a == b ? 1 : 0; i += 4; break; case 9: (a, _) = GetParameters(i, relativeBase, program); relativeBase += (int)a; i += 2; break; default: Console.WriteLine($"Unknown instruction: {program[i]}"); return(true); } } pausableProgram.Program = program; pausableProgram.ProgramCounter = i; pausableProgram.InputPointer = inputPointer; pausableProgram.RelativeBase = relativeBase; return(true); }