From 47f468ff687cef6af34c08386c0d31da77b6913e Mon Sep 17 00:00:00 2001 From: Sebastian Lindemeier Date: Sat, 6 Dec 2025 10:54:11 +0100 Subject: [PATCH] Add Day 06 solution, needs cleanup later --- AoC_2025.Runner/Program.cs | 6 +++ AoC_2025.Tests/Day06Test.cs | 47 +++++++++++++++++++++++ AoC_2025/Day05.cs | 22 +++++++---- AoC_2025/Day06.cs | 76 +++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 AoC_2025.Tests/Day06Test.cs create mode 100644 AoC_2025/Day06.cs diff --git a/AoC_2025.Runner/Program.cs b/AoC_2025.Runner/Program.cs index 252a70e..c0967de 100644 --- a/AoC_2025.Runner/Program.cs +++ b/AoC_2025.Runner/Program.cs @@ -32,4 +32,10 @@ var day05 = new Day05(); string day05Path = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day05.txt"; Console.WriteLine(day05.SolvePart1(day05Path)); Console.WriteLine(day05.SolvePart2(day05Path)); +Console.WriteLine(); + +var day06 = new Day06(); +string day06Path = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day06.txt"; +Console.WriteLine(day06.SolvePart1(day06Path)); +Console.WriteLine(day06.SolvePart2(day06Path)); Console.WriteLine(); \ No newline at end of file diff --git a/AoC_2025.Tests/Day06Test.cs b/AoC_2025.Tests/Day06Test.cs new file mode 100644 index 0000000..f51613c --- /dev/null +++ b/AoC_2025.Tests/Day06Test.cs @@ -0,0 +1,47 @@ +using AdvenOfCode.Contracts; + +namespace AoC_2025.Tests; + +public class Day06Test +{ + private readonly IPuzzleSolver _sut; + private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + private readonly string TestInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day06.txt"; + private readonly string ProdInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day06.txt"; + + public Day06Test() + { + _sut = new Day06(); + } + [Fact] + public void Part01_Test_equals_4277556() + { + var actual = _sut.SolvePart1(TestInputPath); + + Assert.Equal(4277556, actual); + } + + [Fact] + public void Part01_Prod_equals_6299564383938() + { + var actual = _sut.SolvePart1(ProdInputPath); + + Assert.Equal(6299564383938, actual); + } + + [Fact] + public void Part02_Test_equals_3263827() + { + var actual = _sut.SolvePart2(TestInputPath); + + Assert.Equal(3263827, actual); + } + + [Fact] + public void Part02_Prod_equals_11950004808442() + { + var actual = _sut.SolvePart2(ProdInputPath); + + Assert.Equal(11950004808442, actual); + } +} \ No newline at end of file diff --git a/AoC_2025/Day05.cs b/AoC_2025/Day05.cs index 45c746e..5453d9b 100644 --- a/AoC_2025/Day05.cs +++ b/AoC_2025/Day05.cs @@ -99,15 +99,14 @@ public class Day05 : IPuzzleSolver var optimizedRanges = new List(ranges.Length) { ranges[0] }; foreach (var range in ranges) { - var lastRange = optimizedRanges[^1]; - if (range.from > lastRange.to) - { - optimizedRanges.Add(range); - } - else if (TryCombineRanges(lastRange, range, out var combined)) + if (TryCombineRanges(optimizedRanges[^1], range, out var combined)) { optimizedRanges[^1] = combined; } + else + { + optimizedRanges.Add(range); + } } return optimizedRanges; @@ -115,11 +114,18 @@ public class Day05 : IPuzzleSolver private bool TryCombineRanges(Range first, Range second, out Range combined) { - if (IsIdInRange(second.from, first) && !IsIdInRange(second.to, first)) + if (IsIdInRange(second.from, first)) { - combined = (first.from, second.to); + var isEndInRange = IsIdInRange(second.to, first); + combined = isEndInRange ? first : (first.from, second.to); return true; } + // if (IsIdInRange(first.from, second)) + // { + // var isEndInRange = IsIdInRange(first.to, second); + // combined = isEndInRange ? second : (second.from, first.to); + // return true; + // } combined = first; return false; diff --git a/AoC_2025/Day06.cs b/AoC_2025/Day06.cs new file mode 100644 index 0000000..df9d385 --- /dev/null +++ b/AoC_2025/Day06.cs @@ -0,0 +1,76 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using AdvenOfCode.Contracts; +using Coordinate = (int x, int y); + +namespace AoC_2025; + +public class Day06 : IPuzzleSolver +{ + private (string[] operands, string[] numbers) ParsePuzzleInput(string path) + { + var puzzleInput = File.ReadAllLines(path) + .Where(line => !string.IsNullOrWhiteSpace(line)) + .ToArray(); + var operands = puzzleInput[^1] + .Split(' ', StringSplitOptions.RemoveEmptyEntries); + var numbers = puzzleInput[..^1]; + return (operands, numbers); + } + + public long SolvePart1(string pathToPuzzleInput) + { + var calculations = ParsePuzzleInput(pathToPuzzleInput); + var numbers = calculations.numbers + .Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries)) + .Select(line => line.Index().ToArray()) + .SelectMany(tuples => tuples.Select(tuple => (tuple.Index, long.Parse(tuple.Item))).ToArray()) + .ToArray(); + var pivotedNumbers = numbers.GroupBy(tuple => tuple.Index); + var indexedOperands = calculations.operands + .Index() + .Select(tuple => (tuple.Index, ParseOperand(tuple.Item))) + .ToDictionary(); + var results = pivotedNumbers.Select(group => + group + .Select(tuple => tuple.Item2) + .Aggregate(indexedOperands[group.Key].opBaseValue, + (acc, number) => indexedOperands[group.Key].operation(acc, number))); + return results.Sum(); + } + + public long SolvePart2(string pathToPuzzleInput) + { + var calculations = ParsePuzzleInput(pathToPuzzleInput); + var operands = calculations.operands.Select(ParseOperand).Reverse().ToArray(); + List> numberColumns = []; + List currentColumn = []; + for (var i = 1; i <= calculations.numbers[0].Length; i++) + { + var current = calculations.numbers.Aggregate("", (current1, number) => current1 + number[^i]); + + if (!string.IsNullOrWhiteSpace(current)) + { + currentColumn.Add(long.Parse(current)); + } + else + { + numberColumns.Add(currentColumn); + currentColumn = []; + } + } + numberColumns.Add(currentColumn); + + return numberColumns + .Select((column, colIndex) => column.Aggregate(operands[colIndex].opBaseValue, (acc, next) => operands[colIndex].operation(acc, next))) + .Sum(); + } + + private (Func operation, long opBaseValue) ParseOperand(string operand) => operand switch + { + "+" => ((long a, long b) => a + b, 0), + "*" => ((long a, long b) => a * b, 1), + _ => throw new ArgumentOutOfRangeException(nameof(operand)) + }; +} \ No newline at end of file