Beispiel #1
0
        public static void solve()
        {
            var input  = InputLoader.loadAsString("08");
            var width  = 25;
            var height = 6;
            var depth  = input.Length / (width * height);

            var smallest = new int[] { int.MaxValue, 0, 0 };

            for (var z = 0; z < depth; z++)
            {
                var counts = new int[] { 0, 0, 0 };

                for (var y = 0; y < height; y++)
                {
                    for (var x = 0; x < width; x++)
                    {
                        var value = int.Parse(input[z * width * height + y * width + x] + "");

                        if (value < counts.Length)
                        {
                            counts[value]++;
                        }
                    }
                }

                if (counts[0] < smallest[0])
                {
                    smallest = counts;
                }
            }

            Console.WriteLine(smallest[1] * smallest[2]);
        }
        public static void solve()
        {
            var input  = InputLoader.loadAsString("16");
            var signal = Array.ConvertAll(input.ToCharArray(), strDigit => int.Parse(strDigit + ""));

            var pattern = new int[] { 0, 1, 0, -1 };

            var nextSignal = new int[signal.Length];

            for (var phase = 0; phase < 100; phase++)
            {
                for (var i = 0; i < nextSignal.Length; i++)
                {
                    for (var j = 0; j < signal.Length; j++)
                    {
                        var patternIndex = ((((j + 1) / (i + 1)) % pattern.Length));
                        var patternDigit = pattern[patternIndex];

                        nextSignal[i] += signal[j] * patternDigit;
                    }

                    nextSignal[i] = Math.Abs(nextSignal[i]) % 10;
                }

                signal     = nextSignal;
                nextSignal = new int[signal.Length];
            }

            for (var i = 0; i < 8; i++)
            {
                Console.Write(signal[i]);
            }
            Console.WriteLine();
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("02").Split(",");

            var codes = new int[input.Length];

            for (int i = 0; i < input.Length; i++)
            {
                codes[i] = Int32.Parse(input[i]);
            }

            codes[1] = 12;
            codes[2] = 2;

            for (int i = 0; codes[i] != 99; i += 4)
            {
                if (codes[i] == 1)
                {
                    codes[codes[i + 3]] = codes[codes[i + 1]] + codes[codes[i + 2]];
                }
                else if (codes[i] == 2)
                {
                    codes[codes[i + 3]] = codes[codes[i + 1]] * codes[codes[i + 2]];
                }
            }

            Console.WriteLine(codes[0]);
        }
Beispiel #4
0
        public static void solve()
        {
            var input = InputLoader.loadAsString("05").Split(",");

            var values = new int[input.Length];

            for (int i = 0; i < input.Length; i++)
            {
                values[i] = Int32.Parse(input[i]);
            }

            var register = 1;

            var current = 0;

            while (true)
            {
                var opCode = getOpCode(values[current]);

                if (opCode == OpCode.Add)
                {
                    var value1 = getParameterValue(values, current, 1);
                    var value2 = getParameterValue(values, current, 2);
                    var value3 = values[current + 3];

                    values[value3] = value1 + value2;

                    current += 4;
                }
                else if (opCode == OpCode.Mul)
                {
                    var value1 = getParameterValue(values, current, 1);
                    var value2 = getParameterValue(values, current, 2);
                    var value3 = values[current + 3];

                    values[value3] = value1 * value2;

                    current += 4;
                }
                else if (opCode == OpCode.Input)
                {
                    values[values[current + 1]] = register;

                    current += 2;
                }
                else if (opCode == OpCode.Output)
                {
                    register = values[values[current + 1]];

                    current += 2;
                }
                else if (opCode == OpCode.Terminate)
                {
                    break;
                }
            }

            Console.WriteLine(register);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("16");

            // Tried to calculate by stepping backwards from the message index but it didn't work.  I ended up looking for hints on
            // reddit for this one and didn't really like what I saw so I skipped this one for now.  Something to do with studying
            // the pattern and optimizing... blah, blah.  Hate those...
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("15").Split(",");

            var computer = new IntcodeComputer(input);

            var map = new Map();

            map.set(Tuple.Create(0, 0), 'X');

            var pathes = new LinkedList <Path>();

            pathes.AddFirst(new Path(Direction.North, null));
            pathes.AddFirst(new Path(Direction.East, null));
            pathes.AddFirst(new Path(Direction.South, null));
            pathes.AddFirst(new Path(Direction.West, null));

            while (true)
            {
                var currentPath = pathes.Last.Value;
                pathes.RemoveLast();

                var state = currentPath.apply(computer, map);

                if (state.Item2 == 1)
                {
                    if (!map.isVisited(state.Item1, Direction.North))
                    {
                        pathes.AddFirst(new Path(Direction.North, currentPath));
                    }

                    if (!map.isVisited(state.Item1, Direction.East))
                    {
                        pathes.AddFirst(new Path(Direction.East, currentPath));
                    }

                    if (!map.isVisited(state.Item1, Direction.South))
                    {
                        pathes.AddFirst(new Path(Direction.South, currentPath));
                    }

                    if (!map.isVisited(state.Item1, Direction.West))
                    {
                        pathes.AddFirst(new Path(Direction.West, currentPath));
                    }
                }
                else if (state.Item2 == 2)
                {
                    Console.WriteLine(currentPath.length);
                    break;
                }

                currentPath.revert(computer);
            }
        }
Beispiel #7
0
        public static void solve()
        {
            var input = InputLoader.loadAsString("17").Split(",");

            var computer = new IntcodeComputer(input);

            var outputs = computer.execute(new int[] { });

            var width  = 0;
            var height = 0;
            var map    = new Dictionary <Tuple <int, int>, char>();

            {
                var x = 0;
                var y = 0;

                foreach (var output in outputs)
                {
                    map[Tuple.Create(x, y)] = (char)output;

                    if (output == 10)
                    {
                        x = 0;
                        y++;

                        height = y;
                    }
                    else
                    {
                        x++;
                        width = x;
                    }
                }
            }

            var sum = 0;

            for (var y = 1; y < height - 1; y++)
            {
                for (var x = 1; x < width - 1; x++)
                {
                    if (map[Tuple.Create(x - 1, y)] == '#' && map[Tuple.Create(x + 1, y)] == '#' && map[Tuple.Create(x, y - 1)] == '#' && map[Tuple.Create(x, y + 1)] == '#')
                    {
                        sum += (x * y);
                    }
                }
            }

            Console.WriteLine(sum);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("11").Split(",");

            var program = new Dictionary <Int64, Int64>();

            for (int i = 0; i < input.Length; i++)
            {
                program[i] = Int64.Parse(input[i]);
            }

            var computer = new IntcodeComputer(program);

            var panels = new Dictionary <Tuple <int, int>, int>();

            var robotPos = Tuple.Create(0, 0);
            var robotDir = Direction.Up;

            while (!computer.isHalted())
            {
                var currentColor = 0;
                if (panels.ContainsKey(robotPos))
                {
                    currentColor = panels[robotPos];
                }

                // Compute
                var outputs = computer.execute(new int[] { currentColor });

                // Rotate
                if (outputs[1] == 0)
                {
                    robotDir = rotateLeft(robotDir);
                }
                else
                {
                    robotDir = rotateRight(robotDir);
                }

                // Paint
                panels[robotPos] = (int)outputs[0];

                // Move
                robotPos = move(robotPos, robotDir);
            }

            Console.WriteLine(panels.Count);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("17").Split(",");

            // Wake up the robot.
            input[0] = "2";

            var computer = new IntcodeComputer(input);

            var path = getPath(computer);

            ////////////////////////////////////////////
            // Cheaped out and did the next part by hand.
            ////////////////////////////////////////////

            foreach (var entry in path)
            {
                Console.Write(entry + ",");
            }

            Console.WriteLine();

            // A - R10,L12,R6,
            // A - R10,L12,R6,
            // B - R6,R10,R12,R6,
            // C - R10,L12,L12,
            // B - R6,R10,R12,R6,
            // C - R10,L12,L12,
            // B - R6,R10,R12,R6,
            // C - R10,L12,L12,
            // B - R6,R10,R12,R6,
            // A - R10,L12,R6,

            var instructions = "A,A,B,C,B,C,B,C,B,A\nR,10,L,12,R,6\nR,6,R,10,R,12,R,6\nR,10,L,12,L,12\nn\n";

            var computerInput = new int[instructions.Length];

            for (var i = 0; i < instructions.Length; i++)
            {
                computerInput[i] = instructions[i];
            }

            var outputs = computer.execute(computerInput);

            Console.WriteLine(outputs[outputs.Length - 1]);
        }
Beispiel #10
0
        public static void solve()
        {
            var input = InputLoader.loadAsString("07").Split(",");

            var program = new int[input.Length];

            for (int i = 0; i < input.Length; i++)
            {
                program[i] = Int32.Parse(input[i]);
            }

            var max = 0;

            permutate(new List <int> {
            }, new List <int> {
                5, 6, 7, 8, 9
            }, (List <int> sequence) =>
            {
                var amplifiers = new IntcodeComputer[] {
                    new IntcodeComputer(program),
                    new IntcodeComputer(program),
                    new IntcodeComputer(program),
                    new IntcodeComputer(program),
                    new IntcodeComputer(program)
                };

                var lastSignal = 0;

                for (var i = 0; i < sequence.Count; i++)
                {
                    lastSignal = amplifiers[i].execute(new int[] { sequence[i], lastSignal });
                }

                while (!allHalted(amplifiers))
                {
                    for (var i = 0; i < amplifiers.Length; i++)
                    {
                        lastSignal = amplifiers[i].execute(new int[] { lastSignal });
                    }
                }

                max = Math.Max(max, lastSignal);
            });

            Console.WriteLine(max);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("07").Split(",");

            var program = new int[input.Length];

            for (int i = 0; i < input.Length; i++)
            {
                program[i] = Int32.Parse(input[i]);
            }

            var maxSignal = getMaxSignal(program, new List <int> {
                0, 1, 2, 3, 4
            }, 0);

            Console.WriteLine(maxSignal);
        }
        public static void solve()
        {
            var input  = InputLoader.loadAsString("08");
            var width  = 25;
            var height = 6;

            var image = new Image(input, width, height);

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    Console.Write(image.getPixel(x, y) == 1 ? '#' : ' ');
                }

                Console.WriteLine();
            }
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("04").Split("-");

            var min = Int32.Parse(input[0]);
            var max = Int32.Parse(input[1]);

            var validCount = 0;

            for (var value = min; value <= max; value++)
            {
                if (isValid(String.Format("{0}", value)))
                {
                    validCount++;
                }
            }

            Console.WriteLine(validCount);
        }
Beispiel #14
0
        public static void solve()
        {
            var input = InputLoader.loadAsString("09").Split(",");

            var program = new Dictionary <Int64, Int64>();

            for (int i = 0; i < input.Length; i++)
            {
                program[i] = Int64.Parse(input[i]);
            }

            var computer = new IntcodeComputer(program);

            var outputs = computer.execute(new int[] { 2 });

            foreach (var output in outputs)
            {
                Console.WriteLine(output);
            }
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("13").Split(",");

            var computer = new IntcodeComputer(input);

            var output = computer.execute(new int[] { });

            var blockCount = 0;

            for (int i = 0; i < output.Length / 3; i++)
            {
                var offset = i * 3;

                if (output[offset + 2] == 2)
                {
                    blockCount++;
                }
            }

            Console.WriteLine(blockCount);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("02").Split(",");

            var codes = new int[input.Length];

            for (int i = 0; i < input.Length; i++)
            {
                codes[i] = Int32.Parse(input[i]);
            }

            for (int noun = 0; noun <= 99; noun++)
            {
                for (int verb = 0; verb <= 99; verb++)
                {
                    var result = executeProgram(codes, noun, verb);
                    if (result == 19690720)
                    {
                        Console.WriteLine(100 * noun + verb);
                        return;
                    }
                }
            }
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("15").Split(",");

            var computer = new IntcodeComputer(input);

            var map = new Map();

            map.set(Tuple.Create(0, 0), 'X');

            var pathes = new LinkedList <Path>();

            pathes.AddFirst(new Path(Direction.North, null));
            pathes.AddFirst(new Path(Direction.East, null));
            pathes.AddFirst(new Path(Direction.South, null));
            pathes.AddFirst(new Path(Direction.West, null));

            var oxygenStation = Tuple.Create(0, 0);

            while (pathes.Count > 0)
            {
                var currentPath = pathes.Last.Value;
                pathes.RemoveLast();

                var state = currentPath.apply(computer, map);

                if (state.Item2 != 0)
                {
                    if (!map.isVisited(state.Item1, Direction.North))
                    {
                        pathes.AddFirst(new Path(Direction.North, currentPath));
                    }

                    if (!map.isVisited(state.Item1, Direction.East))
                    {
                        pathes.AddFirst(new Path(Direction.East, currentPath));
                    }

                    if (!map.isVisited(state.Item1, Direction.South))
                    {
                        pathes.AddFirst(new Path(Direction.South, currentPath));
                    }

                    if (!map.isVisited(state.Item1, Direction.West))
                    {
                        pathes.AddFirst(new Path(Direction.West, currentPath));
                    }
                }

                if (state.Item2 == 2)
                {
                    oxygenStation = state.Item1;
                }

                currentPath.revert(computer);
            }


            var oxygens = new LinkedList <Tuple <int, int> >();

            oxygens.AddFirst(oxygenStation);

            var minutes = 0;

            while (oxygens.Count > 0)
            {
                var newList = new LinkedList <Tuple <int, int> >();
                for (var current = oxygens.First; current != null; current = current.Next)
                {
                    map.set(current.Value, 'O');

                    if (map.get(Tuple.Create(current.Value.Item1 + 1, current.Value.Item2)) == '.')
                    {
                        newList.AddFirst(Tuple.Create(current.Value.Item1 + 1, current.Value.Item2));
                    }

                    if (map.get(Tuple.Create(current.Value.Item1 - 1, current.Value.Item2)) == '.')
                    {
                        newList.AddFirst(Tuple.Create(current.Value.Item1 - 1, current.Value.Item2));
                    }

                    if (map.get(Tuple.Create(current.Value.Item1, current.Value.Item2 + 1)) == '.')
                    {
                        newList.AddFirst(Tuple.Create(current.Value.Item1, current.Value.Item2 + 1));
                    }

                    if (map.get(Tuple.Create(current.Value.Item1, current.Value.Item2 - 1)) == '.')
                    {
                        newList.AddFirst(Tuple.Create(current.Value.Item1, current.Value.Item2 - 1));
                    }
                }

                oxygens = newList;

                minutes++;
            }

            Console.WriteLine(minutes);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("11").Split(",");

            var program = new Dictionary <Int64, Int64>();

            for (int i = 0; i < input.Length; i++)
            {
                program[i] = Int64.Parse(input[i]);
            }

            var computer = new IntcodeComputer(program);

            var panels = new Dictionary <Tuple <int, int>, int>();

            var robotPos = Tuple.Create(0, 0);
            var robotDir = Direction.Up;

            panels[robotPos] = 1;

            var minX = 0;
            var minY = 0;
            var maxX = 0;
            var maxY = 0;

            while (!computer.isHalted())
            {
                var currentColor = 0;
                if (panels.ContainsKey(robotPos))
                {
                    currentColor = panels[robotPos];
                }

                // Compute
                var outputs = computer.execute(new int[] { currentColor });

                // Rotate
                if (outputs[1] == 0)
                {
                    robotDir = rotateLeft(robotDir);
                }
                else
                {
                    robotDir = rotateRight(robotDir);
                }

                // Paint
                panels[robotPos] = (int)outputs[0];

                // Move
                robotPos = move(robotPos, robotDir);

                // Update min/max
                minX = Math.Min(minX, robotPos.Item1);
                minY = Math.Min(minY, robotPos.Item2);
                maxX = Math.Max(maxX, robotPos.Item1);
                maxY = Math.Max(maxY, robotPos.Item2);
            }

            for (int y = minY; y <= maxY; y++)
            {
                for (int x = minX; x <= maxX; x++)
                {
                    var pos = Tuple.Create(x, y);

                    if (panels.ContainsKey(pos) && panels[pos] == 1)
                    {
                        Console.Write('#');
                    }
                    else
                    {
                        Console.Write(' ');
                    }
                }

                Console.WriteLine();
            }
        }
Beispiel #19
0
        public static void solve()
        {
            var input = InputLoader.loadAsString("13").Split(",");

            input[0] = "2";

            var screen   = new Dictionary <Tuple <Int64, Int64>, Int64>();
            var computer = new IntcodeComputer(input);

            var   joystick = 0;
            Int64 score    = 0;

            do
            {
                var output = computer.execute(new int[] { joystick });

                Int64 ballX   = 0;
                Int64 paddleX = 0;
                for (int i = 0; i < output.Length / 3; i++)
                {
                    var offset = i * 3;

                    var x        = output[offset];
                    var y        = output[offset + 1];
                    var position = Tuple.Create(x, y);
                    var value    = output[offset + 2];

                    if (x == -1 && y == 0)
                    {
                        score = value;
                    }
                    else
                    {
                        screen[position] = value;
                    }

                    if (value == 3)
                    {
                        paddleX = x;
                    }

                    if (value == 4)
                    {
                        ballX = x;
                    }
                }

                if (ballX > paddleX)
                {
                    joystick = 1;
                }
                else if (ballX < paddleX)
                {
                    joystick = -1;
                }
                else
                {
                    joystick = 0;
                }
            }while (hasBlocksLeft(screen));

            Console.WriteLine(score);
        }