Example #1
0
        public object Work2()
        {
            long?res         = null;
            int  packetCount = 0;

            (long x, long y)natMemory = (0, 0);
            long packetMonitor = 0;

            var nics = new List <Nic>();

            for (int i = 0; i < 50; i++)
            {
                var comp = new IntCodeComp(input);
                var nic  = new Nic(comp, i);
                nic.SendPacket = (packet) =>
                {
                    packetCount++;

                    if (packet.address == 255)
                    {
                        natMemory = (packet.x, packet.y);
                    }
                    else if (packet.address > 49 || packet.address < 0)
                    {
                        throw new Exception($"Invalid address '{packet.address}'.");
                    }
                    else
                    {
                        nics[(int)packet.address].Queue.Enqueue((packet.x, packet.y));
                    }
                };

                nics.Add(nic);
            }

            while (res == null)
            {
                foreach (var nic in nics)
                {
                    nic.Run();
                }

                if (nics.All(nic => nic.IsIdle) && natMemory != (0, 0))
                {
                    if (packetMonitor == natMemory.y)
                    {
                        res = natMemory.y;
                    }
                    else
                    {
                        packetMonitor = natMemory.y;
                    }

                    nics[0].Queue.Enqueue(natMemory);
                    packetCount++;
                }
            }

            return(res.Value);
        }
Example #2
0
        public object Work2()
        {
            string A = "L,12,L,10,R,8,L,12";
            string B = "R,8,R,10,R,12";
            string C = "L,10,R,12,R,8";

            string main = path.Replace(A, "A");

            main = main.Replace(B, "B");
            main = main.Replace(C, "C");

            mainAscii = Encoding.ASCII.GetBytes(main);
            AAscii    = Encoding.ASCII.GetBytes(A);
            BAscii    = Encoding.ASCII.GetBytes(B);
            CAscii    = Encoding.ASCII.GetBytes(C);
            source    = mainAscii;

            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            array[0] = 2;
            long dust = 0;
            var  comp = new IntCodeComp(array);

            comp.GetInput    = CompInput;
            comp.WriteOutput = (output) =>
            {
                dust = output;
                //Console.Write(Encoding.ASCII.GetString(new byte[] { (byte)output }));
            };

            comp.RunProgram();

            return(dust);
        }
Example #3
0
        public Game(IntCodeComp comp)
        {
            Comp = comp;
            Score = 0;
            IsX = true;

            Tiles = new Dictionary<Coordinate, int>();

            Comp.WriteOutput = (output) =>
            {
                if (IsX)
                {
                    X = (int)output;

                    IsX = false;
                    IsY = true;
                }
                else if (IsY)
                {
                    Y = (int)output;

                    IsY = false;
                    IsTile = true;
                }
                else if (IsTile)
                {
                    if (X == -1 && Y == 0)
                        Score = output;
                    else
                    {
                        var position = new Coordinate(X, Y);
                        if (Tiles.ContainsKey(position))
                            Tiles[position] = (int)output;
                        else
                            Tiles.Add(position, (int)output);
                    }

                    IsTile = false;
                    IsX = true;
                }
                else
                    throw new Exception("This should never happen.");
            };

            Comp.GetInput = () =>
            {
                //Display();

                int paddlePos = Tiles.First(t => t.Value == 3).Key.X;
                int ballPos = Tiles.First(t => t.Value == 4).Key.X;

                if (paddlePos < ballPos)
                    return 1;
                else if (paddlePos > ballPos)
                    return -1;
                else
                    return 0;
            };
        }
Example #4
0
        public object Work2()
        {
            var comp  = new IntCodeComp(input);
            var droid = new Droid(comp);

            droid.AddCommands(program2);

            return(droid.SuccessOutput);
        }
Example #5
0
        public object Work2()
        {
            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            var computer = new IntCodeComp(array)
            {
                GetInput    = () => 2,
                WriteOutput = (output) => Console.WriteLine($"Output: {output}")
            };

            computer.RunProgram();
            return(null);
        }
Example #6
0
        public Droid(IntCodeComp comp)
        {
            Comp            = comp;
            CurrentLocation = Coordinate.Create(0, 0);
            VisitedCoords   = new Dictionary <Coordinate, int>()
            {
                { CurrentLocation, 1 }
            };

            Comp.GetInput = () =>
            {
                while (Input == Direction.None)
                {
                    Thread.Sleep(50);
                }

                int direction = (int)Input;
                Input = Direction.None;

                return(direction);
            };

            Comp.WriteOutput = (output) =>
            {
                switch (output)
                {
                case 0:
                    Output = '#';
                    break;

                case 1:
                    Output = '.';
                    break;

                case 2:
                    Output = '@';
                    break;

                default:
                    throw new Exception($"Invalid return code '{output}'.");
                }
            };
        }
Example #7
0
        public object Work1()
        {
            long?res         = null;
            int  packetCount = 0;

            var nics = new List <Nic>();

            for (int i = 0; i < 50; i++)
            {
                var comp = new IntCodeComp(input);
                var nic  = new Nic(comp, i);
                nic.SendPacket = (packet) =>
                {
                    packetCount++;

                    if (packet.address == 255)
                    {
                        res = packet.y;
                    }
                    else if (packet.address > 49 || packet.address < 0)
                    {
                        throw new Exception($"Invalid address '{packet.address}'.");
                    }
                    else
                    {
                        nics[(int)packet.address].Queue.Enqueue((packet.x, packet.y));
                    }
                };

                nics.Add(nic);
            }

            while (res == null)
            {
                foreach (var nic in nics)
                {
                    nic.Run();
                }
            }

            return(res.Value);
        }
Example #8
0
        public object Work1()
        {
            var comp  = new IntCodeComp(input);
            var droid = new Droid(comp);

            var map = new Dictionary <Coordinate, char>
            {
                { droid.CurrentLocation, 'S' }
            };

            droid.Start();

            int distance = FindOxygenLocation(droid, map);

            var oxygenCoord = map.Single(m => m.Value == '@').Key;

            DrawMap(map, droid);

            return(distance);
        }
        public Amplifier(IntCodeComp comp, int phase)
        {
            Comp = comp;

            Comp.GetInput = () =>
            {
                Comp.GetInput = () =>
                {
                    while (Input == null)
                    {
                        Thread.Sleep(100);
                    }

                    long value = Input.Value;
                    Input = null;
                    return(value);
                };

                return(phase);
            };
        }
Example #10
0
        public Droid(IntCodeComp comp)
        {
            Commands = new Queue <byte[]>();
            Comp     = comp;

            Comp.GetInput = () =>
            {
                if (commandIdx >= Commands.Peek().Length)
                {
                    commandIdx = 0;
                    Commands.Dequeue();
                    return(10);
                }
                else
                {
                    return(Commands.Peek()[commandIdx++]);
                }
            };

            Comp.WriteOutput = (output) =>
            {
                char asciiChar = Encoding.ASCII.GetChars(new byte[] { (byte)output })[0];

                if (asciiChar != '.' && asciiChar != '#' && asciiChar != '@' && output != 10)
                {
                    SuccessOutput = output;
                }
                else
                {
                    if (output == 10)
                    {
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.Write(asciiChar);
                    }
                }
            };
        }
Example #11
0
        public object Work1()
        {
            var  comp = new IntCodeComp(input);
            long x = 0, y = 0;
            bool isY    = false;
            int  pulled = 0;

            comp.GetInput = () =>
            {
                if (isY)
                {
                    isY = false;
                    return(y);
                }
                else
                {
                    isY = true;
                    return(x);
                }
            };

            comp.WriteOutput = (output) =>
            {
                if (output > 0)
                {
                    pulled++;
                }
            };

            for (x = 0; x < 50; x++)
            {
                for (y = 0; y < 50; y++)
                {
                    comp.RunProgram();
                }
            }

            return(pulled);
        }
Example #12
0
        public object Work1()
        {
            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            var computer = new IntCodeComp(array);

            computer.GetInput = () =>
            {
                int value1;
                Console.Write("Insert number: ");
                if (!int.TryParse(Console.ReadLine(), out value1))
                {
                    throw new ArgumentException("Incorrect input supplied! Only numeric inputs are compatible.");
                }

                return(value1);
            };

            computer.WriteOutput = (output) => Console.WriteLine($"Output: {output}");

            computer.RunProgram();

            return(null);
        }
Example #13
0
        public object Work1()
        {
            var comp = new IntCodeComp(input);

            var scaffoldBuilder = new StringBuilder();

            comp.WriteOutput = (output) =>
            {
                switch (output)
                {
                case 35:
                    scaffoldBuilder.Append('#');
                    break;

                case 46:
                    scaffoldBuilder.Append('.');
                    break;

                case 10:
                    scaffoldBuilder.AppendLine();
                    break;

                case 94:
                    scaffoldBuilder.Append('^');
                    break;

                default:
                    Console.WriteLine($"Invalid output from ASCII computer: '{output}'.");
                    break;
                }
            };

            comp.RunProgram();

            // transform to array
            var lines    = scaffoldBuilder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var scaffold = new char[lines[0].Length, lines.Length];

            for (int x = 0; x < lines[0].Length; x++)
            {
                for (int y = 0; y < lines.Length; y++)
                {
                    scaffold[x, y] = lines[y][x];
                }
            }

            Display(scaffold, lines[0].Length, lines.Length);

            // calculate intersections
            var intersections = new List <int>();

            for (int x = 0; x < lines[0].Length; x++)
            {
                for (int y = 0; y < lines.Length; y++)
                {
                    if (scaffold[x, y] == '#')
                    {
                        var surroundings = new StringBuilder();
                        if (x + 1 < lines[0].Length)
                        {
                            surroundings.Append(scaffold[x + 1, y]);
                        }
                        if (x - 1 >= 0)
                        {
                            surroundings.Append(scaffold[x - 1, y]);
                        }

                        if (y + 1 < lines.Length)
                        {
                            surroundings.Append(scaffold[x, y + 1]);
                        }
                        if (y - 1 >= 0)
                        {
                            surroundings.Append(scaffold[x, y - 1]);
                        }

                        surroundings = surroundings.Replace(".", "");

                        if (surroundings.Length >= 3)
                        {
                            intersections.Add(y * x);
                        }
                    }
                }
            }

            return(intersections.Sum());
        }
Example #14
0
        public object Work1()
        {
            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            var amp1 = new IntCodeComp(array);
            var amp2 = new IntCodeComp(array);
            var amp3 = new IntCodeComp(array);
            var amp4 = new IntCodeComp(array);
            var amp5 = new IntCodeComp(array);

            var phases = new List <int>()
            {
                0, 1, 2, 3, 4
            }.Permute();
            int res1 = 0, res2 = 0, res3 = 0, res4 = 0, res5 = 0;

            var maxThrusts = new List <int>();

            foreach (var phaseEnum in phases)
            {
                var phase = phaseEnum.ToList();

                amp1.GetInput = () =>
                {
                    amp1.GetInput = () =>
                    {
                        return(0);
                    };

                    return(phase[0]);
                };
                amp1.WriteOutput = (output) => res1 = (int)output;

                amp2.GetInput = () =>
                {
                    amp2.GetInput = () =>
                    {
                        return(res1);
                    };

                    return(phase[1]);
                };
                amp2.WriteOutput = (output) => res2 = (int)output;

                amp3.GetInput = () =>
                {
                    amp3.GetInput = () =>
                    {
                        return(res2);
                    };

                    return(phase[2]);
                };
                amp3.WriteOutput = (output) => res3 = (int)output;

                amp4.GetInput = () =>
                {
                    amp4.GetInput = () =>
                    {
                        return(res3);
                    };

                    return(phase[3]);
                };
                amp4.WriteOutput = (output) => res4 = (int)output;

                amp5.GetInput = () =>
                {
                    amp5.GetInput = () =>
                    {
                        return(res4);
                    };

                    return(phase[4]);
                };
                amp5.WriteOutput = (output) => res5 = (int)output;

                amp1.RunProgram();
                amp2.RunProgram();
                amp3.RunProgram();
                amp4.RunProgram();
                amp5.RunProgram();

                maxThrusts.Add(res5);
            }

            return(maxThrusts.Max());
        }
Example #15
0
        public Robot(IntCodeComp comp, int startColor)
        {
            Comp          = comp;
            Position      = new Coordinate(0, 0);
            Direction     = Direction.Up;
            VisitedFields = new Dictionary <Coordinate, int>
            {
                { Position, startColor }
            };

            comp.GetInput = () =>
            {
                if (VisitedFields.ContainsKey(Position))
                {
                    return(VisitedFields[Position]);
                }
                else
                {
                    throw new IndexOutOfRangeException("This should not happen.");
                }
            };

            comp.WriteOutput = (output) =>
            {
                if (isPainting)
                {
                    VisitedFields[Position] = (int)output;
                    isPainting = false;
                }
                else
                {
                    if (output == 0)
                    {
                        switch (Direction)
                        {
                        case Direction.Up:
                            Position  = new Coordinate(Position.X - 1, Position.Y);
                            Direction = Direction.Left;
                            break;

                        case Direction.Down:
                            Position  = new Coordinate(Position.X + 1, Position.Y);
                            Direction = Direction.Right;
                            break;

                        case Direction.Left:
                            Position  = new Coordinate(Position.X, Position.Y - 1);
                            Direction = Direction.Down;
                            break;

                        case Direction.Right:
                            Position  = new Coordinate(Position.X, Position.Y + 1);
                            Direction = Direction.Up;
                            break;

                        default:
                            break;
                        }
                    }
                    else if (output == 1)
                    {
                        switch (Direction)
                        {
                        case Direction.Up:
                            Position  = new Coordinate(Position.X + 1, Position.Y);
                            Direction = Direction.Right;
                            break;

                        case Direction.Down:
                            Position  = new Coordinate(Position.X - 1, Position.Y);
                            Direction = Direction.Left;
                            break;

                        case Direction.Left:
                            Position  = new Coordinate(Position.X, Position.Y + 1);
                            Direction = Direction.Up;
                            break;

                        case Direction.Right:
                            Position  = new Coordinate(Position.X, Position.Y - 1);
                            Direction = Direction.Down;
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        throw new ArgumentException($"Error. Incorrect turn mode value '{output}'.");
                    }

                    if (!VisitedFields.ContainsKey(Position))
                    {
                        VisitedFields.Add(Position, 0);
                    }

                    isPainting = true;
                }
            };
        }
Example #16
0
        public object Work2()
        {
            var  comp = new IntCodeComp(input);
            long x = 0, y = 0;
            bool isY  = false;
            int  size = 200;

            // offset identified by calculation and subsequent experimentation
            int offsetY = 1000;
            int offsetX = 1300;

            int[,] field = new int[size, size];

            comp.GetInput = () =>
            {
                if (isY)
                {
                    isY = false;
                    return(y);
                }
                else
                {
                    isY = true;
                    return(x);
                }
            };

            comp.WriteOutput = (output) =>
            {
                if (output > 0)
                {
                    field[x - offsetX, y - offsetY] = 1;
                }
                else
                {
                    field[x - offsetX, y - offsetY] = 0;
                }
            };

            // generate field
            for (x = offsetX; x < size + offsetX; x++)
            {
                for (y = offsetY; y < size + offsetY; y++)
                {
                    comp.RunProgram();
                }
            }

            for (x = 0; x < size - 99; x++)
            {
                for (y = 0; y < size - 99; y++)
                {
                    if (field[x, y] == 1 && field[x + 99, y] == 1 && field[x, y + 99] == 1)
                    {
                        DisplayFieldWithObject(field, x, y, 100);
                        Console.WriteLine($"100x100 field found at: X: {x + offsetX} Y: {y + offsetY}; answer is {(x + offsetX) * 10000 + (y + offsetY)}");
                        return(0);
                    }
                }
            }
            return(0);
        }