commit fb3a16c7725a499bedf23831ea0635a8f580206f Author: henrik Date: Tue Dec 3 20:34:16 2024 +0100 Initial Days diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84938ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ +*.txt \ No newline at end of file diff --git a/.idea/.idea.AdventOfCode2024/.idea/.gitignore b/.idea/.idea.AdventOfCode2024/.idea/.gitignore new file mode 100644 index 0000000..f4d4658 --- /dev/null +++ b/.idea/.idea.AdventOfCode2024/.idea/.gitignore @@ -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 diff --git a/.idea/.idea.AdventOfCode2024/.idea/encodings.xml b/.idea/.idea.AdventOfCode2024/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.AdventOfCode2024/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.AdventOfCode2024/.idea/indexLayout.xml b/.idea/.idea.AdventOfCode2024/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.AdventOfCode2024/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.AdventOfCode2024/.idea/vcs.xml b/.idea/.idea.AdventOfCode2024/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.AdventOfCode2024/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AdventOfCode2024.sln b/AdventOfCode2024.sln new file mode 100644 index 0000000..b9a446a --- /dev/null +++ b/AdventOfCode2024.sln @@ -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 diff --git a/AdventOfCode2024.sln.DotSettings.user b/AdventOfCode2024.sln.DotSettings.user new file mode 100644 index 0000000..3d7c446 --- /dev/null +++ b/AdventOfCode2024.sln.DotSettings.user @@ -0,0 +1,4 @@ + + ForceIncluded + ForceIncluded + \ No newline at end of file diff --git a/Day1/Day1.csproj b/Day1/Day1.csproj new file mode 100644 index 0000000..1199b13 --- /dev/null +++ b/Day1/Day1.csproj @@ -0,0 +1,20 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + + PreserveNewest + + + + diff --git a/Day1/Program.cs b/Day1/Program.cs new file mode 100644 index 0000000..61acb2b --- /dev/null +++ b/Day1/Program.cs @@ -0,0 +1,21 @@ +string[] inputs = File.ReadAllLines("input.txt"); + +var a = new List(); +var b = new List(); +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); \ No newline at end of file diff --git a/Day2/Day2.csproj b/Day2/Day2.csproj new file mode 100644 index 0000000..961901c --- /dev/null +++ b/Day2/Day2.csproj @@ -0,0 +1,16 @@ + + + + Exe + net9.0 + enable + enable + + + + + PreserveNewest + + + + diff --git a/Day2/Program.cs b/Day2/Program.cs new file mode 100644 index 0000000..9bdb711 --- /dev/null +++ b/Day2/Program.cs @@ -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 connected = new LinkedList(); + 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 linkedList) +{ + int unsafeCount = 0; + var node = linkedList.First; + List directions = new List(); + 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 +} \ No newline at end of file diff --git a/Day3/Day3.csproj b/Day3/Day3.csproj new file mode 100644 index 0000000..961901c --- /dev/null +++ b/Day3/Day3.csproj @@ -0,0 +1,16 @@ + + + + Exe + net9.0 + enable + enable + + + + + PreserveNewest + + + + diff --git a/Day3/Program.cs b/Day3/Program.cs new file mode 100644 index 0000000..00de21f --- /dev/null +++ b/Day3/Program.cs @@ -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\\((?[0-9]+),(?[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); \ No newline at end of file