public override void Start2() { var filename = GetFileName1(); var amplifier = new Amplifier() { IntCode = GetLongList_BySeparator(filename, ","), }; var Grid = new Grid(10_000); int prevMove = 1; while (true) { //Grid.Print(30); int move = Grid.GetNextMoveByPrev_RightRule(prevMove); amplifier.IO.AddLast(move); amplifier.WorkUntilHaltOrWaitForInput(); if (amplifier.IsHalt()) { break; } if ((int)amplifier.Output.FindLast(x => true) != 0) { prevMove = move; } if ((int)amplifier.Output.FindLast(x => true) == 2) { Grid.Locations[Grid.CurrentY, Grid.CurrentX] = 2; } Grid.SetLocationAndMove((int)amplifier.Output.FindLast(x => true), move); if (Grid.IsAtStart()) { break; } } Grid.Print(30); Grid.FillOxygen(); }
public void RunPart2() { string[] lines = File.ReadAllLines("Data/Day09.txt"); string line = lines[0]; Int64[] numbers; numbers = line.Split(',').Select(x => Int64.Parse(x)).ToArray(); Array.Resize(ref numbers, numbers.Length * 10); Amplifier amplifier = new Amplifier('A', numbers); amplifier.Inputs.Add(2); amplifier.Calculate(); Console.WriteLine("Part 2: " + amplifier.Output); }
public void RunPart2() { string[] lines = File.ReadAllLines("Data/Day11.txt"); string line = lines[0]; Int64[] numbers; numbers = line.Split(',').Select(x => Int64.Parse(x)).ToArray(); Array.Resize(ref numbers, numbers.Length * 10); Console.WriteLine("Part 2: "); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { board[x, y] = 1; } } amplifier = new Amplifier('A', numbers); while (!amplifier.IsDone) { this.amplifier.Inputs.Add(board[positionX, positionY] == 1 ? 1 : 0); var instructions = amplifier.Calculate(); if (!amplifier.IsDone) { doMove(instructions); } } for (int y = 68; y < 107; y++) { string writeLine = ""; for (int x = 70; x < 76; x++) { writeLine += board[x, y] == 1 ? "X" : " "; } Console.WriteLine(writeLine); } }
public void RunPart1() { string[] lines = File.ReadAllLines("Data/Day09.txt"); //lines = new string[] { "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99" }; //lines = new string[] { "1102,34915192,34915192,7,4,7,99,0" }; //lines = new string[] { "104,1125899906842624,99" }; string line = lines[0]; Int64[] numbers; numbers = line.Split(',').Select(x => Int64.Parse(x)).ToArray(); Array.Resize(ref numbers, numbers.Length * 10); Amplifier amplifier = new Amplifier('A', numbers); amplifier.Inputs.Add(1); amplifier.Calculate(); Console.WriteLine("Part 1: " + amplifier.Output); }
public override void Start1() { var filename = GetFileName1(); var amplifier = new Amplifier() { IntCode = GetLongList_BySeparator(filename, ","), }; while (!amplifier.IsHalt()) { amplifier.WorkUntilHaltOrWaitForInput(); if (amplifier.IsHalt()) { break; } } var output = amplifier.Output; var screen = new Screen(50); for (int i = 0; i < output.Count; i += 3) { screen.Grid[output[i], output[i + 1]] = output[i + 2]; } long blockTiles = 0; foreach (var tile in screen.Grid) { if (tile == 2) { blockTiles++; } } Console.WriteLine(blockTiles); }
public override void Start2() { var filename = GetFileName1(); //1250 101 //7091148 high //7081149 high/ //wrong 6991133 int N = 1250, M = 1300; var grid = new Grid(N, M); int prevStart = 0; int prevEnd = 1; for (int i = 0; i < N; i++) { if (prevEnd < prevStart) { prevEnd = prevStart; } bool started = false; for (int j = prevStart; j < M; j++) { if (i < 10 && j > 10) { break; } var amplifier = new Amplifier() { IntCode = GetLongList_BySeparator(filename, ","), }; if (grid.CheckPoint(amplifier, j, i) == 1) { if (!started) { prevStart = j; for (int j1 = j; j1 <= prevEnd; j1++) { grid.Array[i, j1] = 1; } j = prevEnd - 1;//??? } started = true; } else { if (started) { prevEnd = j; break; } } } //grid.Print(30); if (grid.GetMaxSquareFromLast() >= 100) { grid.Print(100); int firstJ = grid.GetFirst1InLastFilledLine(); Console.WriteLine((firstJ - 1) * 10000 + (i - 100)); } //if (i %10==0) //{ // grid.Print(50); // Console.WriteLine(grid.GetMaxSquareFromLast()); //} } grid.Print(30); Console.WriteLine(grid.GetLastWidth()); Console.WriteLine(grid.GetLastHeight()); Console.WriteLine(grid.GetMaxSquare()); }
public override void Start2() { var filename = GetFileName1(); var amplifier = new Amplifier() { IntCode = GetLongList_BySeparator(filename, ","), }; amplifier.IntCode[0] = 2; var screen = new Screen(40); int nextDrawPosition = 0; long step = 0; while (true) { step++; if (amplifier.Output.Count > nextDrawPosition) { screen.AddMoves(amplifier.Output.GetRange(nextDrawPosition, amplifier.Output.Count - nextDrawPosition)); nextDrawPosition = amplifier.Output.Count; } //ConsoleKeyInfo _Key = Console.ReadKey(); //switch (_Key.Key) //{ // case ConsoleKey.RightArrow: // amplifier.IO.AddLast(1); // break; // case ConsoleKey.LeftArrow: // amplifier.IO.AddLast(-1); // break; // default: // amplifier.IO.AddLast(0); // break; //} long current3X = screen.Get3X(); long current4X = screen.Get4X(); if (current4X > current3X) { amplifier.IO.AddLast(1); } else if (current4X < current3X) { amplifier.IO.AddLast(-1); } else { amplifier.IO.AddLast(0); } //if (step % 1 == 0) //{ // screen.PrintScreen(); // Console.ReadKey(); //} amplifier.WorkUntilHaltOrWaitForInput(); if (amplifier.IsHalt()) { break; } } Console.Clear(); Console.WriteLine("Game over"); Console.WriteLine($"Score = {amplifier.Output.Last()}"); }
private void doMoves(State[,] board, long size, int cursorTop, ref long currentX, ref long currentY, ref long previousX, ref long previousY, Amplifier amplifier, List <Tuple <long, long, int, Direction> > stepsOnPath, ref int maxPath, ref int maxPathCurrent, bool print, bool part1) { while (true) { State north = board[currentX, currentY - 1]; State east = board[currentX + 1, currentY]; State south = board[currentX, currentY + 1]; State west = board[currentX - 1, currentY]; int amountOfWays = 0; if (north == State.Way) { amountOfWays++; } if (east == State.Way) { amountOfWays++; } if (south == State.Way) { amountOfWays++; } if (west == State.Way) { amountOfWays++; } Direction directionToGo; if (north == State.Unknown) { directionToGo = Direction.N; } else if (east == State.Unknown) { directionToGo = Direction.E; } else if (south == State.Unknown) { directionToGo = Direction.S; } else if (west == State.Unknown) { directionToGo = Direction.W; } else { // No where to go (means that part 2 is finished) if (amountOfWays == 0) { break; } // Dead end else if (amountOfWays == 1) { if (maxPathCurrent > maxPath) { maxPath = maxPathCurrent; } Tuple <long, long, int, Direction> previousStep = stepsOnPath.LastOrDefault(); if (previousStep != null) { board[previousStep.Item1, previousStep.Item2] = State.DeadEnd; stepsOnPath.Remove(previousStep); maxPathCurrent -= 2; } if (north == State.Way) { directionToGo = Direction.N; } else if (east == State.Way) { directionToGo = Direction.E; } else if (south == State.Way) { directionToGo = Direction.S; } else if (west == State.Way) { directionToGo = Direction.W; } else { throw new NotSupportedException(); } } else { // Moved up or down from last move if (currentX == previousX) { // Last move was move to the top if (previousY > currentY) { directionToGo = Direction.N; } // Last move was move to the bottom else { directionToGo = Direction.S; } } // Moved left or right from last move else { // Last move was move to the left if (previousX > currentX) { directionToGo = Direction.W; } // Last move was move to the right else { directionToGo = Direction.E; } } } } amplifier.Inputs.Add((int)directionToGo); amplifier.Calculate(); Tuple <long, long, bool> result = this.finishMovement(board, stepsOnPath, amountOfWays, currentX, currentY, directionToGo, amplifier.Output); if (previousX != currentX || previousY != currentY) { maxPathCurrent++; } previousX = currentX; previousY = currentY; currentX = result.Item1; currentY = result.Item2; if (part1) { // Station found if (result.Item3) { break; } } if (print) { this.print(board, size, currentX, currentY, cursorTop, maxPath, maxPathCurrent); Thread.Sleep(100); } } }
public override async Task Run() { string[] lines = File.ReadAllLines("Data/Day15.txt"); //lines = new string[] { "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99" }; //lines = new string[] { "1102,34915192,34915192,7,4,7,99,0" }; //lines = new string[] { "104,1125899906842624,99" }; string line = lines[0]; Int64[] numbers; numbers = line.Split(',').Select(x => Int64.Parse(x)).ToArray(); Array.Resize(ref numbers, numbers.Length * 10); long size = 50; State[,] board = new State[size, size]; long currentX = size / 2; long currentY = size / 2; long startX = size / 2; long startY = size / 2; long previousX = size / 2; long previousY = size / 2; int cursorTop = Console.CursorTop; int part1WayCounter = 0; int part2MaxPath = 0; int part2MaxPathCurrent = 0; Amplifier amplifier = new Amplifier('A', numbers); board[currentX, currentY] = State.Way; List <Tuple <long, long, int, Direction> > stepsOnPath = new List <Tuple <long, long, int, Direction> >(); doMoves(board, size, cursorTop, ref currentX, ref currentY, ref previousX, ref previousY, amplifier, stepsOnPath, ref part2MaxPath, ref part2MaxPathCurrent, false, true); //this.print(board, size, currentX, currentY, cursorTop, 0, 0); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (board[x, y] == State.Way) { part1WayCounter++; } } } Console.WriteLine("Part 1: " + part1WayCounter + " (station at [" + currentX + "," + currentY + "])"); cursorTop = Console.CursorTop; part2MaxPath = 0; part2MaxPathCurrent = 0; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { board[x, y] = State.Unknown; } } board[currentX, currentY] = State.Way; //this.print(board, size, currentX, currentY, cursorTop, 0, 0); doMoves(board, size, cursorTop, ref currentX, ref currentY, ref previousX, ref previousY, amplifier, stepsOnPath, ref part2MaxPath, ref part2MaxPathCurrent, false, false); Console.WriteLine("Part 2: " + (part2MaxPath - 1)); }