diff --git a/AdventOfCode/Problems/AOC2025/Day2/GiftShop.cs b/AdventOfCode/Problems/AOC2025/Day2/GiftShop.cs index 4c0175b..7589f72 100644 --- a/AdventOfCode/Problems/AOC2025/Day2/GiftShop.cs +++ b/AdventOfCode/Problems/AOC2025/Day2/GiftShop.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Data; using System.Reflection.Metadata; +using System.Security.Cryptography; using System.Text; using ZLinq; @@ -13,14 +15,64 @@ internal class GiftShop : Problem private IdRange[] _ranges = []; public override void CalculatePart1() { - throw new NotImplementedException(); + var v = _ranges.SelectMany(GetDoubleSequences); + //Console.WriteLine(v.AsJoinedString()); + Part1 = v.Sum(); } - public static List GetDoubleSequences(IdRange range) + public static long[] GetDoubleSequences(IdRange range) { + range = range.Snap(); var minDigits = range.Min.DigitCount() / 2; var maxDigits = range.Max.DigitCount() / 2; - throw new NotImplementedException(); + + var min = GetMinValue((int)minDigits, range.Min); + var max = GetMaxValue((int)maxDigits, range.Max); + //Console.WriteLine($"{min}-{max}"); + if (max < min) + return []; + var n = (max - min) + 1; + var result = new long[n]; + for (long i = min; i <= max; i++) + { + result[i - min] = (i * QuickMath.FastPow10(minDigits)) + i; + } + return result; + } + + public static long SnapToUpNearestValidRange(long value) + { + var dc = value.DigitCount(); + if (dc.IsEven()) + return value; + return QuickMath.FastPow10(dc); + } + public static long SnapToDownNearestValidRange(long value) + { + var dc = value.DigitCount(); + if (dc.IsEven()) + return value; + return QuickMath.FastPow10(dc - 1) - 1; + } + + public static long GetMinValue(int digits, long value) + { + var val = long.Parse(value.ToString()[..^digits]); + while ((val * QuickMath.FastPow10(digits)) + val < value) + { + val++; + } + return val; + } + + public static long GetMaxValue(int digits, long value) + { + var val = long.Parse(value.ToString()[..^digits]); + while ((val * QuickMath.FastPow10(digits)) + val > value) + { + val--; + } + return val; } public override void CalculatePart2() @@ -30,13 +82,19 @@ internal class GiftShop : Problem public override void LoadInput() { - var text = ReadInputText("sample.txt"); + var text = ReadInputText("input.txt"); _ranges = text.Split(',') .AsValueEnumerable() - .Select(r => r.Split('-').Select(int.Parse)) + .Select(r => r.Split('-').Select(long.Parse)) .Select(r => new IdRange(r.First(), r.Last())) .ToArray(); } - public record IdRange(int Min, int Max); + public record IdRange(long Min, long Max) + { + public IdRange Snap() + { + return new IdRange(SnapToUpNearestValidRange(Min), SnapToDownNearestValidRange(Max)); + } + } } diff --git a/AdventOfCode/Problems/AOC2025/Day3/Lobby.cs b/AdventOfCode/Problems/AOC2025/Day3/Lobby.cs new file mode 100644 index 0000000..23f2dc8 --- /dev/null +++ b/AdventOfCode/Problems/AOC2025/Day3/Lobby.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using ZLinq; + +namespace AdventOfCode.Problems.AOC2025.Day3; + +[ProblemInfo(2025, 3, "Lobby")] +internal class Lobby : Problem +{ + private (int val, int idx)[][] _batteryBanks = []; + + public override void CalculatePart1() + { + Part1 = _batteryBanks.AsValueEnumerable().Select(bank => + { + var batteries = GetViableBatteries(bank); + return GetPower(batteries); + }).Sum(); + } + + + public override void CalculatePart2() + { + Console.WriteLine(); + var b = _batteryBanks.AsValueEnumerable().Select(bank => + { + var batteries = GetViableBatteries(bank, 12); + return GetPower(batteries); + }); + Part2 = b.Sum(); + } + + public static long GetPower(int[] values) + { + return values.Select((v, idx) => + { + var mag = (long)Math.Pow(10, values.Length - idx - 1); + return v * mag; + }).Sum(); + } + + public static int[] GetViableBatteries((int val, int idx)[] source, int count = 2) + { + var batteries = new int[count]; + var offset = 0; + for (int i = count; i > 0; i--) + { + var tgt = i - 1; + var (val, idx) = source[offset..^tgt].MaxBy(v => v.val); + offset = idx + 1; + batteries[count - i] = val; + } + return batteries; + } + + public override void LoadInput() + { + _batteryBanks = ReadInputLines("input.txt") + .AsValueEnumerable() + .Select(l => l.AsValueEnumerable().Select((v, idx) => (v - '0', idx)).ToArray()) + .ToArray(); + } +} diff --git a/AdventOfCode/Program.cs b/AdventOfCode/Program.cs index ab33153..b6b2c75 100644 --- a/AdventOfCode/Program.cs +++ b/AdventOfCode/Program.cs @@ -4,4 +4,4 @@ global using AdventOfCode.Utils; var runner = new AOCRunner(); -runner.RenderInteractiveMenu(); \ No newline at end of file +runner.WithDay(2).RenderInteractiveMenu(); diff --git a/AdventOfCode/Runner/ExtraMath.cs b/AdventOfCode/Runner/ExtraMath.cs index 4127e1b..ad42f16 100644 --- a/AdventOfCode/Runner/ExtraMath.cs +++ b/AdventOfCode/Runner/ExtraMath.cs @@ -44,4 +44,14 @@ public static class ExtraMath else return remainder; } + + public static bool IsEven(this T value) where T : INumber + { + return T.IsEvenInteger(value); + } + + public static bool IsOdd(this T value) where T : INumber + { + return T.IsOddInteger(value); + } } \ No newline at end of file