public static bool DoesPointExist(int x, int y, List <long> intCodes) { var intComputer = new IntComputer { IntCodes = intCodes.Select(i => i).ToList() }; intComputer.Input.Enqueue(x); intComputer.Input.Enqueue(y); intComputer.Start(); var output = intComputer.DumpOutput().Item1; return(output == 1); }
public static void Main() { var intCodes = System.IO.File .ReadAllText( "C:\\Users\\aarondk\\source\\repos\\AdventOfCode-CSharp\\AdventOfCode-CSharp\\Resources\\Day19.txt") .Split(",").Select(long.Parse).ToList(); var affectedPoints = 0; for (var y = 0; y < 50; y++) { for (var x = 0; x < 50; x++) { var intComputer = new IntComputer { IntCodes = intCodes.Select(i => i).ToList() }; intComputer.Input.Enqueue(x); intComputer.Input.Enqueue(y); intComputer.Start(); if (intComputer.DumpOutput().Item1 == 1) { affectedPoints++; } } } Console.WriteLine($"Amount of affected points = {affectedPoints}"); var yLine = 100; var xOffset = 0; while (true) { var point = GetLeftMostPointInRow(yLine, xOffset, intCodes.Select(i => i).ToList()); if (DoesPointExist(point + 99, yLine - 99, intCodes.Select(i => i).ToList())) { Console.WriteLine($"Part2: {point * 10000 + (yLine - 99)}"); break; } yLine++; xOffset = point; } }
public static int GetLeftMostPointInRow(int y, int xOffset, List <long> intCodes) { var x = xOffset; while (true) { var intComputer = new IntComputer { IntCodes = intCodes.Select(i => i).ToList() }; intComputer.Input.Enqueue(x); intComputer.Input.Enqueue(y); intComputer.Start(); var output = intComputer.DumpOutput().Item1; if (output == 1) { return(x); } x++; } }
public static void Main() { var input = System.IO.File .ReadAllText( "C:\\Users\\Aaron\\source\\repos\\AdventOfCode-CSharp\\AdventOfCode-CSharp\\Resources\\Day15.txt") .Split(",").Select(long.Parse).ToList(); var checkPoints = new Queue <CheckPoint>(); var moveStacks = new Stack <Direction2>(); var environment = new Dictionary <string, int>(); environment.Add("0,0", 1); var currentState = State.CheckNeighbours; var amountNeighboursChecked = 0; CheckPoint currentCheckPoint = null; var currentX = 0; var currentY = 0; var currentDirection = Direction2.West; var tries = 0; var goBack = false; var skipInput = false; var found = false; var stepsToCoolerList = new List <int>(); var intComputer = new IntComputer { IntCodes = input }; intComputer.Start(); while ((intComputer.Status == ComputerStatus.Running || intComputer.Status == ComputerStatus.Waiting) && !found) { if (intComputer.Status == ComputerStatus.Waiting) { //Move the robot according to the output var output = intComputer.DumpOutput()?.Item1; if (output == 1 || output == 2) { var(item1, item2) = UpdateDirection(currentX, currentY, currentDirection); currentX = item1; currentY = item2; Console.WriteLine($"Current at point: {currentX}, {currentY}"); } if (true) { if (currentState == State.CheckNeighbours) { if (goBack == false) { if (amountNeighboursChecked == 0) { currentDirection = Direction2.North; } if (amountNeighboursChecked == 1) { currentDirection = Direction2.South; } if (amountNeighboursChecked == 2) { currentDirection = Direction2.West; } if (amountNeighboursChecked == 3) { currentDirection = Direction2.East; } moveStacks.Push(currentDirection); goBack = true; amountNeighboursChecked++; Console.WriteLine($"Checking neighbour {currentDirection}"); } else { if (output == 0) { var tempPosition = UpdateDirection(currentX, currentY, currentDirection); if (!environment.ContainsKey($"{tempPosition.Item1},{tempPosition.Item2}")) { environment.Add($"{tempPosition.Item1},{tempPosition.Item2}", 0); } Console.WriteLine("Neighbour is a wall"); moveStacks.Pop(); skipInput = true; } else if (output == 1 || output == 2) { if (output == 2) { stepsToCoolerList.Add(moveStacks.Count); } var checkPoint = new CheckPoint { X = currentX, Y = currentY, DirectionsToPoint = StackExtensions.ToReverseQueue(moveStacks) }; Console.WriteLine("Neighbour is not a wall"); Console.WriteLine("Going back to checkpoint"); if (!environment.ContainsKey($"{currentX},{currentY}")) { environment.Add($"{currentX},{currentY}", output.GetValueOrDefault()); checkPoints.Enqueue(checkPoint); } currentDirection = GetOppositeDirection(moveStacks.Pop()); } if (amountNeighboursChecked >= 4) { currentState = State.GoingBack; amountNeighboursChecked = 0; } goBack = false; } } else if (currentState == State.GoingBack) { Console.WriteLine("Going back"); if (moveStacks.Count > 0) { currentDirection = GetOppositeDirection(moveStacks.Pop()); } else { currentState = State.GoingToPoint; skipInput = true; } } else if (currentState == State.GoingToPoint) { Console.WriteLine("Going to new point"); if (currentCheckPoint != null && currentCheckPoint.DirectionsToPoint.Count > 0) { if (output == 1 || output == 2) { currentDirection = currentCheckPoint.DirectionsToPoint.Dequeue(); moveStacks.Push(currentDirection); } else { throw new ArgumentException(); } } else if (currentCheckPoint != null && currentCheckPoint.DirectionsToPoint.Count == 0) { currentState = State.CheckNeighbours; currentCheckPoint = null; } else { if (checkPoints.Count == 0) { found = true; } else { currentCheckPoint = checkPoints.Dequeue(); currentDirection = currentCheckPoint.DirectionsToPoint.Dequeue(); moveStacks.Push(currentDirection); } } if (currentCheckPoint != null && currentCheckPoint.DirectionsToPoint.Count == 0) { currentState = State.CheckNeighbours; } } } if (skipInput) { skipInput = false; } else { Console.WriteLine($"Current direction: {currentDirection}"); Console.WriteLine(); intComputer.Input.Enqueue((int)currentDirection + 1); intComputer.Start(); //Thread.Sleep(10); } } } Console.WriteLine($"Least amount of steps to cooler: {stepsToCoolerList.Min()}"); //printHashMap(environment); var generations = new Dictionary <string, int>(); var pointsToCheck = new Queue <string>(); pointsToCheck.Enqueue("14,20"); generations.Add("14,20", 0); while (pointsToCheck.Count > 0) { var point = pointsToCheck.Dequeue(); if (!environment.ContainsKey(point)) { continue; } var pointX = int.Parse(point.Split(",")[0]); var pointY = int.Parse(point.Split(",")[1]); var generation = generations[point]; if (environment.ContainsKey($"{pointX},{pointY + 1}") && environment[$"{pointX},{pointY + 1}"] == 1) { if (!generations.ContainsKey($"{pointX},{pointY + 1}")) { pointsToCheck.Enqueue($"{pointX},{pointY + 1}"); generations.Add($"{pointX},{pointY + 1}", generation + 1); } } if (environment.ContainsKey($"{pointX},{pointY - 1}") && environment[$"{pointX},{pointY - 1}"] == 1) { if (!generations.ContainsKey($"{pointX},{pointY - 1}")) { pointsToCheck.Enqueue($"{pointX},{pointY - 1}"); generations.Add($"{pointX},{pointY - 1}", generation + 1); } } if (environment.ContainsKey($"{pointX + 1},{pointY}") && environment[$"{pointX + 1},{pointY}"] == 1) { if (!generations.ContainsKey($"{pointX + 1},{pointY}")) { pointsToCheck.Enqueue($"{pointX + 1},{pointY}"); generations.Add($"{pointX + 1},{pointY}", generation + 1); } } if (environment.ContainsKey($"{pointX - 1},{pointY}") && environment[$"{pointX - 1},{pointY}"] == 1) { if (!generations.ContainsKey($"{pointX - 1},{pointY}")) { pointsToCheck.Enqueue($"{pointX - 1},{pointY}"); generations.Add($"{pointX - 1},{pointY}", generation + 1); } } } Console.WriteLine($"Part 2: it takes {generations.Values.Max()} minutes for the room to fill with oxygen"); }