Skip to content

Commit

Permalink
day 11: add recursive attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 12, 2024
1 parent 9de29f2 commit f2e0646
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions AdventOfCode/Day11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,72 @@ private List<long> Blink(List<long> stones)
List<long> result = new List<long>();
for (int i = 0; i < stones.Count; i++)
{
result.AddRange(Blink(stones[i]));
(long a, long? b) = Blink(stones[i]);
result.Add(a);
if (b.HasValue)
{
result.Add(b.Value);
}
}
return result;
}

private List<long> Blink(long stone)
private (long a, long? b) Blink(long stone)
{
if (stone == 0)
{
return new List<long> { 1L };
return (1L, null);
}
else if (stone.ToString().Length % 2 == 0)
{
string stoneNumberString = stone.ToString();
long one = long.Parse(stoneNumberString.Substring(0, stoneNumberString.Length / 2));
long two = long.Parse(stoneNumberString.Substring(stoneNumberString.Length / 2));

return new List<long> { one, two };
return (one, two);
}
else
{
return new List<long> { stone * 2024L };
return (stone * 2024L, null);
}
}

private List<long> GetInput()
{
{
return File.ReadAllText(InputFilePath)
.Split(" ", StringSplitOptions.RemoveEmptyEntries)
.Select(long.Parse)
.ToList();
}
}

#region recursive

private long BlinkRecursive(List<long> stones, int times)
{
long result = 0;
for (int i = 0; i < stones.Count; i++)
{
result += BlinkRecursive(stones[i], times);
}
return result;
}

private long BlinkRecursive(long stone, int times)
{
(long a, long? b) = Blink(stone);

if (times == 1)
{
return b.HasValue ? 2 : 1;
}

long result = BlinkRecursive(a, times - 1);
if (b.HasValue)
{
result += BlinkRecursive(b.Value, times - 1);
}
return result;
}

#endregion
}

0 comments on commit f2e0646

Please sign in to comment.