Exemple #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));
        }
Exemple #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();
        }
Exemple #3
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));
        }
Exemple #4
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();
        }
Exemple #5
0
        public long Answer(params long[] arguments)
        {
            runProgram();
            IntCodeProcessor2 icp2 = new IntCodeProcessor2(program2);

            icp2.Run();

            Console.WriteLine($"New processor: {icp2.Memory[0]}");
            Console.WriteLine($"Old processor: {program[0]}");

            return(program[0]);
        }
Exemple #6
0
        public long Answer(params long[] arguments)
        {
            try
            {
                IntCodeProcessor2 icp2 = new IntCodeProcessor2(programLong);

                icp2.Run();
                Console.WriteLine($"New output {icp2.Memory[0]}");

                runProgram();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            return(0);
        }
Exemple #7
0
        private long[] test8 = { 109, 1, 203, 2, 204, 2, 99 };     // outputs the input

        public long Answer(params long[] arguments)
        {
            //Console.WriteLine(processor.RunCode(code.ToList(), new int[] {}));
            IntCodeProcessor2 icp2;

            //Day 1
            icp2 = new IntCodeProcessor2(program)
            {
                Debug = false, InputModeSetting = IntCodeProcessor2.InputMode.Set, InputNumbers = { 1 }
            };

            icp2.Run();

            // Day 2
            icp2 = new IntCodeProcessor2(program)
            {
                Debug = false, InputModeSetting = IntCodeProcessor2.InputMode.Set, InputNumbers = { 2 }
            };

            icp2.Run();

            // Tests
            bool debug = false;

            icp2 = new IntCodeProcessor2(test1)
            {
                Debug = debug
            };
            icp2.Run();

            icp2 = new IntCodeProcessor2(test2)
            {
                Debug = debug
            };
            icp2.Run();

            icp2 = new IntCodeProcessor2(test3)
            {
                Debug = debug
            };
            icp2.Run();

            icp2 = new IntCodeProcessor2(test4)
            {
                Debug = debug
            };
            icp2.Run();

            icp2 = new IntCodeProcessor2(test5)
            {
                Debug = debug
            };
            icp2.Run();

            icp2 = new IntCodeProcessor2(test6)
            {
                Debug = debug
            };
            icp2.Run();

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

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


            return(0);
        }
Exemple #8
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);
        }
Exemple #9
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);
        }