private async Task <int> Paint(SynchronousIntMachine intMachine, Dictionary <Point, int> canvas) { var paintedPositions = new HashSet <Point>(); var directions = new[] { new Point(-1, 0), new Point(0, 1), new Point(1, 0), new Point(0, -1) }; var direction = 0; var pos = new Point(0, 0); while (intMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed) { if (IsUpdateProgressNeeded()) { await UpdateProgressAsync(); } var color = canvas.GetOrAdd(pos, _ => 0); intMachine.InputQueue.Enqueue(color); intMachine.RunUntilBlockOrComplete(); canvas[pos] = (int)intMachine.OutputQueue.Dequeue(); paintedPositions.Add(pos); intMachine.RunUntilBlockOrComplete(); var directionDelta = (int)intMachine.OutputQueue.Dequeue() == 0 ? -1 : 1; direction = (direction + directionDelta + 4) % 4; pos += directions[direction]; } return(paintedPositions.Count); }
public override async Task <string> Part1Async(string input) { var intMachine = new SynchronousIntMachine(input); var map = await GetMap(intMachine); var width = map.Keys.Max(p => p.X + 1); var height = map.Keys.Max(p => p.Y + 1); var alignmentSum = 0; for (var y = 1; y < height - 1; y++) { for (var x = 1; x < width - 1; x++) { if (map[new Point(x, y)] == '#' && map[new Point(x - 1, y)] == '#' && map[new Point(x + 1, y)] == '#' && map[new Point(x, y + 1)] == '#' && map[new Point(x, y - 1)] == '#') { alignmentSum += x * y; } } } return(alignmentSum.ToString()); }
private async Task FollowPath(SynchronousIntMachine intMachine, IEnumerable <string> path) { foreach (var direction in path) { _ = await RunMachineAsync(intMachine, direction); } }
public override async Task <string> Part2Async(string input) { var memory = IntMachineBase.ParseProgram(input); memory[0] = 2; // Wake up var intMachine = new SynchronousIntMachine(memory); var map = await GetMap(intMachine); await UpdateProgressAsync(.5, 1); var(robotPos, robotChar) = map.First(x => RobotDirections.Contains(x.Value)); var robotDirection = Directions[Array.IndexOf(RobotDirections, robotChar)]; map[robotPos] = Scaffolding; myGarbageCounter = char.MaxValue; var path = await FollowPath(map, robotPos, robotDirection); var commandData = await FindSlicing(path.ToArray()); var commands = commandData.Select(x => ConvertCommand(x.Command)).ToArray(); var movementRoutine = CreateMovementRoutine(commandData); movementRoutine.ForEach(x => intMachine.InputQueue.Enqueue(x)); commands.SelectMany(x => x).ForEach(x => intMachine.InputQueue.Enqueue(x)); "n\n".ForEach(x => intMachine.InputQueue.Enqueue(x)); while (intMachine.RunUntilBlockOrComplete() != ReturnCode.Completed) { } return(intMachine.OutputQueue.Last().ToString()); }
public override async Task <string> Part1Async(string input) { var intMachine = new SynchronousIntMachine(input); var canvas = new Dictionary <Point, int>(); var paintedPositionCount = await Paint(intMachine, canvas); return(paintedPositionCount.ToString()); }
public async Task <int> PaintAsync() { var intMachine = new SynchronousIntMachine(input); var canvas = new Dictionary <Point, int> (); var paintedPositionCount = await Paint(intMachine, canvas); return(paintedPositionCount); }
private static bool IsPulling(long [] memory, int x, int y) { var intMachine = new SynchronousIntMachine(memory); intMachine.InputQueue.Enqueue(x); intMachine.InputQueue.Enqueue(y); intMachine.RunUntilBlockOrComplete(); return(intMachine.OutputQueue.Dequeue() == 1); }
public override async Task <string> Part2Async(string input) { var intMachine = new SynchronousIntMachine(input); var canvas = new Dictionary <Point, int> { [new Point(0, 0)] = 1 }; await Paint(intMachine, canvas); return(Render(canvas)); }
public override async Task <string> Part1Async(string input) { var intMachine = new SynchronousIntMachine(input); var(rooms, collectedItems) = await DiscoverRoomsAndGatherItemsAsync(intMachine); var hullBreachRoom = rooms[HullBreach]; var securityCheckpointRoom = rooms[SecurityCheckpoint]; var pathToSecurityCheck = GetPath(hullBreachRoom, securityCheckpointRoom); await FollowPath(intMachine, pathToSecurityCheck); var nextDirection = securityCheckpointRoom.Doors.Keys.Single(d => d != InverseDirections[pathToSecurityCheck.Last()]); var inventories = collectedItems .Subsets() .Select(x => x.OrderBy(y => y).ToList()) .Append(new List <string>()) .OrderBy(x => Math.Abs(collectedItems.Count / 2 - x.Count)) .ToList(); var currentInventory = inventories.First(); string password = null; foreach (var(newInventory, inventoryIndex) in inventories.WithIndex()) { Progress.Percentage = inventoryIndex * 100.0 / inventories.Count * 4; if (!currentInventory.SequenceEqual(newInventory)) { var stuffToDrop = currentInventory.Except(newInventory).ToList(); var stuffToTake = newInventory.Except(currentInventory).ToList(); foreach (var stuff in stuffToDrop) { _ = await RunMachineAsync(intMachine, string.Concat("drop ", stuff)); } foreach (var stuff in stuffToTake) { _ = await RunMachineAsync(intMachine, string.Concat("take ", stuff)); } } currentInventory = newInventory; var response = await RunMachineAsync(intMachine, nextDirection); var roomNames = RoomNameRegex.Matches(response).OfType <Match>().Select(x => x.Groups[1].Value).ToList(); if (roomNames.Count == 1) { password = PasswordRegex.Match(response).Value; break; } } return(password); }
private static (int Address, long X, long Y) ReadPacket(SynchronousIntMachine machine) { var o = machine.OutputQueue; while (o.Count < 3) { machine.RunUntilBlockOrComplete(); } return((int)o.Dequeue(), o.Dequeue(), o.Dequeue()); }
public override async Task <string> Part1Async(string input) { var intMachine = new SynchronousIntMachine(input); var autoInput = new[] { "OR A J", "AND B J", "AND C J", "NOT J J", "AND D J", "WALK" }; var result = await RunAsciiMachine(intMachine, autoInput); return(result.ToString()); }
public static async Task Run() { var input = (await File.ReadAllTextAsync("inputs\\21.txt")); var myIntMachine = new SynchronousIntMachine(input); myIntMachine.addASCIILine("OR A J\n"); myIntMachine.addASCIILine("AND B J\n"); myIntMachine.addASCIILine("AND C J\n"); myIntMachine.addASCIILine("NOT J J\n"); myIntMachine.addASCIILine("AND D J\n"); myIntMachine.addASCIILine("WALK\n"); var c = 0; while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed) { c++; } Console.WriteLine("Part 1: " + myIntMachine.OutputQueue.Last()); myIntMachine = new SynchronousIntMachine(input); myIntMachine.addASCIILine("OR A J\n"); myIntMachine.addASCIILine("AND B J\n"); myIntMachine.addASCIILine("AND C J\n"); myIntMachine.addASCIILine("NOT J J\n"); myIntMachine.addASCIILine("AND D J\n"); myIntMachine.addASCIILine("OR I T\n"); myIntMachine.addASCIILine("OR F T\n"); myIntMachine.addASCIILine("AND E T\n"); myIntMachine.addASCIILine("OR H T\n"); myIntMachine.addASCIILine("AND T J\n"); myIntMachine.addASCIILine("RUN\n"); c = 0; while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed) { c++; } Console.WriteLine("Part 2: " + myIntMachine.OutputQueue.Last()); }
public static async Task Run() { var input = await File.ReadAllTextAsync("inputs\\15.txt"); myIntMachine = new SynchronousIntMachine(input); Map = new Dictionary <Classes.Point, Tile> () { [new Classes.Point(0, 0)] = Tile.Empty }; PathToOxygenGenerator = new List <Classes.Point> (); await Backtrack(new Point (0, 0), null); Console.WriteLine("Part 1: " + PathToOxygenGenerator.Count().ToString()); var o2gen = Map.First(x => x.Value == Tile.OxygenSystem).Key; var maxDistance = 0; Visited = new HashSet <Point> (); var queue = new Queue <(Point p, int distance)> (new [] {
private async Task <long> RunAsciiMachine(SynchronousIntMachine intMachine, string[] inputLines) { if (!IsInteractiveInput) { inputLines.Select(l => l + '\n').SelectMany(l => l).ForEach(c => intMachine.InputQueue.Enqueue(c)); } ReturnCode returnCode; while ((returnCode = intMachine.RunUntilBlockOrComplete()) != ReturnCode.Completed) { if (IsUpdateProgressNeeded()) { await UpdateProgressAsync(); } switch (returnCode) { case ReturnCode.WaitingForInput: var userInput = Console.ReadLine() + '\n'; userInput.ToList().ForEach(x => intMachine.InputQueue.Enqueue(x)); break; case ReturnCode.WrittenOutput: while (intMachine.OutputQueue.Count > 0) { var value = intMachine.OutputQueue.Dequeue(); if (value < 256) { if (IsInteractiveOutput) { Console.Write((char)value); } } else { return(value); } } break; } } return(-1); }
private static IEnumerable <(int Address, long X, long Y)> HandleOutgoingPackets(SynchronousIntMachine machine, Queue <long> [] queues, int MachineCount) { while (machine.RunUntilBlockOrComplete() == ReturnCode.WrittenOutput) { var packet = ReadPacket(machine); var(address, x, y) = packet; if (address < MachineCount) { queues [address].Enqueue(x); queues [address].Enqueue(y); } yield return(packet); } }
public static async Task Run() { var input = (await File.ReadAllTextAsync("inputs\\17.txt")); var myIntMachine = new SynchronousIntMachine(input); List <char> lines = new List <char> (); if (!File.Exists("outputs\\17.txt")) { while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed) { var o = myIntMachine.OutputQueue.Dequeue(); lines.Add((char)o); } var sb = new StringBuilder(); lines.ForEach(e => sb.Append(e)); if (!Directory.Exists("outputs")) { Directory.CreateDirectory("outputs"); } await File.WriteAllTextAsync("outputs\\17.txt", sb.ToString(), Encoding.ASCII); } var scaffold = (await File.ReadAllLinesAsync("outputs\\17.txt", Encoding.ASCII)).Where(e => e != "").ToArray(); var aparm = 0; for (int y = 0; y < scaffold.Length; y++) { for (int x = 0; x < scaffold [y].Length; x++) { aparm += GetIntersect(x, y, scaffold); } } Console.WriteLine("Part 1: " + aparm.ToString()); myIntMachine = new SynchronousIntMachine(input); myIntMachine.SetMemoryRegister(0, 2); var A = "L,12,L,8,R,10,R,10\n"; var B = "L,6,L,4,L,12\n"; var C = "R,10,L,8,L,4,R,10\n"; var seq = "A,B,A,B,C,B,A,C,B,C\n"; //use these to debug your input lengths //if you are stuck, try outputting the outputqueue after the vm is done running as ascii text // var la = A.ToCharArray ().Length; // var lb = B.ToCharArray ().Length; // var lc = C.ToCharArray ().Length; // var ls = seq.ToCharArray ().Length; myIntMachine.addASCIILine(seq); myIntMachine.addASCIILine(A); myIntMachine.addASCIILine(B); myIntMachine.addASCIILine(C); myIntMachine.addASCIILine("n\n"); var c = 0; while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed) { c++; } var part2 = myIntMachine.OutputQueue.Last(); Console.WriteLine("Part 2: " + part2.ToString()); }
public static async Task Run() { var input = await File.ReadAllTextAsync("inputs\\13.txt"); Dictionary <PointInt, tile_type> tiles = new Dictionary <PointInt, tile_type> (); var intMachine = new SynchronousIntMachine(input); while (intMachine.RunUntilBlockOrComplete() == ReturnCode.WrittenOutput) { while (intMachine.OutputQueue.Count < 3) { intMachine.RunUntilBlockOrComplete(); } var x = (int)intMachine.OutputQueue.Dequeue(); var y = (int)intMachine.OutputQueue.Dequeue(); var tile = intMachine.OutputQueue.Dequeue(); tiles [new PointInt(x, y)] = (tile_type)tile; } var blocks = tiles.Where(e => e.Value == tile_type.Block).Count(); Console.WriteLine("Part 1: " + blocks.ToString()); intMachine = new SynchronousIntMachine(input); intMachine.SetMemoryRegister(0, 2); long score = 0; var blockCount = blocks; var ball = Classes.Point.Empty; var paddle = Classes.Point.Empty; ReturnCode returnCode; while (blockCount > -1 && (returnCode = intMachine.RunUntilBlockOrComplete()) != ReturnCode.Completed) { switch (returnCode) { case ReturnCode.WaitingForInput: if (blockCount == 0) { blockCount = -1; break; } var joystickInput = ball.X.CompareTo(paddle.X); intMachine.InputQueue.Enqueue(joystickInput); break; case ReturnCode.WrittenOutput: while (intMachine.OutputQueue.Count < 3) { intMachine.RunUntilBlockOrComplete(); } var x = (int)intMachine.OutputQueue.Dequeue(); var y = (int)intMachine.OutputQueue.Dequeue(); var t = intMachine.OutputQueue.Dequeue(); if (x == -1) { score = t; } else { var tile = (tile_type)t; if (tile != tile_type.Block && tiles [new PointInt(x, y)] == tile_type.Block) { blockCount--; } tiles [new PointInt(x, y)] = tile; if (tile == tile_type.Ball) { ball = new Classes.Point(x, y); } else if (tile == tile_type.HorizPaddle) { paddle = new Classes.Point(x, y); } } break; } } Console.WriteLine("Part 2: " + score); }