Esempio n. 1
0
 public Day13()
 {
     //Test = true;
     PuzzleInput   = Test ? part2puzzle : puzzle;
     ID            = 13;
     Name          = "Day 13: Mine Cart Madness";
     trackLines    = PuzzleInput.Replace("\r\n", "").ToCharArray();
     originalTrack = PuzzleInput.Replace("\r\n", "").Replace(">", "-").Replace("<", "-").Replace("^", "|").Replace("v", "|").ToCharArray();
     stride        = PuzzleInput.Substring(0, PuzzleInput.IndexOf("\r\n")).Length;
 }
Esempio n. 2
0
        public override string Part1()
        {
            var strideX = PuzzleInput.Substring(0, PuzzleInput.IndexOf("\r\n")).Length;
            var strideY = PuzzleInput.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Count();
            var yVal    = 0;
            var acres   = new int[strideX, strideY];

            foreach (var puzzleAcre in PuzzleInput.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList())
            {
                var x = 0;
                foreach (var character in puzzleAcre)
                {
                    acres[x, yVal] = character;
                    x++;
                }
                yVal++;
            }
            if (Test)
            {
                Print(acres);
            }

            var minutes = 10;

            for (int i = 0; i < minutes; i++)
            {
                int[,] newAcres = acres.Clone() as int[, ];

                for (int y = 0; y < acres.GetLength(1); y++)
                {
                    for (int x = 0; x < acres.GetLength(0); x++)
                    {
                        newAcres[x, y] = (int)GetNewAcreType(x, y, acres);
                    }
                }
                acres = newAcres;
                if (Test)
                {
                    Print(acres);
                }
            }

            //Count wooded * lumberyars
            return(LumberResourcesAnswer(acres));
        }
Esempio n. 3
0
        public override string Part2()
        {
            var strideX        = PuzzleInput.Substring(0, PuzzleInput.IndexOf("\r\n")).Length;
            var strideY        = PuzzleInput.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Count();
            var yVal           = 0;
            var acres          = new int[strideX, strideY];
            var patternHistory = new Dictionary <int, int[, ]>();

            int[,] patternToFind = null;
            var sequence = 0;

            foreach (var puzzleAcre in PuzzleInput.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList())
            {
                var x = 0;
                foreach (var character in puzzleAcre)
                {
                    acres[x, yVal] = character;
                    x++;
                }
                yVal++;
            }
            if (Test)
            {
                Print(acres);
            }

            var minutes = 1000000000;

            for (int i = 0; i < minutes; i++)
            {
                int[,] newAcres = acres.Clone() as int[, ];

                for (int y = 0; y < acres.GetLength(1); y++)
                {
                    for (int x = 0; x < acres.GetLength(0); x++)
                    {
                        newAcres[x, y] = (int)GetNewAcreType(x, y, acres);
                    }
                }

                //First find recurrent pattern
                if (patternToFind == null)
                {
                    int?isknownpattern = CheckPatterns(patternHistory, newAcres);

                    if (isknownpattern.HasValue)
                    {
                        sequence      = i;
                        patternToFind = patternHistory.Where(c => c.Key == isknownpattern).First().Value;
                    }
                    patternHistory.Add(i, newAcres);
                }
                //Recurrent pattern has been found, check after how many minutes pattern shows up again
                else
                {
                    if (CheckPattern(patternToFind, newAcres))
                    {
                        //Reduce remainingminutes based on pattern-stride
                        sequence = i - sequence;
                        var remainingloops = (minutes - i) % sequence;
                        i = minutes - remainingloops;
                    }
                    ;
                    patternHistory.Add(i, newAcres);
                }
                acres = newAcres;
            }

            //Count wooded * lumberyars
            return(LumberResourcesAnswer(acres));
        }