Skip to content

Commit

Permalink
refactor Day 11 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 11, 2024
1 parent 09a1a93 commit 8cb6839
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions AdventOfCode/Day11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,51 @@ public override ValueTask<string> Solve_1()

for (int i = 0; i < 25; ++i)
{
Blink(stones);
stones = Blink(stones);
}

return new(stones.Count.ToString());
}

public override ValueTask<string> Solve_2()
{
return new("");
List<Stone> stones = GetInput();

for (int i = 0; i < 35; ++i) // TODO make work for 75
{
stones = Blink(stones);
}

return new(stones.Count.ToString());
}

private void Blink(List<Stone> stones)
private List<Stone> Blink(List<Stone> stones)
{
List<Stone> result = new List<Stone>();
for (int i = 0; i < stones.Count; i++)
{
if (stones[i].Value == 0)
{
stones[i].Value++;
}
else if (stones[i].Value.ToString().Length % 2 == 0)
{
string stoneNumberString = stones[i].Value.ToString();
long one = long.Parse(stoneNumberString.Substring(0, stoneNumberString.Length / 2));
long two = long.Parse(stoneNumberString.Substring(stoneNumberString.Length / 2));

stones[i].Value = one;
stones.Insert(i + 1, new Stone(two));
i++;
}
else
{
stones[i].Value *= 2024;
}
result.AddRange(Blink(stones[i]));
}
return result;
}

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

return new List<Stone> { new Stone(one), new Stone(two) };
}
else
{
return new List<Stone> { new Stone(stone.Value * 2024) };
}
}

Expand Down

0 comments on commit 8cb6839

Please sign in to comment.