Untitled
unknown
csharp
2 years ago
6.2 kB
9
Indexable
using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace Day8 { public class Tree { public int Id { get; set; } public int Height { get; set; } public int SST { get; set; } public int SSB { get; set; } public int SSL { get; set; } public int SSR { get; set; } } public class Program { static void Main(string[] args) { var Input = File.ReadAllLines(@"input.txt").Select(x => x.Select(z => z - '0').ToArray()).ToList(); var treeId = 100; var Forest = new List<List<Tree>>(); foreach (var ListList in Input) { var NewTreeList = new List<Tree>(); foreach (var height in ListList) { NewTreeList.Add(new Tree() { Id = treeId, Height = height }); treeId++; } Forest.Add(NewTreeList); } //Part2 for (int i = 1; i < Forest.Count - 1; i++) { for (int j = 0; j < Forest[i].Count() - 1; j++) { var currentTree = Forest[i][j]; bool blockedViewBottom = false; for (int z = 1; z <= Forest[i].Count() - i -1; z++) { if (!blockedViewBottom) { currentTree.SSB++; if (currentTree.Height > Forest[i + z][j].Height) { } else if(currentTree.Height == Forest[i + z][j].Height) { blockedViewBottom = true; } else { blockedViewBottom = true; } } } bool blockedViewTop = false; for (int z = 1; z <= i; z++) { if (!blockedViewTop) { currentTree.SST++; if (currentTree.Height > Forest[i - z][j].Height) { } else if (currentTree.Height == Forest[i - z][j].Height) { blockedViewTop = true; } else { blockedViewTop = true; } } } bool blockedViewRight = false; for (int z = 1; z < Forest[i].Count() - j; z++) { if (!blockedViewRight) { currentTree.SSR++; if (currentTree.Height > Forest[i][j + z].Height) { } else if (currentTree.Height == Forest[i][j + z].Height) { blockedViewRight = true; } else { blockedViewRight = true; } } } //left bool blockedViewLeft = false; for (int z = 1; z <= j; z++) { if (!blockedViewLeft) { currentTree.SSL++; if (currentTree.Height > Forest[i][j - z].Height) { } else if(currentTree.Height == Forest[i][j - z].Height) { blockedViewLeft = true; } else { blockedViewLeft = true; } } } } } //Part 1 var trees = new List<Tree>(); for (int i = 0; i < Forest.Count; i++) { var top = Forest.Select(x => x[i]).ToList(); trees.AddRange(GetTrees(top)); var bottom = Forest.Select(x => x[i]).Reverse<Tree>().ToList(); trees.AddRange(GetTrees(bottom)); var left = Forest[i]; trees.AddRange(GetTrees(left)); var right = Forest[i].Reverse<Tree>().ToList(); trees.AddRange(GetTrees(right)); } var part1 = trees.GroupBy(x => x.Id).Count(); var part2 = Forest.SelectMany(z => z.Select(x => x.SST * x.SSL * x.SSB * x.SSR)).Max(); Console.WriteLine(part1); Console.WriteLine(part2); Console.ReadLine(); } public static List<Tree> GetTrees(List<Tree> trees) { var countedTrees = new List<Tree>(); countedTrees.Add(trees.First()); var tempHighest = trees[0].Height; for (int i = 0; i < trees.Count; i++) { for (int j = 1; j < trees.Count; j++) { if (trees[i].Height < trees[j].Height && trees[j].Height > tempHighest) { countedTrees.Add(trees[j]); tempHighest = trees[j].Height; } } } return countedTrees; } } }
Editor is loading...