Skip to content

Commit

Permalink
2023 Day15 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 15, 2023
1 parent 926ece8 commit 97aa7f2
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions Solutions/2023/Day15.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,52 @@ private static int Solution1(string[] input) {
return input
.Single()
.TrimmedSplit(",")
.As<Step>()
.Sum(step => Step.HashNumber(step.Name));
.Sum(HashNumber);
}

private static int Solution2(string[] input) {
List<Step> initializationSequence = [..
input
.Single()
.TrimmedSplit(",")
.As<Step>()];

Dictionary<int ,Box> boxes = Enumerable.Range(0, 256)
Dictionary<int, Box> boxes
= Enumerable.Range(0, 256)
.Select(i => new Box(i))
.ToDictionary(b => b.Number, b => b);

foreach (Step step in initializationSequence) {
Lens lens = step.Lens;
int boxNo = Step.HashNumber(lens.Label);
boxes[boxNo].ProcessLens(step.Operation, lens);
}
input
.Single()
.TrimmedSplit(",")
.As<Step>()
.ToList()
.ForEach(step => boxes[HashNumber(step.Lens.Label)].ProcessStep(step));

return boxes.Sum(box => box.Value.FocusingPower);
}



private sealed record Step(string Name) : IParsable<Step>
public static int HashNumber(string s)
{
private const char INSERT = '=';
int result = 0;
foreach (char c in s) {
result += c;
result = result * 17 % 256;
}

public readonly Operation Operation = Name.Contains(INSERT) ? Operation.Insert : Operation.Remove;
public readonly Lens Lens = Name.As<Lens>();
return result;
}

public static int HashNumber(string s)
{
int result = 0;
foreach (char c in s) {
result += c;
result *= 17;
result %= 256;
}
return result;
}

public static Step Parse(string s, IFormatProvider? provider) => new(s);
private record Step(string Instruction) : IParsable<Step>
{
const char INSERT = '=';

public readonly Lens Lens = Instruction.As<Lens>();

public static Step Parse(string s, IFormatProvider? provider) => s.Contains(INSERT) ? new InsertStep(s) : new RemoveStep(s) ;
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out Step result)
=> ISimpleParsable<Step>.TryParse(s, provider, out result);
}

private record RemoveStep(string Label) : Step(Label);
private record InsertStep(string Label) : Step(Label);



private sealed record Box(int Number) {
Expand All @@ -72,12 +69,12 @@ private sealed record Box(int Number) {

public int FocusingPower => lensSlot.Select((lens, index) => (Number + 1) * (index + 1) * lens.FocalLength).Sum();

public void ProcessLens(Operation operation, Lens lens)
public void ProcessStep(Step step)
{
if (operation == Operation.Remove) {
Remove(lens);
if (step is RemoveStep) {
Remove(step.Lens);
} else {
Insert(lens);
Insert(step.Lens);
}
}

Expand Down Expand Up @@ -110,20 +107,11 @@ public static Lens Parse(string s, IFormatProvider? provider)
char[] splitBy = [INSERT, REMOVE];

string[] split = s.TrimmedSplit(splitBy);
return s.Contains(INSERT)
? new(split[0], split[1].As<int>())
: new(split[0], 0);

return s.Contains(INSERT) ? new Lens(split[0], split[1].As<int>()) : new Lens(split[0], 0);
}

public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out Lens result)
=> ISimpleParsable<Lens>.TryParse(s, provider, out result);
};



public enum Operation
{
Remove,
Insert,
}
}

0 comments on commit 97aa7f2

Please sign in to comment.