93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using AdventOfCode.Core.Shared.IO;
|
|
|
|
namespace AdventOfCode.Solutions._2024
|
|
{
|
|
public class Day11 : IChallange
|
|
{
|
|
public int Year => 2024;
|
|
|
|
public int Day => 11;
|
|
|
|
private readonly IInputReader _inputReader;
|
|
|
|
public Day11(IInputReader inputReader)
|
|
{
|
|
_inputReader = inputReader;
|
|
_inputReader.SetInput(this);
|
|
//_inputReader.SetSampleInput(true);
|
|
}
|
|
|
|
// 55312
|
|
// 220999
|
|
public async Task<string> GetSolutionPart1()
|
|
{
|
|
string input = await _inputReader.ReadAsString();
|
|
List<long> stones = input.Split(' ').Select(long.Parse).ToList();
|
|
//Print(stones);
|
|
|
|
for (int i = 0; i < 25; i++)
|
|
{
|
|
stones = ApplyRules(stones);
|
|
Console.WriteLine($"{i}: {stones.Count}");
|
|
//Print(stones);
|
|
}
|
|
|
|
return stones.Count.ToString();
|
|
}
|
|
|
|
// 6
|
|
//
|
|
public async Task<string> GetSolutionPart2()
|
|
{
|
|
string input = await _inputReader.ReadAsString();
|
|
List<long> stones = input.Split(' ').Select(long.Parse).ToList();
|
|
long total = 0;
|
|
//Print(stones);
|
|
for (int i = 0; i < stones.Count; i++)
|
|
{
|
|
List<long> stone = [stones[i]];
|
|
for (int y = 0; y < 75; y++)
|
|
{
|
|
stone = ApplyRules(stone);
|
|
//Print(stones);
|
|
}
|
|
total += stone.Count;
|
|
}
|
|
|
|
return total.ToString();
|
|
}
|
|
|
|
private List<long> ApplyRules(List<long> stones)
|
|
{
|
|
List<long> stonesResult = [];
|
|
|
|
for (int i = 0; i < stones.Count; i++)
|
|
{
|
|
var stone = stones[i];
|
|
string stringStone = stone.ToString();
|
|
|
|
// check length
|
|
if (stone == 0)
|
|
{
|
|
stonesResult.Add(1);
|
|
}
|
|
else if (stringStone.Length % 2 == 0)
|
|
{
|
|
stonesResult.Add(long.Parse(stringStone[..(stringStone.Length / 2)]));
|
|
stonesResult.Add(long.Parse(stringStone.Substring(stringStone.Length / 2, stringStone.Length / 2)));
|
|
}
|
|
else
|
|
{
|
|
stonesResult.Add(stone * 2024);
|
|
}
|
|
}
|
|
|
|
return stonesResult;
|
|
}
|
|
|
|
private static void Print(List<string> stones)
|
|
{
|
|
Console.WriteLine($"Stones: {string.Join(", ", stones)}");
|
|
}
|
|
}
|
|
} |