Completed day 15
This commit is contained in:
parent
c3eb1c24a5
commit
50810d6e06
@ -4,12 +4,12 @@ using AdventOfCode.Core;
|
||||
|
||||
InputReader inputReader = new()
|
||||
{
|
||||
IsDebug = true
|
||||
//IsDebug = true
|
||||
};
|
||||
|
||||
//inputReader.SetInputByChallange(3);
|
||||
|
||||
IChallange challange = new Day14(inputReader);
|
||||
IChallange challange = new Day15(inputReader);
|
||||
|
||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using AdventOfCode.Core;
|
||||
using System.Text;
|
||||
|
||||
namespace AdventOfCode.Solutions._2023
|
||||
{
|
||||
@ -8,12 +9,91 @@ namespace AdventOfCode.Solutions._2023
|
||||
|
||||
public async Task<string> GetSolutionPart1()
|
||||
{
|
||||
return string.Empty;
|
||||
string[] data = (await _inputReader.ReadAsString()).Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
int stringTotal = 0;
|
||||
foreach (string s in data)
|
||||
{
|
||||
stringTotal += GetHash(s);
|
||||
}
|
||||
return stringTotal.ToString();
|
||||
}
|
||||
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
return string.Empty;
|
||||
string[] data = (await _inputReader.ReadAsString()).Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
Dictionary<int, List<(string Label, int FocusLength)>> boxes = [];
|
||||
int stringTotal = 0;
|
||||
foreach (string s in data)
|
||||
{
|
||||
if (s.EndsWith('-'))
|
||||
{
|
||||
string label = s[..^1];
|
||||
// remove
|
||||
int box = GetHash(label);
|
||||
if (boxes.TryGetValue(box, out var lensBox))
|
||||
{
|
||||
var currentLens = lensBox.FirstOrDefault(l => l.Label == label);
|
||||
if (!string.IsNullOrEmpty(currentLens.Label))
|
||||
{
|
||||
lensBox.Remove(currentLens);
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char.IsDigit(s[^1]))
|
||||
{
|
||||
string label = s[..^2];
|
||||
int box = GetHash(label);
|
||||
int newFocal = int.Parse($"{s[^1]}");
|
||||
if (boxes.TryGetValue(box, out var lensBox))
|
||||
{
|
||||
var newLens = (label, newFocal);
|
||||
var currentLens = lensBox.FirstOrDefault(l => l.Label == label);
|
||||
if (!string.IsNullOrEmpty(currentLens.Label))
|
||||
{
|
||||
int index = lensBox.IndexOf(currentLens);
|
||||
lensBox[index] = newLens;
|
||||
}
|
||||
else
|
||||
{
|
||||
lensBox.Add(newLens);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
boxes.Add(box, [(label, newFocal)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
|
||||
//return boxes.Select(kvp => kvp.Key + 1 * GetBoxValue(kvp.Value)).Sum().ToString();
|
||||
|
||||
foreach(var box in boxes)
|
||||
{
|
||||
total += GetBoxValue(box.Key + 1, box.Value);
|
||||
}
|
||||
|
||||
return total.ToString();
|
||||
}
|
||||
|
||||
private static int GetBoxValue(int boxId, List<(string Label, int FocusLength)> box) => box.Select((kvp, index) => boxId * (index + 1) * kvp.FocusLength).Sum();
|
||||
|
||||
private static int GetBoxValue(Dictionary<string, int> box) => box.Select((kvp, index) => (index + 1) * kvp.Value).Sum();
|
||||
|
||||
private static int GetHash(string s)
|
||||
{
|
||||
int segementTotal = 0;
|
||||
foreach (byte value in Encoding.ASCII.GetBytes(s))
|
||||
{
|
||||
segementTotal += value;
|
||||
segementTotal *= 17;
|
||||
segementTotal %= 256;
|
||||
}
|
||||
return segementTotal;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user