Partial day 11

This commit is contained in:
Rob Stoffelen 2024-12-16 08:48:15 +01:00
parent dd7cc6f542
commit a78c95af67
5 changed files with 99 additions and 12 deletions

View File

@ -4,13 +4,12 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using AdventOfCode.Core.Shared.IO;
IChallange challange = Host.CreateDefaultBuilder()
.ConfigureServices(ConfigureServices)
.Build()
.Services
.GetService<SolutionManager>()
.GetChallange(2024, 6);
.GetChallange(2024, 11);
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");

View File

@ -0,0 +1,93 @@
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)}");
}
}
}

View File

@ -0,0 +1 @@
6 11 33023 4134 564 0 8922422 688775

View File

@ -34,6 +34,9 @@
<Compile Update="2023\Day 13\Day13.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2024\Day 11\Day11.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2024\Day 06\Day06.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>

View File

@ -1,10 +1 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
125 17