Exemplo n.º 1
0
        static void P1()
        {
            var lines = Utilities.GetStringFromFile("Day13.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            vm.WriteProgram(lines);
            //vm.WriteMemory(0, 1);
            vm.RunProgram();
            processOutputs(vm);
            Draw(grid);

            var blockcount = 0;

            for (int i = 0; i < grid.GetLength(0); i++)
            {
                for (int j = 0; j < grid.GetLength(1); j++)
                {
                    if (grid[i, j] == 'B')
                    {
                        blockcount++;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void P1()
        {
            var lines = Utilities.GetStringFromFile("Day17.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            vm.WriteProgram(lines);

            char[,] charmap = new char[41, 49];

            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
                break;
            }

            string map = "";

            int J = 0;
            int I = 0;

            while (vm.outputs.Count > 0)
            {
                char tile;
                tile = (char)vm.outputs.Dequeue();
                map += tile;
                if (tile == 10)
                {
                    J++;
                    I = 0;
                }
                else
                {
                    charmap[J, I] = tile;
                    I++;
                }
            }

            int score = 0;

            for (int i = 1; i < charmap.GetLength(1) - 1; i++)
            {
                for (int j = 1; j < charmap.GetLength(0) - 1; j++)
                {
                    if (charmap[j, i] == '#' && (charmap[j, i - 1] == '#' && charmap[j, i + 1] == '#' && charmap[j - 1, i] == '#' && charmap[j + 1, i] == '#'))
                    {
                        score += i * j;
                    }
                }
            }

            Console.Write(map);
        }
Exemplo n.º 3
0
        static void processOutputs(IntCodeVM vm)
        {
            blockcount = 0;
            while (vm.outputs.Count > 0)
            {
                var x    = vm.outputs.Dequeue();
                var y    = vm.outputs.Dequeue();
                var tile = vm.outputs.Dequeue();

                if (x == -1 && y == 0)
                {
                    /* score output, not screen */
                    score = tile;
                }
                else
                {
                    char writeTile = '_';
                    switch (tile)
                    {
                    case 0:
                        writeTile = '█';
                        break;

                    case 1:
                        writeTile = 'X';
                        break;

                    case 2:
                        writeTile = 'B';
                        blockcount++;
                        break;

                    case 3:
                        writeTile = '-';
                        paddlepos = x;
                        break;

                    case 4:
                        writeTile = 'O';
                        ballPos   = x;
                        break;

                    default:
                        break;
                    }

                    //write tile to array
                    grid[x, y] = writeTile;
                }
            }
        }
Exemplo n.º 4
0
        static int CheckBeam(int x, int y)
        {
            var lines = Utilities.GetStringFromFile("Day19.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            vm.WriteProgram(lines);
            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
                vm.WriteInput(x);
                vm.WriteInput(y);
            }

            return((int)vm.outputs.Dequeue());
        }
Exemplo n.º 5
0
        static void P2()
        {
            var lines = Utilities.GetStringFromFile("Day13.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            vm.WriteProgram(lines);
            vm.WriteMemory(0, 2);
            processOutputs(vm);
            //Draw(grid);

            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
                processOutputs(vm);

                if (ballPos < paddlepos)
                {
                    vm.WriteInput(-1);
                }
                else if (ballPos > paddlepos)
                {
                    vm.WriteInput(1);
                }
                else
                {
                    vm.WriteInput(0);
                }
                Draw(grid);

                if (blockcount == 0)
                {
                    break;
                }
            }
            processOutputs(vm);
            Draw(grid);
        }
Exemplo n.º 6
0
        public static void P2()
        {
            var lines = Utilities.GetStringFromFile("Day17.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
                FriendlyLogging = false
            });

            vm.WriteProgram(lines);
            vm.WriteMemory(0, 2);

            var input = "65,44,65,44,66,44,67,44,66,44,67,44,66,44,67,44,67,44,65,10,76,44,49,48,44,82,44,56,44,82,44,56,10,76,44,49,48,44,76,44,49,50,44,82,44,56,44,82,44,49,48,10,82,49,48,44,76,49,50,44,82,49,48,10".Split(",").Select(int.Parse).ToList();
            var main  = "65,44,65,44,66,44,67,44,66,44,67,44,66,44,67,44,67,44,65,10".Split(",").Select(int.Parse).ToList();
            var A     = "76,44,49,48,44,82,44,56,44,82,44,56,10".Split(",").Select(int.Parse).ToList();
            var B     = "76,44,49,48,44,76,44,49,50,44,82,44,56,44,82,44,49,48,10".Split(",").Select(int.Parse).ToList();
            var C     = "82,44,49,48,44,76,44,49,50,44,82,44,49,48,10".Split(",").Select(int.Parse).ToList();
            //foreach (var inp in input)
            //{
            //    vm.WriteInput(inp);
            //}
            //vm.WriteInput(110);
            //vm.WriteInput(10);

            var inpPhase = 0;

            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
                while (vm.outputs.Count > 0)
                {
                    Console.Write((char)vm.outputs.Dequeue());
                }
                switch (inpPhase)
                {
                case 0:
                    foreach (var inp in main)
                    {
                        vm.WriteInput(inp);
                    }
                    inpPhase++;
                    break;

                case 1:
                    foreach (var inp in A)
                    {
                        vm.WriteInput(inp);
                    }
                    inpPhase++;
                    break;

                case 2:
                    foreach (var inp in B)
                    {
                        vm.WriteInput(inp);
                    }
                    inpPhase++;
                    break;

                case 3:
                    foreach (var inp in C)
                    {
                        vm.WriteInput(inp);
                    }
                    inpPhase++;
                    break;

                case 4:
                    vm.WriteInput((int)'Y');
                    vm.WriteInput(10);
                    inpPhase++;
                    break;
                }
            }

            var x = vm.outputs.First();
            var y = vm.outputs.Last();
            int e = 0;
            var s = "";

            while (vm.outputs.Count > 0)
            {
                s += (char)vm.outputs.Dequeue();
                e++;
                if (e == 2051)
                {
                    Console.Clear();
                    Console.WriteLine(s);
                    s = "";
                    e = 0;
                    Thread.Sleep(250);
                }
            }

            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
            }
        }
Exemplo n.º 7
0
        static void P1()
        {
            var lines = Utilities.GetStringFromFile("Day15.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            vm.WriteProgram(lines);
            grid[25, 25] = 'S';

            Draw(grid);



            vm.WriteInput((long)Facing.north);

            //vm.WriteMemory(0, 1);
            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
                processOutputs(vm);

                if (complete)
                {
                    break;
                }

                //what type of step was taken?
                switch (stepType)
                {
                case StepType.forward:
                    //was step forward successful?  if not turn left
                    if (!stepSuccess)
                    {
                        TurnDirection(Direction.left);
                    }
                    //attempt to turn right now.
                    stepType = StepType.right;
                    vm.WriteInput((long)GetTurnDirection(Direction.right));
                    break;

                case StepType.right:
                    //if step was successful change facing and try to turn right again
                    if (stepSuccess)
                    {
                        TurnDirection(Direction.right);
                        stepType = StepType.right;
                        vm.WriteInput((long)GetTurnDirection(Direction.right));
                    }
                    //step unsuccessful, continue forward.
                    else
                    {
                        vm.WriteInput((long)currentFacing);
                        stepType = StepType.forward;
                    }
                    break;

                default:
                    break;
                }

                //Draw(grid);
            }

            Draw(grid);

            int[] target = new int[] { 0, 0 };

            for (int i = 0; i < grid.GetLength(0); i++)
            {
                for (int j = 0; j < grid.GetLength(1); j++)
                {
                    if (grid[i, j] == 'O')
                    {
                        target = new int[] { i, j };
                    }
                }
            }

            var s = new seeker(target, 0, grid, new int[] { 25, 25 }, Facing.north);

            var result = s.Seek();

            Console.WriteLine($"Min Steps to get to Oxygen Control: {result.Value}");
        }
Exemplo n.º 8
0
        static void processOutputs(IntCodeVM vm)
        {
            while (vm.outputs.Count > 0)
            {
                var tile    = vm.outputs.Dequeue();
                var nextPos = new int[2];
                curPos.CopyTo(nextPos, 0);
                //assume step success
                stepSuccess = true;

                char writeTile = '.';
                switch (tile)
                {
                case 0:
                    writeTile   = '█';
                    stepSuccess = false;     //step unsuccessful
                    break;

                case 1:
                    writeTile = '.';
                    break;

                case 2:
                    writeTile = 'O';
                    break;

                default:
                    break;
                }

                Facing f = currentFacing;

                if (stepType == StepType.right)
                {
                    f = GetTurnDirection(Direction.right);
                }

                switch (f)
                {
                case Facing.north:
                    nextPos[1]--;
                    break;

                case Facing.south:
                    nextPos[1]++;
                    break;

                case Facing.west:
                    nextPos[0]--;
                    break;

                case Facing.east:
                    nextPos[0]++;
                    break;

                default:
                    break;
                }

                if (nextPos[0] == 25 && nextPos[1] == 25)
                {
                    complete = true;
                    return;
                }

                grid[nextPos[0], nextPos[1]] = writeTile;

                if (writeTile != '█')
                {
                    curPos = nextPos;
                }
            }
        }
Exemplo n.º 9
0
        static void P2()
        {
            var lines = Utilities.GetStringFromFile("Day19.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            var rows = 500;

            char[,] charmap = new char[1200, 1200];

            bool hitbeam   = false;
            int  beamstart = 0;
            int  drawx     = 0;
            int  drawy     = 0;

            for (int i = 4; i < 1200; i++)
            {
                for (int j = 0; j < 1200; j++)
                {
                    var output = CheckBeam(j, i);
                    if (output == 1)
                    {
                        if (!hitbeam)
                        {
                            hitbeam   = true;
                            beamstart = j - 2;
                        }

                        charmap[i, j] = '#';
                        //Draw(charmap);

                        //check if 99 up is valid
                        //if (i > 100)
                        //{
                        //    output = CheckBeam(j, i - 9);
                        //    if (output == 1)
                        //    {
                        //        if (CheckBeam(j + 9, i - 9) == 1)
                        //        {
                        //            Draw(charmap, j - 5, i - 20, 12, 12);
                        //            Console.Write($"{j} , {i - 10}");
                        //            return;
                        //        }

                        //    }
                        //}
                    }
                    else
                    {
                        if (hitbeam)
                        {
                            hitbeam = false;
                            j       = beamstart;
                            i++;
                            if (i == 1200)
                            {
                                break;
                            }
                            continue;
                        }
                    }
                }
            }

            string map = "";

            for (int i = 0; i < charmap.GetLength(1); i++)
            {
                for (int j = 0; j < charmap.GetLength(0); j++)
                {
                    map += (charmap[j, i] == '\0' ? '.' : charmap[j, i]);
                }
                map += Environment.NewLine;
            }


            Console.WriteLine(map);
            //for (int i = 101; i < 120; i++)
            //{
            //    for (int j = 70; j < 100; j++)
            //    {
            //        var output = CheckBeam(j, i);
            //        if (output == 1)
            //        {
            //            charmap[i, j] = '#';
            //        }
            //    }
            //}

            //Draw(charmap, 101, 70, 20, 20);
        }
Exemplo n.º 10
0
        static void P1()
        {
            var lines = Utilities.GetStringFromFile("Day19.txt").SplitLongArrayFromString(',');



            char[,] charmap = new char[1200, 1200];

            Queue <KeyValuePair <int, int> > coords = new Queue <KeyValuePair <int, int> >();

            Queue <KeyValuePair <int, int> > usedcoords = new Queue <KeyValuePair <int, int> >();

            for (int i = 0; i < 1200; i++)
            {
                for (int j = 0; j < 1200; j++)
                {
                    coords.Enqueue(new KeyValuePair <int, int>(i, j));
                }
            }
            Queue <long> outputs = new Queue <long>();

            while (coords.Count > 0)
            {
                Console.WriteLine(coords.Count);
                var vm = new IntCodeVM(new IntCodeVMConfiguration()
                {
                });
                vm.WriteProgram(lines);
                var coord = coords.Dequeue();
                vm.WriteInput(coord.Key);
                vm.WriteInput(coord.Value);

                while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
                {
                    if (coords.Count == 0)
                    {
                        break;
                    }
                }
                outputs.Enqueue(vm.outputs.Dequeue());
                usedcoords.Enqueue(new KeyValuePair <int, int>(coord.Key, coord.Value));
            }



            long count = 0;

            while (outputs.Count > 0)
            {
                var output   = outputs.Dequeue();
                var outcoord = usedcoords.Dequeue();
                charmap[outcoord.Key, outcoord.Value] = output.ToString()[0];
                count += output;
            }

            Draw(charmap);

            var strgen = new StringBuilder();

            for (int i = 0; i < charmap.GetLength(0); i++)
            {
                for (int j = 0; j < charmap.GetLength(1); j++)
                {
                    strgen.Append(charmap[i, j]);
                }
                strgen.Append(Environment.NewLine);
            }

            var s = strgen.ToString();
        }