Skip to content

Commit

Permalink
2023 Day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
premun committed Dec 15, 2023
1 parent a4d83e4 commit ed8d49a
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/2023/15/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
using AdventOfCode.Common;

var lines = Resources.GetInputFileLines();
var instructions = Resources.GetInputFileLines()
.Single()
.SplitBy(",");

var boxes = Enumerable.Range(0, 256)
.Select(_ => new LinkedList<Lens>())
.ToArray();

foreach (var instruction in instructions)
{
var index1 = instruction.IndexOf('=');
var index2 = instruction.IndexOf('-');
var label = instruction[0..Math.Max(index1, index2)];
var box = boxes[Hash(label)];
var lens = box.FirstOrDefault(lens => lens.Label == label);

if (index1 != -1) // =
{
var length = int.Parse(instruction.Substring(index1 + 1));
if (lens != null)
{
lens.FocalLength = length;
}
else
{
box.AddLast(new Lens(label, length));
}
}
else if (lens != null) // -
{
box.Remove(lens);
}
}

var part1 = instructions
.Select(Hash)
.Sum();

var part2 = boxes
.SelectMany((box, boxId) =>
box.Select((lens, lensId) => (1 + boxId) * (lensId + 1) * lens.FocalLength))
.Sum();

Console.WriteLine($"Part 1: {part1}");
Console.WriteLine($"Part 2: {part2}");

static int Hash(string input)
=> input.Aggregate(0, (acc, c) => acc = (acc + c) * 17 % 256);

file class Lens(string label, int focalLength)
{
public string Label { get; } = label;
public int FocalLength { get; set; } = focalLength;
}

0 comments on commit ed8d49a

Please sign in to comment.