Example #1
0
        static ulong TraverseSlope(List <string> pattern, Slope slope)
        {
            var treesEncountered   = 0UL;
            var patternLengthRight = pattern[0].Count();
            var bottomOfSlope      = pattern.Count();
            var currentRow         = 0;
            var currentColumn      = 0;

            while (currentRow <= bottomOfSlope)
            {
                currentColumn += slope.Right;
                currentRow    += slope.Down;

                var patternColumn = currentColumn - (patternLengthRight * (currentColumn / patternLengthRight));

                if (currentRow < bottomOfSlope && pattern[currentRow][patternColumn] == '#')
                {
                    treesEncountered++;
                }
            }

            Console.WriteLine($@"We would encounter {treesEncountered} trees on a slope of (Down {slope.Down}, Right {slope.Right}).");

            return(treesEncountered);
        }
Example #2
0
 static Func <string, int, bool> TreeDetectorFactory(Slope currentTrajectory)
 {
     return((x, index) =>
     {
         if ((index + 1) % currentTrajectory.Down == 0)
         {
             return x[(index * currentTrajectory.Right) % x.Length] == '#';
         }
         return false;
     });
 }
Example #3
0
        static ulong TraverseSlopeWithMap(List <string> pattern, Slope slope)
        {
            var treesEncountered   = 0UL;
            var patternLengthRight = pattern[0].Count();
            var patternLengthDown  = pattern.Count();
            var repeatPatternRight = slope.Right * (patternLengthDown / slope.Down);
            var repeatPatternDown  = 1;
            var totalColumns       = patternLengthRight * repeatPatternRight;
            var totalRows          = patternLengthDown * repeatPatternDown;
            var currentRow         = 0;
            var currentColumn      = 0;
            var map = new List <string>();

            for (var i = 0; i < pattern.Count(); i++)
            {
                var row = string.Concat(Enumerable.Repeat(pattern[i], repeatPatternRight));
                map.Add(row);
            }

            System.IO.File.WriteAllLines($@"map_{slope.Down}_{slope.Right}.txt", map);

            var mapTraversed = map.ToList();

            while (currentRow <= totalRows)
            {
                currentColumn += slope.Right;
                currentRow    += slope.Down;

                if (currentRow < totalRows)
                {
                    if (mapTraversed[currentRow][currentColumn] == '#')
                    {
                        mapTraversed[currentRow] = MarkMap(mapTraversed[currentRow], currentColumn, 'X');
                        treesEncountered++;
                    }
                    else
                    {
                        mapTraversed[currentRow] = MarkMap(mapTraversed[currentRow], currentColumn, 'O');
                    }
                }
            }

            System.IO.File.WriteAllLines($@"maptraversed_{slope.Down}_{slope.Right}.txt", mapTraversed);

            Console.WriteLine($@"We would encounter {treesEncountered} trees on a slope of (Down {slope.Down}, Right {slope.Right}).");

            return(treesEncountered);
        }