Ejemplo n.º 1
0
        public long Answer(params long[] arguments)
        {
            IntCodeProcessor2 icp2 = new IntCodeProcessor2(program)
            {
                InputModeSetting = IntCodeProcessor2.InputMode.Set
            };

            icp2.Reset();

            while (icp2.IsRunning)
            {
                var x      = icp2.RunWithOutput();
                var y      = icp2.RunWithOutput();
                var tileId = icp2.RunWithOutput();

                var p = new Point((int)x, (int)y);
                if (!screen.ContainsKey(p))
                {
                    screen.Add(p, (int)tileId);
                }
                else
                {
                    screen[p] = (int)tileId;
                }
            }

            DebugMap();

            return(screen.Sum(s => s.Value == 2 ? 1 : 0));
        }
Ejemplo n.º 2
0
        public long Answer(params long[] arguments)
        {
            icp2 = new IntCodeProcessor2(program)
            {
                InputModeSetting = IntCodeProcessor2.InputMode.Set
            };

            icp2.Reset();
            Point location = new Point(0, 0);


            map.Add(location, 0);
            // We know left west is free
            Direction dir = Direction.North;

            Look(location);

            while (map[location] != 2)
            {
                location = Move(dir, location, out var hit);

                Look(location);

                // Can we go right?
                var checkRightKey = location + DirToPoint(RightDirection(dir));
                if (!map.ContainsKey(checkRightKey))
                {
                    Look(checkRightKey);
                }
                if (map[checkRightKey] != 1)
                {
                    dir = RightDirection(dir);
                    continue;
                }

                // can we go straigh?
                if (map[location + DirToPoint(dir)] != 1)
                {
                    continue;
                }

                // Can we go left?
                if (map[location + DirToPoint(OppositeDirection(RightDirection(dir)))] != 1)
                {
                    dir = OppositeDirection(RightDirection(dir));
                    continue;
                }

                dir = OppositeDirection(dir);
            }

            Frame(location);
            Point end = map.First(x => x.Value == 2).Key;

            return(shortestPath(new Point(0, 0), end));
        }
Ejemplo n.º 3
0
        public long Answer(params long[] arguments)
        {
            IntCodeProcessor2 icp2 = new IntCodeProcessor2(program)
            {
                InputModeSetting = IntCodeProcessor2.InputMode.Set
            };

            int score      = 0;
            int paddle     = 0;
            int ball       = 0;
            int controller = 0;

            icp2.Reset();

            while (icp2.IsRunning) // Load first frame
            {
                var x      = icp2.RunWithOutput();
                var y      = icp2.RunWithOutput();
                var tileId = icp2.RunWithOutput();


                var p = new Point((int)x, (int)y);
                if (!screen.ContainsKey(p))
                {
                    screen.Add(p, (int)tileId);
                }
                else
                {
                    screen[p] = (int)tileId;
                }

                if ((x == 36 && y == 21))
                {
                    DebugMap();
                    break;
                }
            }

            ball       = screen.First(b => b.Value == 4).Key.x;
            paddle     = screen.First(b => b.Value == 3).Key.x;
            controller = ball <paddle ? -1 : ball> paddle ? 1 : 0;
            icp2.InputNumbers.Add(controller);

            while (icp2.IsRunning)
            {
                var x      = icp2.RunWithOutput();
                var y      = icp2.RunWithOutput();
                var tileId = icp2.RunWithOutput();

                if (x == -1 && y == 0)
                {
                    score = (int)tileId;
                }
                else
                {
                    var p = new Point((int)x, (int)y);
                    if (!screen.ContainsKey(p))
                    {
                        screen.Add(p, (int)tileId);
                    }
                    else
                    {
                        screen[p] = (int)tileId;
                    }

                    //Console.WriteLine($"{p} {tileId}");

                    if ((x == 36 && y == 21) || tileId == 4)
                    {
                        DebugMap();
                        ball       = screen.First(b => b.Value == 4).Key.x;
                        paddle     = screen.First(b => b.Value == 3).Key.x;
                        controller = ball <paddle ? -1 : ball> paddle ? 1 : 0;
                        icp2.InputNumbers.Add(controller);
                    }
                }
            }


            return(score);
        }
Ejemplo n.º 4
0
        public long Answer(params long[] arguments)
        {
            IntCodeProcessor2 icp2 = new IntCodeProcessor2(program)
            {
                InputModeSetting = IntCodeProcessor2.InputMode.Set, Debug = true
            };

            Point currentLocation  = new Point(0, 0);
            int   currentDirection = 0;

            //DebugMap(currentLocation, currentDirection);
            panels.Add(currentLocation, 1);
            icp2.Reset();

            while (icp2.IsRunning)
            {
                if (!panels.ContainsKey(currentLocation)) // Undiscovered tile? color is black (0)
                {
                    panels.Add(currentLocation, 0);
                }

                int    input = panels[currentLocation]; // Read current tile color
                string c     = input == 0 ? "Black" : "White";
                Console.WriteLine($"Input is {c} at {currentLocation}");

                icp2.InputNumbers.Add(input); // Add it to the input
                var output1 = icp2.RunWithOutput();
                var output2 = icp2.RunWithOutput();

                var paintBlack = output1 == 0;
                var turnLeft   = output2 == 0;

                string s = paintBlack ? "Black" : "White";
                Console.WriteLine($"Painting panel at {currentLocation} the color {s} ");

                if (!panels.ContainsKey(currentLocation))
                {
                    panels.Add(currentLocation, paintBlack ? 0 : 1);
                }
                else
                {
                    panels[currentLocation] = paintBlack ? 0 : 1;
                }

                string d = turnLeft ? "Left" : "Right";
                Console.WriteLine($"Turing to the {d}");

                if (turnLeft)
                {
                    currentDirection--;
                    if (currentDirection < 0)
                    {
                        currentDirection = 3;
                    }
                }
                else
                {
                    currentDirection++;
                    if (currentDirection > 3)
                    {
                        currentDirection = 0;
                    }
                }

                Console.WriteLine($"Moving forward");
                switch (currentDirection)
                {
                case 0:     // up
                    currentLocation.y++;
                    break;

                case 1:     // right
                    currentLocation.x++;
                    break;

                case 2:     // down
                    currentLocation.y--;
                    break;

                case 3:     // left
                    currentLocation.x--;
                    break;
                }

                //DebugMap(currentLocation, currentDirection);
            }
            DebugMap(currentLocation, currentDirection);

            return(panels.Count);
        }