diff --git a/README.md b/README.md index 108bcab..3baf39c 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,5 @@ * [Day 3](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day3.cs) * [Day 4](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day4.cs) * [Day 6](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day6.cs) -* [Day 7](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day7.cs) \ No newline at end of file +* [Day 7](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day7.cs) +* [Day 8](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day8.cs) \ No newline at end of file diff --git a/Year2022/Day8.cs b/Year2022/Day8.cs new file mode 100644 index 0000000..7cd96c6 --- /dev/null +++ b/Year2022/Day8.cs @@ -0,0 +1,99 @@ +using AdventOfCode.Utils; + +namespace AdventOfCode.Year2022; + +public class Day8 +{ + private static readonly List> TreeMap = LoadMap(); + + public Day8() + { + Console.WriteLine("Day8 Solution"); + Console.WriteLine($"Part1 Result: {Part1()}"); + Console.WriteLine($"Part2 Result: {Part2()}"); + Console.WriteLine("\n=============================\n"); + } + + private static int Part1() + { + int count = 0; + for (int i = 0; i < TreeMap.Count; i++) + for (int j = 0; j < TreeMap[i].Count; j++) + { + if (i == 0 || i == TreeMap.Count-1 || j == 0 || j == TreeMap.Count-1 + || TreeMap[i].Sublist(0, j).Max() < TreeMap[i][j] + || TreeMap[i].Sublist(j + 1, TreeMap[i].Count).DefaultIfEmpty().Max() < TreeMap[i][j] + || TreeMap.GetColumn(j).Sublist(0, i).Max() < TreeMap[i][j] + || TreeMap.GetColumn(j).Sublist(i + 1, TreeMap[i].Count).DefaultIfEmpty().Max() < TreeMap[i][j]) + count++; + } + + return count; + } + + private static int Part2() + { + int highestScore = 0; + + for (int i = 0; i < TreeMap.Count; i++) + for (int j = 0; j < TreeMap[i].Count; j++) + { + if (i == 0 || j == 0) continue; + + int currentTree = TreeMap[i][j]; + int currentTreeScore = 1; + int directionCount = 0; + + for (int k = i - 1; k >= 0; k--) + { + directionCount++; + if (TreeMap[k][j] >= currentTree) break; + } + + currentTreeScore *= directionCount; + directionCount = 0; + + for (int k = j - 1; k >= 0; k--) + { + directionCount++; + if (TreeMap[i][k] >= currentTree) break; + } + + currentTreeScore *= directionCount; + directionCount = 0; + + for (int k = i + 1; k < TreeMap.Count; k++) + { + directionCount++; + if (TreeMap[k][j] >= currentTree) break; + } + + currentTreeScore *= directionCount; + directionCount = 0; + + for (int k = j + 1; k < TreeMap[i].Count; k++) + { + directionCount++; + if (TreeMap[i][k] >= currentTree) break; + } + + currentTreeScore *= directionCount; + highestScore = Math.Max(highestScore, currentTreeScore); + } + + return highestScore; + } + + private static List> LoadMap() + { + List> map = new List>(); + string[] lines = File.ReadAllLines("inputs/day8.txt"); + for (int i = 0; i < lines.Length; i++) + { + map.Add(new List()); + for (int j = 0; j < lines[i].Length; j++) + map[i].Add(Convert.ToInt32(lines[i][j].ToString())); + } + return map; + } +} \ No newline at end of file diff --git a/Year2022/Loader2022.cs b/Year2022/Loader2022.cs index 13c1394..110230c 100644 --- a/Year2022/Loader2022.cs +++ b/Year2022/Loader2022.cs @@ -10,6 +10,6 @@ public static class Loader2022 new Day4(); new Day6(); new Day7(); - new Day8(); + // new Day8(); -- Commented because it's a slow solution } } \ No newline at end of file