Example #1
0
        public int Part2()
        {
            long desiredOutput = 19690720;

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

            for (int noun = 0; noun < 100; noun++)
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    computer.Reboot();
                    computer.SetMemory(1, noun);
                    computer.SetMemory(2, verb);

                    computer.Run();

                    if (computer.GetValueAtAddress(0) == desiredOutput)
                    {
                        return(100 * noun + verb);
                    }
                }
            }

            return(0);
        }
Example #2
0
        public int Part1()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(2));

            computer.SetMemory(1, 12);
            computer.Run();
            return((int)computer.GetValueAtAddress(0));
        }
Example #3
0
File: Day5.cs Project: stames/aoc
        public int Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(5));

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

            return((int)computer.GetAllOutput().Last());
        }
Example #4
0
File: Day9.cs Project: stames/aoc
        public long Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(9));

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

            return(computer.GetAllOutput().Last());
        }
Example #5
0
File: Day23.cs Project: stames/aoc
        public long Part2()
        {
            Dictionary <int, IntcodeComputer> computers = new Dictionary <int, IntcodeComputer>();

            for (int i = 0; i < 50; i++)
            {
                IntcodeComputer c = new IntcodeComputer(InputUtils.GetFileName(23));
                c.EnqueueInput(i);
                c.EnqueueInput(-1);
                computers.Add(i, c);
            }

            long natX    = 0;
            long natY    = 0;
            long oldNatY = 0;

            while (true)
            {
                bool networkIdle = true;
                for (int i = 0; i < 50; i++)
                {
                    computers[i].Run();

                    var output = computers[i].GetAllOutput().ToList();
                    if (output.Count() % 3 != 0 || output.Count() == 0)
                    {
                        continue;
                    }

                    networkIdle = false;
                    for (int j = 0; j < output.Count; j += 3)
                    {
                        if (output[j] == 255)
                        {
                            natX = output[j + 1];
                            natY = output[j + 2];
                            continue;
                        }

                        computers[(int)output[j]].EnqueueInput(output[j + 1]);
                        computers[(int)output[j]].EnqueueInput(output[j + 2]);
                    }
                }

                if (networkIdle)
                {
                    if (natY == oldNatY)
                    {
                        return(natY);
                    }

                    computers[0].EnqueueInput(natX);
                    computers[0].EnqueueInput(natY);
                    oldNatY = natY;
                }
            }
        }
Example #6
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);
        }
Example #7
0
File: Day7.cs Project: stames/aoc
        public int Part2()
        {
            List <int> results = new List <int>();

            List <int> values = new List <int> {
                5, 6, 7, 8, 9
            };
            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]);

                long eOutput = 0;

                while (!a.IsHalted && !b.IsHalted && !c.IsHalted && !d.IsHalted && !e.IsHalted)
                {
                    a.EnqueueInput(eOutput);
                    long aOutput = a.RunUntilNextOutput();

                    b.EnqueueInput(aOutput);
                    long bOutput = b.RunUntilNextOutput();

                    c.EnqueueInput(bOutput);
                    long cOutput = c.RunUntilNextOutput();

                    d.EnqueueInput(cOutput);
                    long dOutput = d.RunUntilNextOutput();

                    e.EnqueueInput(dOutput);
                    eOutput = e.RunUntilNextOutput();
                }

                results.Add((int)eOutput);
            }

            return(results.Max());
        }
Example #8
0
File: Day7.cs Project: stames/aoc
        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());
        }
Example #9
0
File: Day23.cs Project: stames/aoc
        public long Part1()
        {
            Dictionary <int, IntcodeComputer> computers = new Dictionary <int, IntcodeComputer>();

            for (int i = 0; i < 50; i++)
            {
                IntcodeComputer c = new IntcodeComputer(InputUtils.GetFileName(23));
                c.EnqueueInput(i);
                c.EnqueueInput(-1);
                computers.Add(i, c);
            }

            while (true)
            {
                for (int i = 0; i < 50; i++)
                {
                    computers[i].Run();

                    var output = computers[i].GetAllOutput().ToList();
                    if (output.Count() % 3 != 0 || output.Count() == 0)
                    {
                        continue;
                    }

                    for (int j = 0; j < output.Count; j += 3)
                    {
                        if (output[j] == 255)
                        {
                            return(output[j + 2]);
                        }

                        computers[(int)output[j]].EnqueueInput(output[j + 1]);
                        computers[(int)output[j]].EnqueueInput(output[j + 2]);
                    }
                }
            }
        }
Example #10
0
File: Day19.cs Project: stames/aoc
        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);
        }
Example #11
0
File: Day17.cs Project: stames/aoc
        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);
        }
Example #12
0
File: Day17.cs Project: stames/aoc
        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);
        }
Example #13
0
File: Day11.cs Project: stames/aoc
        public void RunComputer(int startingValue)
        {
            grid = new Dictionary <Point, bool>();

            int startingX = 0;
            int startingY = 0;

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

            Point     currentPoint = new Point(startingX, startingY);
            int       currentX     = 0;
            int       currentY     = 0;
            Direction direction    = Direction.Up;

            while (true)
            {
                // get current color
                int inputColor = startingValue;
                if (grid.ContainsKey(currentPoint))
                {
                    inputColor = grid[currentPoint] ? 1 : 0;
                }

                computer.EnqueueInput(inputColor);
                long outputColor = computer.RunUntilNextOutput();

                grid[currentPoint] = outputColor == 0 ? false : true;

                // 0 == turn left, 1 == turn right
                long outputDirection = computer.RunUntilNextOutput();

                seenPoints.Add(currentPoint);

                // set the next point
                if (direction == Direction.Up)
                {
                    if (outputDirection == 0)
                    {
                        direction = Direction.Left;
                    }
                    else
                    {
                        direction = Direction.Right;
                    }
                }
                else if (direction == Direction.Right)
                {
                    if (outputDirection == 0)
                    {
                        direction = Direction.Up;
                    }
                    else
                    {
                        direction = Direction.Down;
                    }
                }
                else if (direction == Direction.Left)
                {
                    if (outputDirection == 0)
                    {
                        direction = Direction.Down;
                    }
                    else
                    {
                        direction = Direction.Up;
                    }
                }
                else
                {
                    if (outputDirection == 0)
                    {
                        direction = Direction.Right;
                    }
                    else
                    {
                        direction = Direction.Left;
                    }
                }

                switch (direction)
                {
                case Direction.Up:
                    currentY -= 1;
                    break;

                case Direction.Down:
                    currentY += 1;
                    break;

                case Direction.Left:
                    currentX -= 1;
                    break;

                case Direction.Right:
                    currentX += 1;
                    break;

                default:
                    throw new InvalidOperationException();
                }

                currentPoint = new Point(currentX, currentY);

                if (computer.IsHalted)
                {
                    break;
                }
            }
        }
Example #14
0
File: Day15.cs Project: stames/aoc
        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;
        }
Example #15
0
File: Day19.cs Project: stames/aoc
        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);
        }