예제 #1
0
        public void Task2()
        {
            long[] inputInt = ReadAndParse();

            IntCode intCode = new IntCode(inputInt, new long[0]);
            int     result = 0; bool done = false;

            for (int opCode1 = 0; opCode1 <= 99; opCode1++)
            {
                for (int opCode2 = 0; opCode2 <= 99; opCode2++)
                {
                    intCode.Reset(); intCode.SetValueAtPosition(1, opCode1); intCode.SetValueAtPosition(2, opCode2);

                    while (true)
                    {
                        long output = intCode.RunProgram();
                        if (output == 99)
                        {
                            if (intCode.GetValueAtPosition(0) == 19690720)
                            {
                                result = 100 * opCode1 + opCode2; done = true;
                            }
                            break;
                        }
                    }
                    if (done)
                    {
                        break;
                    }
                }
                if (done)
                {
                    break;
                }
            }

            //9074
            Console.WriteLine("Day 2 task 2 : " + result);
        }
예제 #2
0
        public void Task1()
        {
            long[] inputInt = ReadAndParse();

            IntCode intCode = new IntCode(inputInt, new long[0]);

            intCode.SetValueAtPosition(1, 12); intCode.SetValueAtPosition(2, 2);

            long result = 0;

            while (true)
            {
                long output = intCode.RunProgram();
                if (output == 99)
                {
                    result = intCode.GetValueAtPosition(0); break;
                }
            }

            //2842648
            Console.WriteLine("Day 2 task 1 : " + result);
        }
예제 #3
0
        private void WalkRobot(long[] inputInt, IDictionary <int, IDictionary <int, int> > map)
        {
            int     x = 0; int y = 0; char orientation = 'N'; long[] output = new long[2];
            IntCode intCode = new IntCode(inputInt, new long[0]);

            bool done = false;

            while (true)
            {
                int inputColor = map.ContainsKey(x) && map[x].ContainsKey(y) ? map[x][y] : 0;
                intCode.SetNewReadBuffer(new long[1] {
                    inputColor
                });
                for (int index = 0; index < 2; index++)
                {
                    output[index] = intCode.RunProgram();

                    if (output[index] == 99)
                    {
                        done = true;
                        break;
                    }
                }

                if (done)
                {
                    break;
                }

                if (!map.ContainsKey(x))
                {
                    map[x] = new Dictionary <int, int>();
                }
                map[x][y] = (int)output[0];

                orientation = GetNewOrientation(orientation, output);
                GetNewPosition(orientation, ref x, ref y);
            }
        }
예제 #4
0
        public void Task1()
        {
            long[] inputInt = ReadAndParse();

            IntCode intCode = new IntCode(inputInt, new long[1] {
                1
            });
            IList <long> outputs = new List <long>();

            while (true)
            {
                long output = intCode.RunProgram();
                if (output == 99)
                {
                    break;
                }
                outputs.Add(output);
            }
            long result = outputs[outputs.Count - 1];

            //16209841
            Console.WriteLine("Day 5 task 1 : " + result);
        }
예제 #5
0
        public void Part1()
        {
            var vm = IntCode.CreateVM("Data/Day21.txt");

            vm.WriteLines(
                "NOT A J",
                "NOT B T",
                "OR T J",
                "NOT C T",
                "OR T J",
                "AND D J",
                "WALK"
                );

            vm.Execute();

            foreach (var line in vm.ReadLines())
            {
                output.WriteLine(line);
            }

            vm.Read().Should().Be(19357335);
        }
예제 #6
0
        public void Part1(string input, int answer)
        {
            long max  = 0;
            var  prog = FileIterator.LoadCSV <int>(input);

            foreach (var perm in Permutations(0, 1, 2, 3, 4))
            {
                long output = 0;
                foreach (var p in perm)
                {
                    var vm = IntCode.CreateVM(prog);
                    vm.State.Input  = Generators.Reader(p, output);
                    vm.State.Output = o => output = o;
                    vm.Execute();
                }

                if (max < output)
                {
                    max = output;
                }
            }

            max.Should().Be(answer);
        }
예제 #7
0
        public void Part2()
        {
            var vm = IntCode.CreateVM("Data/Day21.txt");

            vm.WriteLines(
                "NOT B J",
                "NOT C T",
                "OR T J",
                "AND D J",
                "AND H J",
                "NOT A T",
                "OR T J",
                "RUN"
                );

            vm.Execute();

            foreach (var line in vm.ReadLines())
            {
                output.WriteLine(line);
            }

            vm.Read().Should().Be(1140147758);
        }
예제 #8
0
        public void Task2()
        {
            long[] inputInt = ReadAndParse();

            int     result  = 0;
            IntCode intCode = new IntCode(inputInt, new long[0]);
            IDictionary <long, IDictionary <long, int> > grid = new Dictionary <long, IDictionary <long, int> >();

            while (true)
            {
                long x = 0; long y = 0; int tile = 0; bool done = false;

                for (int index = 0; index < 3; index++)
                {
                    long output = intCode.RunProgram();
                    if (output == 99)
                    {
                        done = true;
                        break;
                    }

                    if (index == 0)
                    {
                        x = output;
                    }
                    if (index == 1)
                    {
                        y = output;
                    }
                    if (index == 2)
                    {
                        tile = (int)output;
                    }
                }

                if (done)
                {
                    break;
                }

                if (!grid.ContainsKey(x))
                {
                    grid[x] = new Dictionary <long, int>();
                }
                grid[x][y] = tile;
            }

            /*
             * for (int index = 0; index < 20; index++)
             * {
             *  foreach (int key in grid.Keys)
             *  {
             *      if(grid[key][index] != 0)
             *          Console.Write(grid[key][index]);
             *      else
             *          Console.Write(" ");
             *  }
             *  Console.WriteLine();
             * }*/

            foreach (int key in grid.Keys)
            {
                foreach (int key2 in grid[key].Keys)
                {
                    if (grid[key][key2] == 2)
                    {
                        result++;
                    }
                }
            }

            //193
            Console.WriteLine("Day 13 task 2 : " + result);
        }
예제 #9
0
        public void Task1()
        {
            long[] inputInt = ReadAndParse();

            IntCode intCode = new IntCode(inputInt, new long[0]);

            long         result = 0;
            ISet <Point> wallPoints = new HashSet <Point>();
            int          x = 0; int y = 0;

            while (true)
            {
                long output = intCode.RunProgram();
                if (output == 99)
                {
                    break;
                }
                Point wallPoint = new Point()
                {
                    X = x++, Y = y
                };
                if (output != 10 && output != 46)
                {
                    wallPoints.Add(wallPoint);
                }

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

                if (output == 10)
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.Write((char)output);
                }
            }

            foreach (Point wallPoint in wallPoints)
            {
                Point pointUp = new Point()
                {
                    X = wallPoint.X, Y = wallPoint.Y + 1
                };
                Point pointRight = new Point()
                {
                    X = wallPoint.X + 1, Y = wallPoint.Y
                };
                Point pointDown = new Point()
                {
                    X = wallPoint.X, Y = wallPoint.Y - 1
                };
                Point pointLeft = new Point()
                {
                    X = wallPoint.X - 1, Y = wallPoint.Y
                };

                if (wallPoints.Contains(pointUp) && wallPoints.Contains(pointRight) && wallPoints.Contains(pointDown) && wallPoints.Contains(pointLeft))
                {
                    result += wallPoint.X * wallPoint.Y;
                }
            }

            //5056
            Console.WriteLine("Day 17 task 1 : " + result);
        }
예제 #10
0
        public void Task2()
        {
            long[]  inputInt = ReadAndParse();
            long    max      = long.MinValue;
            IntCode intCode1 = new IntCode(inputInt, new long[0]);
            IntCode intCode2 = new IntCode(inputInt, new long[0]);
            IntCode intCode3 = new IntCode(inputInt, new long[0]);
            IntCode intCode4 = new IntCode(inputInt, new long[0]);
            IntCode intCode5 = new IntCode(inputInt, new long[0]);

            for (int firstPhase = 5; firstPhase < 10; firstPhase++)
            {
                for (int secondPhase = 5; secondPhase < 10; secondPhase++)
                {
                    if (secondPhase == firstPhase)
                    {
                        continue;
                    }
                    for (int thirdPhase = 5; thirdPhase < 10; thirdPhase++)
                    {
                        if (secondPhase == thirdPhase || thirdPhase == firstPhase)
                        {
                            continue;
                        }
                        for (int forthPhase = 5; forthPhase < 10; forthPhase++)
                        {
                            if (forthPhase == thirdPhase || forthPhase == secondPhase || forthPhase == firstPhase)
                            {
                                continue;
                            }
                            for (int fifthPhase = 5; fifthPhase < 10; fifthPhase++)
                            {
                                if (forthPhase == fifthPhase || fifthPhase == thirdPhase || fifthPhase == secondPhase || fifthPhase == firstPhase)
                                {
                                    continue;
                                }

                                long result = 0; long finalResult = 0; bool firstRun = true;

                                while (true)
                                {
                                    intCode1.SetNewReadBuffer(firstRun ? new long[2] {
                                        firstPhase, result
                                    } : new long[1] {
                                        result
                                    });
                                    result = intCode1.RunProgram(); if (result == 99)
                                    {
                                        break;
                                    }

                                    intCode2.SetNewReadBuffer(firstRun ? new long[2] {
                                        secondPhase, result
                                    } : new long[1] {
                                        result
                                    });
                                    result = intCode2.RunProgram(); if (result == 99)
                                    {
                                        break;
                                    }

                                    intCode3.SetNewReadBuffer(firstRun ? new long[2] {
                                        thirdPhase, result
                                    } : new long[1] {
                                        result
                                    });
                                    result = intCode3.RunProgram(); if (result == 99)
                                    {
                                        break;
                                    }

                                    intCode4.SetNewReadBuffer(firstRun ? new long[2] {
                                        forthPhase, result
                                    } : new long[1] {
                                        result
                                    });
                                    result = intCode4.RunProgram(); if (result == 99)
                                    {
                                        break;
                                    }

                                    intCode5.SetNewReadBuffer(firstRun ? new long[2] {
                                        fifthPhase, result
                                    } : new long[1] {
                                        result
                                    });
                                    result = intCode5.RunProgram(); if (result == 99)
                                    {
                                        break;
                                    }

                                    finalResult = result;  firstRun = false;
                                }

                                if (max < finalResult)
                                {
                                    max = finalResult;
                                }

                                intCode1.Reset(); intCode2.Reset(); intCode3.Reset(); intCode4.Reset(); intCode5.Reset();
                            }
                        }
                    }
                }
            }

            //1047153
            Console.WriteLine("Day 7 task 2 : " + max);
        }
예제 #11
0
        public void Task1()
        {
            long[] inputInt = ReadAndParse();

            long    max     = int.MinValue;
            IntCode intCode = new IntCode(inputInt, new long[0]);

            for (int firstPhase = 0; firstPhase < 5; firstPhase++)
            {
                for (int secondPhase = 0; secondPhase < 5; secondPhase++)
                {
                    if (secondPhase == firstPhase)
                    {
                        continue;
                    }
                    for (int thirdPhase = 0; thirdPhase < 5; thirdPhase++)
                    {
                        if (secondPhase == thirdPhase || thirdPhase == firstPhase)
                        {
                            continue;
                        }
                        for (int forthPhase = 0; forthPhase < 5; forthPhase++)
                        {
                            if (forthPhase == thirdPhase || forthPhase == secondPhase || forthPhase == firstPhase)
                            {
                                continue;
                            }
                            for (int fifthPhase = 0; fifthPhase < 5; fifthPhase++)
                            {
                                if (forthPhase == fifthPhase || fifthPhase == thirdPhase || fifthPhase == secondPhase || fifthPhase == firstPhase)
                                {
                                    continue;
                                }

                                intCode.Reset(); intCode.SetNewReadBuffer(new long[2] {
                                    firstPhase, 0
                                });
                                long result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] {
                                    secondPhase, result
                                });
                                result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] {
                                    thirdPhase, result
                                });
                                result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] {
                                    forthPhase, result
                                });
                                result = intCode.RunProgram(); intCode.Reset(); intCode.SetNewReadBuffer(new long[2] {
                                    fifthPhase, result
                                });
                                result = intCode.RunProgram();

                                if (max < result)
                                {
                                    max = result;
                                }
                            }
                        }
                    }
                }
            }

            //17406
            Console.WriteLine("Day 7 task 1 : " + max);
        }
예제 #12
0
 public void Part2()
 {
     var vm = IntCode.CreateVM("Data/Day17.txt", new() { [0] = 2 });
예제 #13
0
        private (long, IntCode) MoveRobot(IntCode intCode, int[] moves, ref bool end)
        {
            int[] movesNew = new int[moves.Length + 1];
            for (int index = 0; index < moves.Length; index++)
            {
                movesNew[index] = moves[index];
            }

            var returnValue = (0L, intCode);

            if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 2)
            {
                IntCode intCodeCopy = intCode.DeepCopy();
                intCode.SetNewReadBuffer(new long[1] {
                    1
                });
                long returnCode = intCode.RunProgram();
                if (returnCode == 1)
                {
                    movesNew[moves.Length] = 1;
                    returnValue            = MoveRobot(intCode, movesNew, ref end);
                    if (!end)
                    {
                        intCode = intCodeCopy;
                    }
                }
                else if (returnCode == 2)
                {
                    end = true; return(movesNew.Length, intCode.DeepCopy());
                }
            }

            if (end)
            {
                return(returnValue);
            }
            if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 1)
            {
                IntCode intCodeCopy = intCode.DeepCopy();
                intCode.SetNewReadBuffer(new long[1] {
                    2
                });
                long returnCode = intCode.RunProgram();
                if (returnCode == 1)
                {
                    movesNew[moves.Length] = 2;
                    returnValue            = MoveRobot(intCode, movesNew, ref end);
                    if (!end)
                    {
                        intCode = intCodeCopy;
                    }
                }
                else if (returnCode == 2)
                {
                    end = true; return(movesNew.Length, intCode.DeepCopy());
                }
            }

            if (end)
            {
                return(returnValue);
            }
            if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 4)
            {
                IntCode intCodeCopy = intCode.DeepCopy();
                intCode.SetNewReadBuffer(new long[1] {
                    3
                });
                long returnCode = intCode.RunProgram();
                if (returnCode == 1)
                {
                    movesNew[moves.Length] = 3;
                    returnValue            = MoveRobot(intCode, movesNew, ref end);
                    if (!end)
                    {
                        intCode = intCodeCopy;
                    }
                }
                else if (returnCode == 2)
                {
                    end = true; return(movesNew.Length, intCode.DeepCopy());
                }
            }

            if (end)
            {
                return(returnValue);
            }
            if (moves.Length == 0 || moves.Length > 0 && moves[moves.Length - 1] != 3)
            {
                IntCode intCodeCopy = intCode.DeepCopy();
                intCode.SetNewReadBuffer(new long[1] {
                    4
                });
                long returnCode = intCode.RunProgram();
                if (returnCode == 1)
                {
                    movesNew[moves.Length] = 4;
                    returnValue            = MoveRobot(intCode, movesNew, ref end);
                    //if (!end) { intCode = intCodeCopy; }
                }
                else if (returnCode == 2)
                {
                    end = true; return(movesNew.Length, intCode.DeepCopy());
                }
            }
            return(returnValue);
        }
예제 #14
0
        private long MoveOxygen(IntCode intCode)
        {
            Point point = new Point()
            {
                X = 0, Y = 0
            };
            IDictionary <Point, IntCode> PointState = new Dictionary <Point, IntCode>();

            PointState[point] = intCode;

            int count = 0;

            while (true)
            {
                bool canMove = false;
                IDictionary <Point, IntCode> PointStateNew = new Dictionary <Point, IntCode>();
                foreach (Point pointInitial in PointState.Keys)
                {
                    Point newPointNorth = new Point()
                    {
                        X = pointInitial.X, Y = pointInitial.Y + 1
                    };
                    if (!PointState.ContainsKey(newPointNorth))
                    {
                        IntCode intCodeCopy = PointState[pointInitial].DeepCopy();
                        intCodeCopy.SetNewReadBuffer(new long[1] {
                            1
                        });
                        long returnCode = intCodeCopy.RunProgram();
                        if (returnCode == 1)
                        {
                            PointStateNew[newPointNorth] = intCodeCopy;
                            canMove = true;
                        }
                    }

                    Point newPointSouth = new Point()
                    {
                        X = pointInitial.X, Y = pointInitial.Y - 1
                    };
                    if (!PointState.ContainsKey(newPointSouth) && !PointStateNew.ContainsKey(newPointSouth))
                    {
                        IntCode intCodeCopy = PointState[pointInitial].DeepCopy();
                        intCodeCopy.SetNewReadBuffer(new long[1] {
                            2
                        });
                        long returnCode = intCodeCopy.RunProgram();
                        if (returnCode == 1)
                        {
                            PointStateNew[newPointSouth] = intCodeCopy;
                            canMove = true;
                        }
                    }

                    Point newPointEast = new Point()
                    {
                        X = pointInitial.X + 1, Y = pointInitial.Y
                    };
                    if (!PointState.ContainsKey(newPointEast) && !PointStateNew.ContainsKey(newPointEast))
                    {
                        IntCode intCodeCopy = PointState[pointInitial].DeepCopy();
                        intCodeCopy.SetNewReadBuffer(new long[1] {
                            3
                        });
                        long returnCode = intCodeCopy.RunProgram();
                        if (returnCode == 1)
                        {
                            PointStateNew[newPointEast] = intCodeCopy;
                            canMove = true;
                        }
                    }

                    Point newPointWest = new Point()
                    {
                        X = pointInitial.X - 1, Y = pointInitial.Y
                    };
                    if (!PointState.ContainsKey(newPointWest) && !PointStateNew.ContainsKey(newPointWest))
                    {
                        IntCode intCodeCopy = PointState[pointInitial].DeepCopy();
                        intCodeCopy.SetNewReadBuffer(new long[1] {
                            4
                        });
                        long returnCode = intCodeCopy.RunProgram();
                        if (returnCode == 1)
                        {
                            PointStateNew[newPointWest] = intCodeCopy;
                            canMove = true;
                        }
                    }
                }

                if (canMove)
                {
                    count++;
                    foreach (Point pointNew in PointStateNew.Keys)
                    {
                        PointState[pointNew] = PointStateNew[pointNew];
                    }
                }
                else
                {
                    break;
                }
            }

            return(count);
        }