コード例 #1
0
ファイル: Day9.cs プロジェクト: WardenGnaw/AdventOfCode
        public void Part2()
        {
            string data = IO.ReadFile(Path.Combine(Directory.GetCurrentDirectory(), "input1.txt"));

            Dictionary <Tuple <int, int>, Node> grid = new Dictionary <Tuple <int, int>, Node>();

            string[] lines  = data.Split("\r\n");
            int      maxRow = int.MinValue;
            int      i      = 0;

            foreach (string line in lines)
            {
                int[] numbers = line.ToList().Select(x => int.Parse(x.ToString())).ToArray();
                maxRow = numbers.Length;
                for (int j = 0; j < numbers.Length; j++)
                {
                    grid.Add(Tuple.Create(i, j), new Node(i, j, numbers[j], grid));
                }
                i++;
            }

            List <Node> lowPoints = new();

            foreach (var node in grid.Values)
            {
                if (node.IsLowPoint(i, maxRow))
                {
                    lowPoints.Add(node);
                }
            }

            List <Basin> basins = new();

            // All basins flow to a low point so start from low point.
            foreach (Node n in lowPoints)
            {
                if (!n.IsLowPoint(i, maxRow) || n.IsInBasin)
                {
                    continue;
                }
                Basin b = new Basin(n, grid, i, maxRow);

                basins.Add(b);
            }

            // Sort from greatest to least
            basins.Sort((a, b) => b.Size - a.Size);

            // Take the top 3 and multiply
            Assert.Equal(0, basins.Take(3).Aggregate(1, (count, b) => count * b.Size));
        }
コード例 #2
0
ファイル: Day9.cs プロジェクト: WardenGnaw/AdventOfCode
 public void AddToBasin(Basin b)
 {
     this.basin = b;
 }