Beispiel #1
0
        public override async Task Part2()
        {
            var intComputer = new Service.IntCodeComputer(ComputerMemoryInput, 2);

            intComputer.Start();
            ResultPart2 = intComputer.LocalComputerMemory.ProgramValue.ToString();
        }
Beispiel #2
0
        public override async Task Part2()
        {
            var paddle = new Coordinate();
            var ball   = new Coordinate();

            ComputerMemoryInput[0] = 2;
            var intComputer = new Service.IntCodeComputer(ComputerMemoryInput, 0);
            var score       = (long)0;
            var input       = (long)0;

            while (true)
            {
                var values = intComputer.StartAndReturn3Outputs(input);
                var tile   = new Tile()
                {
                    Coordinate = new Coordinate()
                    {
                        X = values[0], Y = values[1]
                    }, TileType = (TileType)values[2]
                };
                if (tile.Coordinate.X == -1 && tile.Coordinate.Y == 0)
                {
                    score = values[2];
                }
                else if (tile.TileType == TileType.Paddle)
                {
                    paddle = new Coordinate()
                    {
                        X = values[0], Y = values[1]
                    };
                }
                else if (tile.TileType == TileType.Ball)
                {
                    ball = new Coordinate()
                    {
                        X = values[0], Y = values[1]
                    };
                }

                input = Math.Max(-1, Math.Min((ball.X - paddle.X), 1));

                if (values[3] == 1)
                {
                    break;
                }
            }
            ResultPart2 = score.ToString();
        }
Beispiel #3
0
        public override async Task TestPart1()
        {
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "21", PuzzleString = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99", OutputValue = "21"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "33", PuzzleString = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99", OutputValue = "33"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "4", PuzzleString = "1102,34915192,34915192,7,4,7,99,0", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "4", PuzzleString = "104,1125899906842624,99", OutputValue = "1125899906842624"
            });

            foreach (var tito in TestInputOutput)
            {
                var puzzleinput = tito.PuzzleString.Split(',').Select(x => Convert.ToInt64(x)).ToList();
                var intComputer = new Service.IntCodeComputer(puzzleinput, Convert.ToInt32(tito.InputValue));
                intComputer.Start();
                if (tito.OutputValue == intComputer.LocalComputerMemory.ProgramValue.ToString())
                {
                    Debug.WriteLine($"INPUT:{tito.InputValue} - {tito.PuzzleString} - OUTPUT:{tito.OutputValue} = OK");
                }
                else
                {
                    Debug.WriteLine($"INPUT:{tito.InputValue} - {tito.PuzzleString} - OUTPUT:{tito.OutputValue} = NOK");
                    puzzleinput = tito.PuzzleString.Split(',').Select(x => Convert.ToInt64(x)).ToList();
                    var intComputer2 = new Service.IntCodeComputer(puzzleinput, Convert.ToInt32(tito.InputValue));
                    intComputer2.Start();
                }
                intComputer = null;
                puzzleinput = null;
            }
        }
Beispiel #4
0
        public override async Task Part1()
        {
            var intComputer = new Service.IntCodeComputer(ComputerMemoryInput, 0);
            var stride      = 300;
            var grid        = new int[stride, stride];
            var shiftX      = 150;
            var shiftY      = 150;
            var listTiles   = new Dictionary <TileType, int>();

            while (true)
            {
                var values = intComputer.StartAndReturn3Outputs();
                var tile   = new Tile()
                {
                    Coordinate = new Coordinate()
                    {
                        X = values[0], Y = values[1]
                    }, TileType = (TileType)values[2]
                };

                if (!listTiles.TryAdd(tile.TileType, 1))
                {
                    listTiles[tile.TileType] += 1;
                }
                ;

                if (values[3] == 1)
                {
                    break;
                }
                grid[tile.Coordinate.X + shiftX, tile.Coordinate.Y + shiftY] = (int)tile.TileType;
            }

            var blockTiles = listTiles[TileType.Block];

            ResultPart1 = blockTiles.ToString();
        }
Beispiel #5
0
        public override async Task Part2()
        {
            var intComputer     = new Service.IntCodeComputer(ComputerMemoryInput, 0);
            var inputValue      = 0;
            var robot           = new Robot();
            var stride          = 300;
            var grid            = new int[stride, stride];
            var currentPosition = new Coordinate()
            {
                X = 0, Y = 0
            };
            var shiftX = 150;
            var shiftY = 150;
            var listPaintedCoordinates = new Dictionary <CurrentCoordinate, int>();
            var firstwhite             = true;

            while (true)
            {
                try
                {
                    var currentColor = (Color)grid[currentPosition.X + shiftX, currentPosition.Y + shiftY];
                    if (firstwhite)
                    {
                        currentColor = Color.White;
                        firstwhite   = false;
                    }

                    var values = intComputer.StartAndReturn2Outputs((int)currentColor);

                    if (values[2] == 1)
                    {
                        break;
                    }

                    grid[currentPosition.X + shiftX, currentPosition.Y + shiftY] = (int)values[0];
                    var coord = new CurrentCoordinate()
                    {
                        X = currentPosition.X, Y = currentPosition.Y
                    };
                    if (listPaintedCoordinates.TryAdd(new CurrentCoordinate()
                    {
                        X = currentPosition.X, Y = currentPosition.Y
                    }, 1))
                    {
                        listPaintedCoordinates[coord] += 1;
                    }
                    ;
                    //robot.Paint(values[0]);
                    robot.DetermineDirection((Direction)values[1]);
                    robot.Move();
                    Debug.WriteLine($"{values[0]} - {values[1]}");
                    currentPosition = robot.GetCurrentCoordinate();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }

            var paintedSquares = listPaintedCoordinates.Count();

            var puzzleAnswerBuilder = new StringBuilder();

            for (int y = stride - 1; y >= 0; y--)
            {
                for (int x = 0; x < stride; x++)
                {
                    puzzleAnswerBuilder.Append(grid[x, y] == 0 ? ' ' : '*');
                }
                puzzleAnswerBuilder.AppendLine();
            }
            Debug.WriteLine(puzzleAnswerBuilder.ToString());

            ResultPart2 = "hczrugaz";
        }
Beispiel #6
0
        public override async Task TestPart1()
        {
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "2", PuzzleString = "3,9,8,9,10,9,4,9,99,-1,8", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "5", PuzzleString = "3,9,8,9,10,9,4,9,99,-1,8", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "8", PuzzleString = "3,9,8,9,10,9,4,9,99,-1,8", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "3", PuzzleString = "3,9,7,9,10,9,4,9,99,-1,8", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "18", PuzzleString = "3,9,7,9,10,9,4,9,99,-1,8", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "10", PuzzleString = "3,3,1108,-1,8,3,4,3,99", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "8", PuzzleString = "3,3,1108,-1,8,3,4,3,99", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "2", PuzzleString = "3,3,1107,-1,8,3,4,3,99", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "8", PuzzleString = "3,3,1107,-1,8,3,4,3,99", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "9", PuzzleString = "3,3,1107,-1,8,3,4,3,99", OutputValue = "0"
            });


            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "0", PuzzleString = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "9", PuzzleString = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "33", PuzzleString = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "0", PuzzleString = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1", OutputValue = "0"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "9", PuzzleString = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1", OutputValue = "1"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "33", PuzzleString = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1", OutputValue = "1"
            });


            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "2", PuzzleString = @"3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", OutputValue = "999"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "8", PuzzleString = @"3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", OutputValue = "1000"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "9", PuzzleString = @"3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", OutputValue = "1001"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "33", PuzzleString = @"3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", OutputValue = "1001"
            });
            TestInputOutput.Add(new InputOutput()
            {
                InputValue = "0", PuzzleString = @"3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99", OutputValue = "999"
            });

            foreach (var tito in TestInputOutput)
            {
                var puzzleinput = tito.PuzzleString.Split(',').Select(x => Convert.ToInt64(x)).ToList();
                var intComputer = new Service.IntCodeComputer(puzzleinput, Convert.ToInt32(tito.InputValue));
                intComputer.Start();
                if (tito.OutputValue == intComputer.LocalComputerMemory.ProgramValue.ToString())
                {
                    Debug.WriteLine($"INPUT:{tito.InputValue} - {tito.PuzzleString} - OUTPUT:{tito.OutputValue} = OK");
                }
                else
                {
                    Debug.WriteLine($"INPUT:{tito.InputValue} - {tito.PuzzleString} - OUTPUT:{tito.OutputValue} = NOK");
                    puzzleinput = tito.PuzzleString.Split(',').Select(x => Convert.ToInt64(x)).ToList();
                    var intComputer2 = new Service.IntCodeComputer(puzzleinput, Convert.ToInt32(tito.InputValue));
                    intComputer2.Start();
                }
                intComputer = null;
                puzzleinput = null;
            }
        }