day 2 pt 1; day 3
This commit is contained in:
@@ -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<long, long>
|
||||
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<long> 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<long, long>
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
65
AdventOfCode/Problems/AOC2025/Day3/Lobby.cs
Normal file
65
AdventOfCode/Problems/AOC2025/Day3/Lobby.cs
Normal file
@@ -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<long, long>
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,4 @@ global using AdventOfCode.Utils;
|
||||
|
||||
|
||||
var runner = new AOCRunner();
|
||||
runner.RenderInteractiveMenu();
|
||||
runner.WithDay(2).RenderInteractiveMenu();
|
||||
|
||||
@@ -44,4 +44,14 @@ public static class ExtraMath
|
||||
else
|
||||
return remainder;
|
||||
}
|
||||
|
||||
public static bool IsEven<T>(this T value) where T : INumber<T>
|
||||
{
|
||||
return T.IsEvenInteger(value);
|
||||
}
|
||||
|
||||
public static bool IsOdd<T>(this T value) where T : INumber<T>
|
||||
{
|
||||
return T.IsOddInteger(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user