[2022] Day8 added

This commit is contained in:
Tiago Garcia 2023-06-08 13:59:45 +01:00
parent a4d072ae17
commit 6f2f699ea5
No known key found for this signature in database
GPG Key ID: F423E37A2415E160
3 changed files with 102 additions and 2 deletions

View File

@ -11,3 +11,4 @@
* [Day 4](https://github.com/TiagoRG/AdventOfCode/tree/main/AdventOfCode/Year2022/Day4.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 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) * [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)

99
Year2022/Day8.cs Normal file
View File

@ -0,0 +1,99 @@
using AdventOfCode.Utils;
namespace AdventOfCode.Year2022;
public class Day8
{
private static readonly List<List<int>> 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<List<int>> LoadMap()
{
List<List<int>> map = new List<List<int>>();
string[] lines = File.ReadAllLines("inputs/day8.txt");
for (int i = 0; i < lines.Length; i++)
{
map.Add(new List<int>());
for (int j = 0; j < lines[i].Length; j++)
map[i].Add(Convert.ToInt32(lines[i][j].ToString()));
}
return map;
}
}

View File

@ -10,6 +10,6 @@ public static class Loader2022
new Day4(); new Day4();
new Day6(); new Day6();
new Day7(); new Day7();
new Day8(); // new Day8(); -- Commented because it's a slow solution
} }
} }