-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |