Beispiel #1
0
        public long Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(21));

            computer.Run();
            var initialOutput = computer.GetAllOutput().ToList();

            if (initialOutput.Any())
            {
                StringBuilder outputBuilder = new StringBuilder();
                foreach (var o in initialOutput)
                {
                    outputBuilder.Append((char)o);
                }

                Console.WriteLine(outputBuilder.ToString());
            }

            List <int>    input   = new List <int>();
            List <string> program = new List <string>();

            program.Add("OR E J");
            program.Add("OR H J");
            program.Add("AND D J");
            program.Add("OR A T");
            program.Add("AND B T");
            program.Add("AND C T");
            program.Add("NOT T T");
            program.Add("AND T J");
            program.Add("RUN");

            foreach (string s in program)
            {
                foreach (char c in s)
                {
                    input.Add((int)c);
                }
                input.Add(10);
            }

            foreach (var i in input)
            {
                computer.EnqueueInput(i);
                computer.Run();
                var output = computer.GetAllOutput().ToList();
                if (output.Any())
                {
                    return(output.Last());
                }
            }

            return(0);
        }
Beispiel #2
0
        public int Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(5));

            computer.EnqueueInput(5);
            computer.Run();

            return((int)computer.GetAllOutput().Last());
        }
Beispiel #3
0
        public long Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(9));

            computer.EnqueueInput(2);
            computer.Run();

            return(computer.GetAllOutput().Last());
        }
Beispiel #4
0
        public int Part1()
        {
            List <int> results = new List <int>();

            List <int> values = new List <int> {
                0, 1, 2, 3, 4
            };
            var permutations = values.Permute();

            foreach (var permutation in permutations)
            {
                IntcodeComputer a = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer b = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer c = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer d = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer e = new IntcodeComputer(InputUtils.GetFileName(7));

                a.EnqueueInput(permutation.ToList()[0]);
                b.EnqueueInput(permutation.ToList()[1]);
                c.EnqueueInput(permutation.ToList()[2]);
                d.EnqueueInput(permutation.ToList()[3]);
                e.EnqueueInput(permutation.ToList()[4]);

                a.EnqueueInput(0);
                a.Run();
                b.EnqueueInput(a.GetAllOutput().First());
                b.Run();
                c.EnqueueInput(b.GetAllOutput().First());
                c.Run();
                d.EnqueueInput(c.GetAllOutput().First());
                d.Run();
                e.EnqueueInput(d.GetAllOutput().First());
                e.Run();

                results.Add((int)e.GetAllOutput().First());
            }

            return(results.Max());
        }
Beispiel #5
0
        public int Part1()
        {
            Dictionary <Point, char> grid = new Dictionary <Point, char>();

            int count = 0;

            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(19));

            for (int x = 0; x < 50; x++)
            {
                for (int y = 0; y < 50; y++)
                {
                    computer.Reboot();

                    // give x, y as input
                    computer.EnqueueInput(x);
                    computer.EnqueueInput(y);

                    computer.Run();
                    var output = computer.GetAllOutput().ToList();

                    Point p = new Point(x, y);
                    if (output.Any() && output.Last() == 1)
                    {
                        grid.Add(p, '#');
                        count++;
                    }
                    else
                    {
                        grid.Add(p, '.');
                    }
                }
            }

            return(count);
        }
Beispiel #6
0
        public int Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(17));

            computer.SetMemory(0, 2);

            List <int> input = new List <int>();

            // did this part by hand on pen and paper :)

            string mainRoutine = "B,C,C,A,B,C,A,B,C,A";

            foreach (char c in mainRoutine)
            {
                input.Add((int)c);
            }
            input.Add(10);

            // R = 82, L = 76
            // L,6,L,10,L,10,R,6
            string A = "L,6,L,10,L,10,R,6";

            foreach (char c in A)
            {
                input.Add((int)c);
            }
            input.Add(10);

            string B = "L,6,R,12,L,4,L,6";

            foreach (char c in B)
            {
                input.Add((int)c);
            }
            input.Add(10);

            string C = "R,6,L,6,R,12";

            foreach (char c in C)
            {
                input.Add((int)c);
            }
            input.Add(10);

            input.Add((int)'n');
            input.Add(10);

            StringBuilder outputBuilder = new StringBuilder();

            foreach (var i in input)
            {
                computer.EnqueueInput(i);
                computer.Run();
                var output = computer.GetAllOutput();
                if (output.Any())
                {
                    foreach (var o in output)
                    {
                        outputBuilder.Append(o.ToString() + ",");
                    }

                    //Console.WriteLine(outputBuilder.ToString());
                }
            }

            int result = int.Parse(outputBuilder.ToString().TrimEnd(',').Split(',').Last());

            return(result);
        }
Beispiel #7
0
        public int Part1()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(17));

            computer.Run();
            var           initialOutput = computer.GetAllOutput();
            StringBuilder outputBuilder = new StringBuilder();

            if (initialOutput.Any())
            {
                foreach (var o in initialOutput)
                {
                    if ((char)o == '.' || (char)o == '#' || o == 10)
                    {
                        outputBuilder.Append(((char)o).ToString());
                    }
                }

                //Console.WriteLine(outputBuilder.ToString());
            }

            string inputGrid = outputBuilder.ToString();
            var    lines     = inputGrid.Split(Environment.NewLine).ToList();

            lines.RemoveAll(p => p.Equals(String.Empty));

            Dictionary <Point, char> grid = new Dictionary <Point, char>();

            for (int y = 0; y < lines.Count; y++)
            {
                for (int x = 0; x < lines[0].Length; x++)
                {
                    grid.Add(new Point(x, y), lines[y][x]);
                }
            }

            // count the number of crossings in the grid, and add
            // up the "score"
            int xMax = grid.Keys.Max(p => p.X);
            int yMax = grid.Keys.Max(p => p.Y);

            int score = 0;

            for (int x = 0; x <= xMax; x++)
            {
                for (int y = 0; y <= yMax; y++)
                {
                    Point thisPoint = new Point(x, y);
                    if (grid.ContainsKey(thisPoint) && grid[thisPoint] == '#')
                    {
                        if (grid.ContainsKey(thisPoint.PointInDirection(Direction.North)) &&
                            grid.ContainsKey(thisPoint.PointInDirection(Direction.East)) &&
                            grid.ContainsKey(thisPoint.PointInDirection(Direction.West)) &&
                            grid.ContainsKey(thisPoint.PointInDirection(Direction.South)))
                        {
                            if (grid[thisPoint.PointInDirection(Direction.North)] == '#' &&
                                grid[thisPoint.PointInDirection(Direction.South)] == '#' &&
                                grid[thisPoint.PointInDirection(Direction.East)] == '#' &&
                                grid[thisPoint.PointInDirection(Direction.West)] == '#')
                            {
                                score += thisPoint.X * thisPoint.Y;
                            }
                        }
                    }
                }
            }

            return(score);
        }
Beispiel #8
0
        public int Part1()
        {
            IntcodeComputer computer = new IntcodeComputer("/Users/jjacoby/testing/advent2019/day25.txt");

            computer.Run();
            var initialOutput = computer.GetAllOutput();

            if (initialOutput.Any())
            {
                StringBuilder outputBuilder = new StringBuilder();
                foreach (var o in initialOutput)
                {
                    outputBuilder.Append(((char)o).ToString());
                }

                Console.WriteLine(outputBuilder.ToString());
            }

            List <int> input = new List <int>();

            List <string> program = new List <string>();

            program.Add("north");
            program.Add("east");
            program.Add("take coin");
            program.Add("west");
            program.Add("south");
            program.Add("south");
            program.Add("take food ration");
            program.Add("west");
            program.Add("take sand");
            program.Add("north");
            program.Add("north");
            program.Add("east");
            program.Add("take astrolabe");
            program.Add("west");
            program.Add("south");
            program.Add("south");
            program.Add("east");
            program.Add("north");
            program.Add("east");
            program.Add("take cake");
            program.Add("east");
            program.Add("east");
            program.Add("east");

            foreach (string s in program)
            {
                foreach (char c in s)
                {
                    computer.EnqueueInput((long)c);
                }
                computer.EnqueueInput(10);

                //computer.NextInput = i;
                computer.Run();
                var output = computer.GetAllOutput();
                if (output.Any())
                {
                    StringBuilder outputBuilder = new StringBuilder();
                    foreach (var o in output)
                    {
                        outputBuilder.Append(((char)o).ToString());
                    }

                    Console.WriteLine(outputBuilder.ToString());
                }
            }

            while (true)
            {
                string textInput = Console.ReadLine();

                foreach (char c in textInput)
                {
                    computer.EnqueueInput((long)c);
                }
                computer.EnqueueInput(10);

                //computer.NextInput = i;
                computer.Run();
                var output = computer.GetAllOutput();
                if (output.Any())
                {
                    StringBuilder outputBuilder = new StringBuilder();
                    foreach (var o in output)
                    {
                        outputBuilder.Append(((char)o).ToString());
                    }

                    Console.WriteLine(outputBuilder.ToString());
                }

                if (computer.IsHalted)
                {
                    return(0);
                }

                input.Clear();
            }
        }
Beispiel #9
0
        public Day15()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(15));

            Point robot = new Point(0, 0);

            grid[new Point(0, 0)] = GridLocation.Robot;

            int currentX = 0;
            int currentY = 0;

            for (int rep = 0; rep < 2000000; rep++)
            {
                // try each movement

                int       nextValue     = new Random().Next(1, 5);
                Direction nextDirection = (Direction)nextValue;
                computer.EnqueueInput(nextValue);
                computer.Run();
                long outputCode = computer.GetAllOutput().First();

                int xDelta = 0;
                int yDelta = 0;
                switch (nextDirection)
                {
                case Direction.North:
                    yDelta = 1;
                    break;

                case Direction.South:
                    yDelta = -1;
                    break;

                case Direction.West:
                    xDelta = -1;
                    break;

                case Direction.East:
                    xDelta = 1;
                    break;
                }

                if (outputCode == 0)
                {
                    // wall
                    Point p = new Point(currentX + xDelta, currentY + yDelta);
                    grid[p] = GridLocation.Wall;
                }
                else if (outputCode == 1)
                {
                    // robot moved
                    grid[robot] = GridLocation.Empty;
                    currentY   += yDelta;
                    currentX   += xDelta;

                    robot.Y += yDelta;
                    robot.X += xDelta;
                    Point rp = new Point(robot.X, robot.Y);
                    if (!grid.ContainsKey(rp) || grid[rp] != GridLocation.Oxygen)
                    {
                        grid[rp] = GridLocation.Robot;
                    }
                }
                else if (outputCode == 2)
                {
                    Point p = new Point(currentX + xDelta, currentY + yDelta);
                    grid[p] = GridLocation.Oxygen;

                    currentY += yDelta;
                    currentX += xDelta;

                    robot.Y += yDelta;
                    robot.X += xDelta;

                    oxygenX = p.X;
                    oxygenY = p.Y;
                }
            }

            ox       = new Point(oxygenX, oxygenY);
            grid[ox] = GridLocation.Oxygen;
        }
Beispiel #10
0
        public int Part2()
        {
            Dictionary <Point, char> grid = new Dictionary <Point, char>();

            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(19));

            for (int y = 0; y < 2000; y++)
            {
                bool foundHash = false;
                for (int x = Math.Max(0, y - 30); x < 2000; x++)
                {
                    computer.Reboot();

                    // give x, y as input
                    computer.EnqueueInput(x);
                    computer.EnqueueInput(y);

                    computer.Run();
                    var output = computer.GetAllOutput().ToList();

                    Point p = new Point(x, y);
                    if (output.Any() && output.Last() == 1)
                    {
                        grid.Add(p, '#');
                        foundHash = true;
                    }
                    else
                    {
                        grid.Add(p, '.');
                        if (foundHash)
                        {
                            // tractor beam is solid, no more hashes
                            // come if there has already been one and now
                            // comes a dot
                            break;
                        }
                    }
                }
            }

            for (int x = 0; x < 2000; x++)
            {
                for (int y = 0; y < 2000; y++)
                {
                    Point point = new Point(x, y);
                    if (grid.ContainsKey(point) && grid[point] == '#')
                    {
                        bool ok = true;
                        for (int xdelta = 1; xdelta <= 99; xdelta++)
                        {
                            Point p = new Point(x + xdelta, y);
                            if (!grid.ContainsKey(p) || grid[p] == '.')
                            {
                                ok = false;
                                break;
                            }
                        }
                        for (int ydelta = 1; ydelta <= 99; ydelta++)
                        {
                            Point p = new Point(x, y + ydelta);
                            if (!grid.ContainsKey(p) || grid[p] == '.')
                            {
                                ok = false;
                                break;
                            }
                        }
                        if (ok)
                        {
                            return(x * 10000 + y);
                        }
                    }
                }
            }

            return(0);
        }