Initial Days

This commit is contained in:
henrik
2024-12-03 20:34:16 +01:00
commit fb3a16c772
13 changed files with 252 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
*.txt

13
.idea/.idea.AdventOfCode2024/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.AdventOfCode2024.iml
/modules.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

28
AdventOfCode2024.sln Normal file
View File

@@ -0,0 +1,28 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day1", "Day1\Day1.csproj", "{E1C9A86B-41EA-46A2-8FB3-76B39BE8A12A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day2", "Day2\Day2.csproj", "{58A7974C-0349-48F2-A82D-2D5B6DECAED2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day3", "Day3\Day3.csproj", "{AB523613-5CF2-4D73-8642-2E174E58F699}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E1C9A86B-41EA-46A2-8FB3-76B39BE8A12A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1C9A86B-41EA-46A2-8FB3-76B39BE8A12A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1C9A86B-41EA-46A2-8FB3-76B39BE8A12A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1C9A86B-41EA-46A2-8FB3-76B39BE8A12A}.Release|Any CPU.Build.0 = Release|Any CPU
{58A7974C-0349-48F2-A82D-2D5B6DECAED2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58A7974C-0349-48F2-A82D-2D5B6DECAED2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58A7974C-0349-48F2-A82D-2D5B6DECAED2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58A7974C-0349-48F2-A82D-2D5B6DECAED2}.Release|Any CPU.Build.0 = Release|Any CPU
{AB523613-5CF2-4D73-8642-2E174E58F699}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB523613-5CF2-4D73-8642-2E174E58F699}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB523613-5CF2-4D73-8642-2E174E58F699}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB523613-5CF2-4D73-8642-2E174E58F699}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASelect_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F46e849375f3e8fcb25f51d7367c4f2e3c07bef61f5f208617124579d3afc5a_003FSelect_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F8874fa6fbc50b05ba8332188d36141eb4bba81fb1f92189ba9d7a25f545_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
</wpf:ResourceDictionary>

20
Day1/Day1.csproj Normal file
View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TUnit" Version="0.4.51" />
</ItemGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

21
Day1/Program.cs Normal file
View File

@@ -0,0 +1,21 @@
string[] inputs = File.ReadAllLines("input.txt");
var a = new List<int>();
var b = new List<int>();
foreach (string input in inputs)
{
string[] tokens = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
a.Add(int.Parse(tokens[0]));
b.Add(int.Parse(tokens[1]));
}
a = a.OrderBy(x => x).ToList();
b = b.OrderBy(x => x).ToList();
int totalDistance = a.Select((t, i) => Math.Abs(t - b[i])).Sum();
Console.WriteLine(totalDistance);
var aBucket = a.GroupBy(x => x);
var star2 = aBucket.Select(bucket => bucket.Key * b.Count(x => x == bucket.Key) * bucket.Count()).Sum();
Console.WriteLine(star2);

16
Day2/Day2.csproj Normal file
View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

85
Day2/Program.cs Normal file
View File

@@ -0,0 +1,85 @@
string[] lines = """
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
""".Split('\n', StringSplitOptions.RemoveEmptyEntries);
// string[] lines = System.IO.File.ReadAllLines("input.txt");
int levelsSafe = 0;
foreach (string input in lines)
{
LinkedList<int> connected = new LinkedList<int>();
var levels = input.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
foreach (var paredLevel in levels)
{
connected.AddLast(paredLevel);
}
Console.Write($"Testing {input} = ");
var safe = TestLevel(connected);
Console.WriteLine($"{(safe ? "Safe" : "Unsafe")}");
levelsSafe += safe ? 1 : 0;
}
Console.WriteLine(levelsSafe);
bool TestLevel(LinkedList<int> linkedList)
{
int unsafeCount = 0;
var node = linkedList.First;
List<LevelDirection> directions = new List<LevelDirection>();
while (node != null)
{
bool safeRange = node switch
{
{ Previous: not null, Next: not null }
=> InsideRange2(node.Previous.Value, node.Value, node.Next.Value),
{ Previous: not null, Next: null }
=> InsideRange(node.Previous.Value, node.Value),
{ Previous: null, Next: not null }
=> InsideRange(node.Next.Value, node.Value),
_ => throw new InvalidOperationException()
};
if(safeRange is false)
{
unsafeCount++;
}
if (node.Next is not null)
{
directions.Add(node.Value > node.Next.Value ? LevelDirection.Increasing : LevelDirection.Decreasing);
}
node = node.Next;
}
bool allIncreasing = directions.All(x => x == LevelDirection.Increasing);
bool allDecreasing = directions.All(x => x == LevelDirection.Decreasing);
Console.Write($" {unsafeCount}|{allIncreasing}|{allDecreasing} ");
return ((allIncreasing || allDecreasing) && unsafeCount == 0) || (allIncreasing == false && allDecreasing == false);
bool InsideRange(int current, int target)
{
var abs = Math.Abs(current - target);
return abs is >= 1 and <= 3;
}
bool InsideRange2(int prev, int current, int next)
{
var p = Math.Abs(prev - current);
var n = Math.Abs(next - current);
return p is >= 1 and <= 3 && n is >= 1 and <= 3;
}
}
enum LevelDirection
{
Increasing,
Decreasing
}

16
Day3/Day3.csproj Normal file
View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

25
Day3/Program.cs Normal file
View File

@@ -0,0 +1,25 @@
using System.Text.RegularExpressions;
// string input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
string input = File.ReadAllText("input.txt").ReplaceLineEndings("");
int total = 0;
Regex valid = new Regex("mul\\((?<x>[0-9]+),(?<y>[0-9]+)\\)");
Regex dontandDo = new Regex("(don\\'t\\(\\).*?do\\(\\))");
input = dontandDo.Replace(input, "");
List<(int x, int y)> multiplies = new();
foreach (Match match in valid.Matches(input))
{
string x = match.Groups["x"].Value;
string y = match.Groups["y"].Value;
multiplies.Add((int.Parse(x), int.Parse(y)));
}
foreach ((int x, int y) in multiplies)
{
total += x * y;
}
Console.WriteLine(total);