Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 13, 2023
1 parent 789bdff commit 0dcba94
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions AdventOfCode/Day12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override ValueTask<string> Solve_1()
{
string[] parts = line.Split(' ');
char[] spring = parts[0].ToArray();
List<int> groups = parts[1].Split(',').Select(x => int.Parse(x)).ToList();
int[] groups = parts[1].Split(',').Select(x => int.Parse(x)).ToArray();

Preprocess(spring, groups);
sum += GetPossibleArrangements(spring, groups);
Expand All @@ -32,8 +32,8 @@ public override ValueTask<string> Solve_2()
{
string[] parts = line.Split(' ');
char[] spring = string.Join("?", Enumerable.Repeat(parts[0], 5)).ToArray();
List<int> groups = parts[1].Split(',').Select(x => int.Parse(x)).ToList();
groups = Enumerable.Repeat(groups, 5).SelectMany(x => x).ToList();
int[] groups = parts[1].Split(',').Select(x => int.Parse(x)).ToArray();
groups = Enumerable.Repeat(groups, 5).SelectMany(x => x).ToArray();

Preprocess(spring, groups);
sum += GetPossibleArrangements(spring, groups);
Expand All @@ -42,9 +42,9 @@ public override ValueTask<string> Solve_2()
return new(sum.ToString());
}

private void Preprocess(char[] spring, List<int> groups)
private void Preprocess(char[] spring, int[] groups)
{
int groupLength = groups.Sum() + groups.Count - 1;
int groupLength = groups.Sum() + groups.Length - 1;
int springLength = spring.Length;

int index = 0;
Expand Down Expand Up @@ -73,7 +73,7 @@ private void Preprocess(char[] spring, List<int> groups)
}
}

private int GetPossibleArrangements(char[] spring, List<int> groups, int index = 0, int curGroupIndex = 0)
private int GetPossibleArrangements(char[] spring, int[] groups, int index = 0, int curGroupIndex = 0)
{
if (index == spring.Length)
{
Expand Down Expand Up @@ -119,14 +119,14 @@ private int GetPossibleArrangements(char[] spring, List<int> groups, int index =
return count;
}

private (bool isValid, int curGroupIndex) IsValid(char[] spring, List<int> groups, int index, int curGroupIndex)
private (bool isValid, int curGroupIndex) IsValid(char[] spring, int[] groups, int index, int curGroupIndex)
{
if (index == spring.Length)
{
return (curGroupIndex == groups.Count, curGroupIndex);
return (curGroupIndex == groups.Length, curGroupIndex);
}

int cur = curGroupIndex == groups.Count ? 0 : groups[curGroupIndex];
int cur = curGroupIndex == groups.Length ? 0 : groups[curGroupIndex];

if (spring[index] == '.')
{
Expand Down

0 comments on commit 0dcba94

Please sign in to comment.