public AiInput(IntCodeStateMachine cpu) { this.cpu = cpu; cpu.OnOutput += Intercept; _map = new Floor[100, 100]; _pos = _last = _origin = new Coordinate(50, 50); }
// find a path to a point in an unknown environment. Then find the shortest path // from the origin to that point. public int Run(string input = Input) { var memory = input.Split(',').Select(int.Parse); var cpu = new IntCodeStateMachine(memory); //ICommandInput commandInput = new ConsoleInput(); ICommandInput commandInput = new AiInput(cpu); var repairDroid = new RepairDroid(cpu, commandInput); var consoleView = new ConsoleView((40, 40)); var rcp = new RemoteControlProgram(repairDroid, commandInput, consoleView); var finder = new RemoteDroidPathFinder(repairDroid, rcp); int shortestPath = -1; finder.ShortestPathFound += (_, steps) => { shortestPath = steps; rcp.Stop(); }; rcp.FoundOxygenSystem += (_, coord) => { Console.SetCursorPosition(0, 2); Console.WriteLine("Found oxygen tank at {0}. Navigating back to origin.", coord); }; Console.WriteLine("1=N, 2=S, 3=W, 4=E"); rcp.Run(); return(shortestPath); }
public ArcadeProgram(IntCodeStateMachine cpu, IGameView gameView, IGameInput gameInput = null) { cpu.OnOutput += Advance; _gameView = gameView; gameInput.OnInput += (_, e) => cpu.SetInput(e); _gameState = new GameState(gameView, gameInput); }
private static int RunImpl(string input, int startValue, Dictionary <Coordinate, SquareColor> paintedSquares) { var cpu = new IntCodeStateMachine(input.Split(',').Select(BigInteger.Parse)); var paintingRobotState = new MessageState(paintedSquares, cpu); cpu.OnOutput += paintingRobotState.Run; cpu.SetInput(startValue); cpu.RunAll(); var totalPaintedSquares = paintedSquares.Count; return(totalPaintedSquares); }
public int Run(string input = Input) { var memory = Input.Split(',').Select(s => BigInteger.Parse(s)); // probably not the best origin of control, but I don't // have to make any changes, sooooo, here's my excuse. var cpu = new IntCodeStateMachine(memory); var view = new InMemoryGameView(); var game = new ArcadeProgram(cpu, view); cpu.RunAll(); var blockCount = view.Memory.Values.Count(t => t == TileType.Block); return(blockCount); }
public int Run(string input = Input) { var memory = Input.Split(',').Select(s => BigInteger.Parse(s)).ToArray(); memory[0] = 2; // to play for free! var cpu = new IntCodeStateMachine(memory); var scoreView = new ScoreView(); var gameView = new SegmentDisplay(scoreView, new ConsoleGameView()); var gameInput = new AiGameInput(cpu); var game = new ArcadeProgram(cpu, gameView, gameInput); cpu.RunAll(); Console.Clear(); return(scoreView.FinalScore); }
public RepairDroid(IntCodeStateMachine cpu, ICommandInput commandInput) { _cpu = cpu; _commandInput = commandInput; _commandInput.InputReceived += (_, v) => { var cmd = (MovementCommand)v; if (v < 1 || v > 4) { throw new Exception("Wrong input value"); } _cpu.SetInput((int)cmd); }; _cpu.OnOutput += (_, o) => { _lastOutput = (RepairDroidStatusCode)o; OnReply?.Invoke(this, _lastOutput); commandInput.AwaitInput(); }; }
private int TestPhaseWithFeedback(int phase1, int phase2, int phase3, int phase4, int phase5) { var amp1 = new IntCodeStateMachine(m_currentInput, phase1); var amp2 = new IntCodeStateMachine(m_currentInput, phase2); var amp3 = new IntCodeStateMachine(m_currentInput, phase3); var amp4 = new IntCodeStateMachine(m_currentInput, phase4); var amp5 = new IntCodeStateMachine(m_currentInput, phase5); var input = 0; while (true) { var output1 = amp1.IntCode(input); if (output1 == -1) { break; } var output2 = amp2.IntCode(output1); if (output2 == -1) { break; } var output3 = amp3.IntCode(output2); if (output3 == -1) { break; } var output4 = amp4.IntCode(output3); if (output4 == -1) { break; } input = amp5.IntCode(output4); } return(input); }
public AiGameInput(IntCodeStateMachine cpu) { cpu.OnOutput += InterceptOutput; }
public MessageState(Dictionary <Coordinate, SquareColor> paintedSquares, IntCodeStateMachine cpu) { this.paintedSquares = paintedSquares; this.cpu = cpu; }