private async Task <BigInteger> RunVm(string tape, int input) => await Task.Run(() => { var vm = new IntCodeVm(tape); vm.SetInput(input); vm.RunProgramUntilHalt(); return(vm.GetOutputs().Last()); });
public override async Task <BigInteger> RunPart1Async() => await Task.Run(() => { var c = new Permutations <int>(new int[] { 0, 1, 2, 3, 4 }, GenerateOption.WithoutRepetition); BigInteger bestOutput = 0; foreach (var phaseSettings in c) { BigInteger nextInput = 0; foreach (var ps in phaseSettings) { var vm = new IntCodeVm(Inputs[0]); vm.SetInput(ps); vm.SetInput(nextInput); vm.RunProgramUntilHalt(); nextInput = vm.GetOutput(); } if (nextInput > bestOutput) { bestOutput = nextInput; } } return(bestOutput); });
public async Task <int> RunCode(int noun, int verb) => await Task.Run(() => { var vm = new IntCodeVm(Inputs[0]); vm.SetValue(1, noun); vm.SetValue(2, verb); vm.RunProgramUntilHalt(); return((int)vm.GetValue(0)); });
private IList <IntCodeVm> GetVms(IList <int> phaseSettings) => Enumerable.Range(0, 5) .Select(r => { var vm = new IntCodeVm(Inputs[0]); vm.SetInput(phaseSettings[r]); if (r == 0) { vm.SetInput(0); } return(vm); }).ToList();
public static void LetsDoThis() { var inputBuffer = new BufferBlock<long>(); var outputBuffer = new BufferBlock<long>(); var input = programStr; var inputArray = input.Split(","); var program = inputArray.Select(v => long.Parse(v)).ToArray(); var vm = new IntCodeVm(program, inputBuffer, outputBuffer); var runningProgram = Task.Run(() => vm.RunProgram()); var playareaDrawer = Task.Run(async () => { var command = (long)Direction.West; var locations = new Dictionary<Point, Point>(); var location = new Point { State = '.' }; locations.Add(location, location); while (true) { try { inputBuffer.Post(command); var statusAfterCommand = await outputBuffer.ReceiveAsync(); if (statusAfterCommand == 0) { // wall var wallLocation = (Direction)command switch { Direction.North => location.MoveUp(), Direction.South => location.MoveDown(), Direction.West => location.MoveLeft(), Direction.East => location.MoveRight(), _ => throw new Exception("illegal command"), }; wallLocation.State = '#'; locations.TryAdd(wallLocation, wallLocation); // new command (turn east) command = (Direction)command switch { Direction.North => (long)Direction.East, Direction.South => (long)Direction.West, Direction.West => (long)Direction.North, Direction.East => (long)Direction.South, _ => throw new Exception("illegal command"), }; } else if (statusAfterCommand == 1) { location = (Direction)command switch { Direction.North => location.MoveUp(), Direction.South => location.MoveDown(), Direction.West => location.MoveLeft(), Direction.East => location.MoveRight(), _ => throw new Exception("illegal command"), }; location.State = '.'; locations.TryAdd(location, location); } else { throw new Exception("found oxygen tank"); } } catch (InvalidOperationException ex) when (ex.Source == "System.Threading.Tasks.Dataflow") { break; } } }); runningProgram.Wait(); outputBuffer.Complete(); playareaDrawer.Wait(); }