Not sure how I feel about this one. I don't mind a "day off" of sorts, but there were just so. many. words in the puzzle that boiled down to such a straightforward implementation. I guess this is practice of "implement detailed requirements exactly to spec" or something.
This commit is contained in:
2023-12-15 22:43:36 -06:00
parent 42e1a27806
commit adc2c2b658
4 changed files with 95 additions and 0 deletions

View File

@ -60,6 +60,8 @@
<EmbeddedResource Include="inputs\13a.txt" />
<EmbeddedResource Include="inputs\14.txt" />
<EmbeddedResource Include="inputs\14a.txt" />
<EmbeddedResource Include="inputs\15.txt" />
<EmbeddedResource Include="inputs\15a.txt" />
</ItemGroup>
<ItemGroup>

1
inputs/15.txt Normal file

File diff suppressed because one or more lines are too long

1
inputs/15a.txt Normal file
View File

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

91
src/15.cs Normal file
View File

@ -0,0 +1,91 @@
namespace aoc2023;
internal class Day15 : Day
{
class lens
{
public string label = string.Empty;
public int length;
public override string ToString() => $"[{label} {length}]";
}
private List<string> sequence = [];
internal override void Parse()
{
var str = Util.Parsing.ReadAllText($"{GetDay()}");
sequence = [.. str.Split(',')];
}
private byte GetSequenceHash(string seq)
{
int val = 0;
foreach (var ch in seq)
{
val += ch;
val *= 17;
val %= 256;
}
return (byte)val;
}
internal override string Part1()
{
long total = sequence.Sum(s => GetSequenceHash(s));
return $"Sum of each sequence's hash: <+white>{total}";
}
internal override string Part2()
{
List<lens>[] boxes = new List<lens>[256];
for (int i = 0; i < 256; i++)
{
boxes[i] = [];
}
foreach (var seq in sequence)
{
var opIdx = seq.IndexOfAny(['-', '=']);
var label = seq[..opIdx];
int box = GetSequenceHash(label);
var op = seq[opIdx];
if (op == '=')
{
int len = int.Parse(seq[(opIdx + 1)..]);
int idx = boxes[box].FindIndex(l => l.label == label);
if (idx >= 0)
{
boxes[box][idx].length = len;
}
else
{
boxes[box].Add(new lens(){label = label, length = len});
}
continue;
}
boxes[box].RemoveAll(l => l.label == label);
}
long total = 0;
for (int i = 0; i < 256; i++)
{
if (!boxes[i].Any())
{
continue;
}
for (int j = 0; j < boxes[i].Count; j++)
{
long boxval = i + 1;
boxval *= (j + 1);
boxval *= boxes[i][j].length;
total += boxval;
}
}
return $"Focusing power of final configuration: <+white>{total}";
}
}