Initial new project commit
This commit is contained in:
parent
f1df483d7c
commit
fba5683ec5
|
@ -0,0 +1,16 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode", "AdventOfCode\AdventOfCode.csproj", "{D9E24F29-20E8-4271-89A3-4940A5A044D7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{D9E24F29-20E8-4271-89A3-4940A5A044D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D9E24F29-20E8-4271-89A3-4940A5A044D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D9E24F29-20E8-4271-89A3-4940A5A044D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D9E24F29-20E8-4271-89A3-4940A5A044D7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,13 @@
|
|||
using AdventOfCode.Year2022;
|
||||
|
||||
namespace AdventOfCode;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
new Day1();
|
||||
new Day3();
|
||||
new Day7();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
namespace AdventOfCode.Year2022;
|
||||
|
||||
public class Day1
|
||||
{
|
||||
private static List<int> _caloriesPerElf = new();
|
||||
|
||||
public Day1()
|
||||
{
|
||||
Console.WriteLine("\nDay1 Solution\n");
|
||||
LoadCaloriesPerElf();
|
||||
Console.WriteLine($"Part1\nResult: {Part1()}\n");
|
||||
Console.WriteLine($"Part2\nResult: {Part2()}\n");
|
||||
Console.WriteLine("=============================\n");
|
||||
}
|
||||
|
||||
private static int Part1()
|
||||
{
|
||||
return _caloriesPerElf.Max();
|
||||
}
|
||||
|
||||
private static int Part2()
|
||||
{
|
||||
List<int> top3 = new List<int>();
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
top3.Add(_caloriesPerElf.Max());
|
||||
_caloriesPerElf.Remove(_caloriesPerElf.Max());
|
||||
}
|
||||
|
||||
return top3.Sum();
|
||||
}
|
||||
|
||||
private static void LoadCaloriesPerElf()
|
||||
{
|
||||
string[] calories = File.ReadAllLines("inputs/day1.txt");
|
||||
int[] caloriesPerDay = new int[calories.Length];
|
||||
var index = 0;
|
||||
foreach (string calorie in calories)
|
||||
{
|
||||
if (calorie == "")
|
||||
index++;
|
||||
else
|
||||
caloriesPerDay[index] += Convert.ToInt32(calorie);
|
||||
}
|
||||
|
||||
int elfCount = caloriesPerDay.ToList().IndexOf(0);
|
||||
_caloriesPerElf = caloriesPerDay.ToList().GetRange(0, elfCount);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
namespace AdventOfCode.Year2022;
|
||||
|
||||
public class Day3
|
||||
{
|
||||
private static readonly string[] backpacks = File.ReadAllLines("inputs/day3.txt");
|
||||
|
||||
public Day3()
|
||||
{
|
||||
Console.WriteLine("Day3 Solution\n");
|
||||
Console.WriteLine($"Part1\nResult: {Part1()}\n");
|
||||
Console.WriteLine($"Part2\nResult: {Part2()}\n");
|
||||
Console.WriteLine("=============================\n");
|
||||
}
|
||||
|
||||
private static int Part1()
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
List<char> duplicates = new List<char>();
|
||||
|
||||
foreach (string backpack in backpacks)
|
||||
{
|
||||
int compartmentSize = backpack.Length / 2;
|
||||
List<char> itemsInCompartment1 = new List<char>();
|
||||
List<char> itemsInCompartment2 = new List<char>();
|
||||
|
||||
for (int i = 0; i < compartmentSize; i++)
|
||||
itemsInCompartment1.Add(backpack[i]);
|
||||
for (int i = compartmentSize; i < compartmentSize * 2; i++)
|
||||
itemsInCompartment2.Add(backpack[i]);
|
||||
|
||||
char duplicatedItem = itemsInCompartment1.Intersect(itemsInCompartment2).FirstOrDefault();
|
||||
duplicates.Add(duplicatedItem);
|
||||
}
|
||||
|
||||
foreach (char duplicate in duplicates)
|
||||
{
|
||||
if (Char.IsUpper(duplicate))
|
||||
sum += Convert.ToInt16(duplicate) - 38;
|
||||
else
|
||||
sum += Convert.ToInt16(duplicate) - 96;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static int Part2()
|
||||
{
|
||||
int sum = 0;
|
||||
List<List<string>> groups = new List<List<string>>();
|
||||
|
||||
for (int i = 0; i < backpacks.Length; i+=3)
|
||||
{
|
||||
List<string> group = new List<string>();
|
||||
|
||||
for (int x = 0; x < 3; x++)
|
||||
{
|
||||
try
|
||||
{
|
||||
group.Add(backpacks[i + x]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (group.All(x => x != ""))
|
||||
groups.Add(group);
|
||||
}
|
||||
|
||||
List<char> duplicates = new List<char>();
|
||||
foreach (List<string> group in groups)
|
||||
{
|
||||
string[] groupArray = group.ToArray();
|
||||
duplicates.Add(groupArray[0].Intersect(groupArray[1].Intersect(groupArray[2])).FirstOrDefault());
|
||||
}
|
||||
|
||||
foreach (char duplicate in duplicates)
|
||||
{
|
||||
if (Char.IsUpper(duplicate))
|
||||
sum += Convert.ToInt16(duplicate) - 38;
|
||||
else
|
||||
sum += Convert.ToInt16(duplicate) - 96;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
namespace AdventOfCode.Year2022;
|
||||
|
||||
public class Day7
|
||||
{
|
||||
private static Dictionary<string, List<string>> tree = new();
|
||||
|
||||
public Day7()
|
||||
{
|
||||
Console.WriteLine("Day7 Solution\n");
|
||||
LoadTree();
|
||||
Console.WriteLine($"Part1\nResult: {Part1()}\n");
|
||||
Console.WriteLine($"Part2\nResult: {Part2()}\n");
|
||||
Console.WriteLine("=============================\n");
|
||||
}
|
||||
|
||||
private static int Part1()
|
||||
{
|
||||
int sum = 0;
|
||||
foreach (string path in tree.Keys)
|
||||
{
|
||||
int size = CalculateDirSize(path);
|
||||
if (size <= 100000)
|
||||
sum += size;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static int Part2()
|
||||
{
|
||||
int neededSpace = CalculateDirSize("/") - 40000000;
|
||||
if (neededSpace <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
List<int> bigEnoughDir = new List<int>();
|
||||
foreach (string path in tree.Keys)
|
||||
{
|
||||
int size = CalculateDirSize(path);
|
||||
if (size > neededSpace)
|
||||
bigEnoughDir.Add(size);
|
||||
}
|
||||
|
||||
bigEnoughDir.Sort();
|
||||
return bigEnoughDir[0];
|
||||
}
|
||||
|
||||
private static int CalculateDirSize(string path)
|
||||
{
|
||||
int size = 0;
|
||||
List<string> dirContent = tree[path];
|
||||
foreach (string content in dirContent)
|
||||
{
|
||||
string[] properties = content.Split(" ");
|
||||
if (properties[0] == "dir")
|
||||
size += CalculateDirSize(path + properties[1] + "/");
|
||||
else
|
||||
size += Convert.ToInt32(properties[0]);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
private static void LoadTree()
|
||||
{
|
||||
string currentPath = "";
|
||||
|
||||
IEnumerable<string> input = File.ReadLines("inputs/day7.txt");
|
||||
foreach (string line in input)
|
||||
{
|
||||
if (line.StartsWith("$"))
|
||||
{
|
||||
string[] cmdLine = line.Substring(2).Split(" ");
|
||||
if (cmdLine[0] == "cd")
|
||||
{
|
||||
string dir = cmdLine[1];
|
||||
if (dir == "/")
|
||||
currentPath = "/";
|
||||
else if (dir == "..")
|
||||
{
|
||||
if (currentPath == "/") continue;
|
||||
int slashIndex = currentPath
|
||||
.Substring(0, currentPath.Length - 1)
|
||||
.LastIndexOf('/');
|
||||
currentPath = currentPath.Substring(0, slashIndex + 1);
|
||||
}
|
||||
else
|
||||
currentPath += dir + "/";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!tree.ContainsKey(currentPath))
|
||||
tree.Add(currentPath, new List<string>());
|
||||
tree[currentPath].Add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue