Example #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));
        }
Example #2
0
        public void RunTest()
        {
            icp2 = new IntCodeProcessor2(test1)
            {
                Debug = false
            };
            // icp2.Run();
            Assert.AreEqual(icp2.RunWithOutput(), -1);

            icp2 = new IntCodeProcessor2(test2)
            {
                Debug = false
            };
            // icp2.Run();
            Assert.AreEqual(icp2.RunWithOutput(), 1);

            icp2 = new IntCodeProcessor2(test3)
            {
                Debug = false
            };
            // icp2.Run();
            Assert.AreEqual(icp2.RunWithOutput(), 109);

            icp2 = new IntCodeProcessor2(test4)
            {
                Debug = false
            };
            // icp2.Run();
            Assert.AreEqual(icp2.RunWithOutput(), 204);

            icp2 = new IntCodeProcessor2(test5)
            {
                Debug = false
            };
            // icp2.Run();
            Assert.AreEqual(icp2.RunWithOutput(), 204);

            icp2 = new IntCodeProcessor2(test6)
            {
                Debug = false
            };
            // icp2.Run();
            Assert.AreEqual(icp2.RunWithOutput(), 204);

            icp2 = new IntCodeProcessor2(test7)
            {
                Debug = false, InputModeSetting = IntCodeProcessor2.InputMode.Set, InputNumbers = { 32 }
            };
            icp2.Run();

            icp2 = new IntCodeProcessor2(test8)
            {
                Debug = false, InputModeSetting = IntCodeProcessor2.InputMode.Set, InputNumbers = { 32 }
            };
            icp2.Run();

            // Assert.Fail();
        }
Example #3
0
        public long Answer(params long[] arguments)
        {
            icp2 = new IntCodeProcessor2(program)
            {
                InputModeSetting = IntCodeProcessor2.InputMode.Console
            };

            StringBuilder b = new StringBuilder();

            int x = 0;
            int y = 0;

            while (icp2.IsRunning)
            {
                var output = icp2.RunWithOutput();

                char s = (char)output;

                map.Add(new Point(x, y), s);

                // x ++
                // add to map
                x++;

                if (s == 10)
                {
                    y++;
                    x = 0;
                    // x = 0
                }

                b.Append(s);
            }

            // scan the map to find crosses

            Console.Write(b.ToString());

            // Scan map for the path '#'
            // Check north south east and west if it contains also a '#'
            // sum all keys ( x * y )

            var l = map.Where(m => m.Value == '#').Where(m =>
            {
                bool up    = map.TryGetValue(m.Key + new Point(0, 1), out int v) && v == '#';
                bool down  = map.TryGetValue(m.Key + new Point(0, -1), out int v2) && v2 == '#';
                bool left  = map.TryGetValue(m.Key + new Point(1, 0), out int v3) && v3 == '#';
                bool right = map.TryGetValue(m.Key + new Point(-1, 0), out int v4) && v4 == '#';

                return(up && down && left && right);
            }).Sum(m => m.Key.x * m.Key.y);

            return(l);//map.Where(x => x.Value == '#').Sum();
        }
Example #4
0
        private Point Move(Direction dir, Point current, out bool hit)
        {
            Point add = DirToPoint(dir);

            icp2.InputNumbers.Add((int)dir);
            var x = icp2.RunWithOutput();


            if (!map.ContainsKey(current + add))
            {
                map.Add(current + add, 0);
            }

            hit = false;
            switch (x)
            {
            case 1:     // nothing in the way

                // Move

                current.x += add.x;
                current.y += add.y;
                hit        = false;
                // Add now location

                break;

            case 0:     // wall in the way didnt move

                map[current + add] = 1;
                hit = true;

                break;

            case 2:     // found leak

                map[current + add] = 2;
                current.x         += add.x;
                current.y         += add.y;
                hit = false;
                // Stop?
                break;
            }

            return(current);
        }
Example #5
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);
        }
Example #6
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);
        }