public override string PartTwo(string input) { var vm = new IntCodeVM(input) { InputFunction = Input, OutputFunction = Output }; vm.Run(); _map = _mapString.CreateCharGrid(); Log(_map.GetString()); // figured these out manually on paper var routine = "A,C,A,C,B,A,B,A,B,C\n"; var a = "R,12,L,8,L,4,L,4\n"; var b = "L,8,L,4,R,12,L,6,L,4\n"; var c = "L,8,R,6,L,6\n"; var vid = "n\n"; _input = routine + a + b + c + vid; vm.Reset(); vm.SetMemory(0, 2); vm.Run(); return(_spaceDust.ToString()); }
public override string Part1() { for (var x = 0; x < 50; x++) { for (var y = 0; y < 50; y++) { vm.Reset(); vm.Run(x, y); grid[x, y] = vm.Result; } } return($"{grid.Cast<long>().Sum()}"); }
private int GetThrusterOutput(IEnumerable <int> phaseSettings, string program, bool feedback) { var A = new IntCodeVM(program); var B = new IntCodeVM(program); var C = new IntCodeVM(program); var D = new IntCodeVM(program); var E = new IntCodeVM(program); A.OutputVM = B; B.OutputVM = C; C.OutputVM = D; D.OutputVM = E; if (feedback) { E.OutputVM = A; } A.AddInput(phaseSettings.ElementAt(0)); B.AddInput(phaseSettings.ElementAt(1)); C.AddInput(phaseSettings.ElementAt(2)); D.AddInput(phaseSettings.ElementAt(3)); E.AddInput(phaseSettings.ElementAt(4)); A.Run(0); return(E.Outputs.Last()); }
public override string Part1() { vm.Reset(); var halt = vm.Run(); return(""); }
public override string PartOne(string input) { var vm = new IntCodeVM(input); var output = vm.Run(1); return(output.Last().ToString()); }
public override string Part1() { var vms = Enumerable.Range(0, 50) .Select((_, i) => { var vm = new IntCodeVM(Input.First()); vm.Run(i); return(vm); }).ToList(); while (true) { foreach (var vm in vms) { while (vm.Output.Any()) { var destination = (int)vm.Result; var x = vm.Result; var y = vm.Result; if (destination == 255) { return($"{y}"); } vms[destination].Run(x, y); } vm.Run(-1); } } }
public override string Part1() { vm.Reset(); vm.Run(); var sb = new StringBuilder(); while (vm.Output.Any()) { sb.Append((char)vm.Result); } // Console.Write(sb); var grid = sb.ToString().Trim().Split().Select(s => s.ToCharArray()).ToArray(); var sum = 0; for (var y = 1; y < grid.Length - 1; y++) { for (var x = 1; x < grid[y].Length - 1; x++) { if (grid[y][x] == '#' && grid[y - 1][x] == '#' && grid[y + 1][x] == '#' && grid[y][x - 1] == '#' && grid[y][x + 1] == '#') { sum += x * y; } } } return($"{sum}"); }
private bool CheckBeamCoords(int x, int y, IntCodeVM vm) { vm.Reset(); vm.AddInput(x); vm.AddInput(y); return(vm.Run()[0] > 0); }
public override string PartOne(string input) { var vm = new IntCodeVM(input); vm.SetMemory(1, 12); vm.SetMemory(2, 2); return(vm.Run().ToString()); }
public override string Part2() { var vms = Enumerable.Range(0, 50) .Select((_, i) => { var vm = new IntCodeVM(Input.First()); vm.Run(i); return(vm); }).ToList(); long natX = 0, natY = 0, lastYSent = -1; while (true) { var numIdle = 0; foreach (var vm in vms) { var isIdle = true; while (vm.Output.Any()) { var destination = (int)vm.Result; var x = vm.Result; var y = vm.Result; if (destination == 255) { natX = x; natY = y; } else { vms[destination].Run(x, y); } isIdle = false; } vm.Run(-1); if (isIdle) { numIdle++; } } if (numIdle == 50) { if (natY == lastYSent) { return($"{natY}"); } vms[0].Run(natX, natY); lastYSent = natY; } } }
private void ExploreMap(string input) { _vm = new IntCodeVM(input) { InputFunction = BotInput, OutputFunction = BotOutput }; _open.Add(_bot); _vm.Run(); }
public override string PartOne(string input) { _vm = new IntCodeVM(input) { InputFunction = GetInput, OutputFunction = PaintPanel }; _vm.Run(); return(_panels.Count.ToString()); }
static void Main(string[] args) { program = FetchInput(); vm = new IntCodeVM(1, (long[])program.Clone(), new long[] { 0 }); vm.SpecialInstructions = "PainterBot"; vm.Debug = true; #if DEBUG vm.Verbose = true; #endif vm.Run(); Console.WriteLine("Output: {0}", vm.Output); }
public override string PartOne(string input) { var vm = new IntCodeVM(input); vm.OutputFunction = Output; vm.Run(); _map = _mapString.CreateCharGrid(); var intersections = _map.GetPoints().Where(p => _map[p.X, p.Y] == '#') .Where(p => _map.GetNeighbors(p.X, p.Y, false) .All(c => c == '#')) .ToList(); var result = intersections.Sum(i => i.X * i.Y); return(result.ToString()); }
public override string PartTwo(string input) { _vm = new IntCodeVM(input) { InputFunction = GetInput, OutputFunction = PaintPanel }; _panels.Add(_pos, true); _vm.Run(); var width = _panels.Max(p => p.Key.X) - _panels.Min(p => p.Key.X) + 1; var height = _panels.Max(p => p.Key.Y) - _panels.Min(p => p.Key.Y) + 1; ImageHelper.CreateBitmap(width, height, @"C:\AdventOfCode\Day11.bmp", GetPixel); return(@"C:\AdventOfCode\Day11.bmp"); }
static void Main(string[] args) { sw = new Stopwatch(); program = FetchInput(); sw.Start(); vm = new IntCodeVM(1, (long[])program.Clone(), new long[] { 2 }); #if DEBUG vm.Verbose = true; #endif vm.Run(); sw.Stop(); Console.WriteLine("Output: {0}", vm.Output); Console.WriteLine("Completed in {0}ms ({1} ticks).", sw.ElapsedMilliseconds, sw.ElapsedTicks); Console.WriteLine("loops: {0}", vm.loopCount); Console.ReadLine(); Environment.Exit(0); }
public override string PartOne(string input) { var vm = new IntCodeVM(input) { InputFunction = Input }; _input = "NOT A J\n" + "NOT B T\n" + "OR T J\n" + "NOT C T\n" + "OR T J\n" + "AND D J\n" + "WALK\n"; var outputs = vm.Run(); DisplayOutputs(outputs); return(outputs.Last().ToString()); }
public override string PartTwo(string input) { var vm = new IntCodeVM(input); for (var noun = 0; noun <= 99; noun++) { for (var verb = 0; verb <= 99; verb++) { vm.SetMemory(1, noun); vm.SetMemory(2, verb); if (vm.Run() == 19690720) { return((100 * noun + verb).ToString()); } vm.Reset(); } } throw new Exception(); }
public override string PartTwo(string input) { var vm = new IntCodeVM(input); return(vm.Run(5).Last().ToString()); }
public override string Part1() { vm.Reset(); var currentLocation = new Location(0, 0); var halt = IntCodeVM.HaltType.Waiting; while (halt == IntCodeVM.HaltType.Waiting) { var direction = currentLocation !.NextDirection(); if (direction <= 4) { var(x, y) = currentLocation.Neighbor(direction); if (Location.GetLocation(x, y) == null) { halt = vm.Run(direction); switch (vm.Result) { case Location.Wall: _ = new Location(x, y, Location.Opposites[direction], Location.Wall); break; case Location.Empty: currentLocation = new(x, y, Location.Opposites[direction]); break; case Location.System: currentLocation = new(x, y, Location.Opposites[direction], Location.System); break; default: throw new($"Unknown IntCodeVM response: {vm.Result}"); } } } else { direction = currentLocation.PreviousDirection; if (direction > 0) { halt = vm.Run(direction); currentLocation = vm.Result switch { Location.Empty or Location.System => Location.GetLocation(currentLocation.Neighbor(direction)), _ => throw new($"Unknown or unexpected response for previous room: {vm.Result}") }; } else { if (verbose) { // find extents of canvas int xMin, xMax, yMin, yMax; xMin = yMin = int.MaxValue; xMax = yMax = int.MinValue; foreach (var(x, y) in Location.AllLocations.Keys) { if (x < xMin) { xMin = x; } if (x > xMax) { xMax = x; } if (y < yMin) { yMin = y; } if (y > yMax) { yMax = y; } } Console.WriteLine($"Canvas extends from ({xMin}, {yMin}) to ({xMax}, {yMax})"); // print board for (var y = yMin; y <= yMax; y++) { var line = ""; for (var x = xMin; x <= xMax; x++) { if (Location.AllLocations.ContainsKey((x, y))) { line += Location.AllLocations[(x, y)].Image(); } else { line += "@"; } } Console.WriteLine(line); } } currentLocation = Location.OxygenLocation; var distance = 0; while (currentLocation?.PreviousDirection != 0) { distance++; currentLocation = Location.GetLocation(currentLocation !.PreviousLocation()); } return($"{distance}"); }
static void Main(string[] args) { workers = 5; program = FetchInput(); part1 = false; part2 = true; long highestValue = 0; long[] highestValueArray = null; if (part1) { ampCodes = GetAllAmpCodePositions(ampValuesPart1); foreach (long[] iArr in ampCodes.Where(w => w != null)) { vm = new IntCodeVM(workers, (long[])program.Clone()); vm.PhaseSettings = iArr; #if DEBUG vm.Verbose = true; #endif vm.Run(); if (vm.Output > highestValue) { highestValue = vm.Output; highestValueArray = (long[])iArr.Clone(); } } Console.WriteLine(Environment.NewLine); Console.WriteLine("Part 1: highest value is: {0} from Amp Code {{{1}}}", highestValue, highestValueArray.ArrToString()); Console.ReadLine(); } highestValue = 0; highestValueArray = null; if (part2) { ampCodes = GetAllAmpCodePositions(ampValuesPart2); foreach (long[] iArr in ampCodes.Where(w => w != null)) { //int[] iArr = new int[] { 9, 8, 7, 6, 5 }; Console.WriteLine("Beginning part 2 work with Amp Code set {{{0}}}.", iArr.ArrToString()); vm = new IntCodeVM(workers, (long[])program.Clone()); vm.PhaseSettings = iArr; vm.SpecialInstructions = "loop"; vm.Debug = true; #if DEBUG vm.Verbose = true; #endif vm.Run(); Console.WriteLine("Part 2 work with Amp Code set {{{0}}} gave result: {1}.", iArr.ArrToString(), vm.Output); if (vm.Output > highestValue) { highestValue = vm.Output; highestValueArray = (long[])iArr.Clone(); } } Console.WriteLine(Environment.NewLine); Console.WriteLine("Part 2: highest value is: {0} from Amp Code {{{1}}}", highestValue, highestValueArray.ArrToString()); Console.ReadLine(); } Environment.Exit(0); }
public override string Part1() { vm.Reset(); vm.Run(1); return($"{vm.Output.ToDelimitedString(",")}"); }