Skip to content

Commit

Permalink
Parameterize the amount of non used parts of the previous solution
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Dec 18, 2023
1 parent f75db6f commit 793aec3
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/AoC_2023/Day_12.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using MoreLinq;
using MoreLinq.Extensions;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Text;

namespace AoC_2023;

Expand Down Expand Up @@ -38,6 +30,7 @@ public override ValueTask<string> Solve_1()

public override ValueTask<string> Solve_2()
{
//return new("");
List<List<string>> previousSolution = new(_input.Count);
foreach (var _ in _input)
{
Expand Down Expand Up @@ -430,11 +423,11 @@ private static List<List<string>> SolveBackwards_Part2(List<Row> input, List<Lis
isPossible = false;

// Old approach
var unknownPositionsBefore = row.Records.Where((ch, index) => index > finalRecordIndex && ch == '?').Count();
var unknownPositionsAfter = row.Records.Where((ch, index) => index > finalRecordIndex && ch == '?').Count();

if (unknownPositionsBefore < possibility.Length - 1)
if (unknownPositionsAfter < possibility.Length - 1)
{
var discardedPossibility = possibility[^(unknownPositionsBefore + 2)..];
var discardedPossibility = possibility[^(unknownPositionsAfter + 2)..];
//possibilities.RemoveAll(x => x.EndsWith(discardedPossibility));
possibilityIndex = -1;
}
Expand All @@ -448,11 +441,11 @@ private static List<List<string>> SolveBackwards_Part2(List<Row> input, List<Lis
{
isPossible = false;

var unknownPositionsBefore = row.Records.Where((ch, index) => index > finalRecordIndex && ch == '?').Count();
var unknownPositionsAfter = row.Records.Where((ch, index) => index > finalRecordIndex && ch == '?').Count();

if (unknownPositionsBefore < possibility.Length - 1)
if (unknownPositionsAfter < possibility.Length - 1)
{
var discardedPossibility = possibility[^(unknownPositionsBefore + 2)..];
var discardedPossibility = possibility[^(unknownPositionsAfter + 2)..];
possibilities.RemoveAll(x => x.EndsWith(discardedPossibility));
possibilityIndex = -1;
}
Expand Down Expand Up @@ -564,7 +557,8 @@ private static List<string> ExtractPossibilities_Part2(Row row, int unknownPosit
var possibilities = new List<string>(row.Records.Length * row.Records.Length);
//var possibilities = new ConcurrentBag<string>();

int maskLength = unknownPositionsCount - previousSolutions[0].Length;
const int previousSolutionLengthNotToUse = 1;
int maskLength = unknownPositionsCount - previousSolutions[0].Length + previousSolutionLengthNotToUse;
var maskSb = new StringBuilder(maskLength);
for (int i = 0; i < maskLength; ++i)
{
Expand All @@ -575,9 +569,10 @@ private static List<string> ExtractPossibilities_Part2(Row row, int unknownPosit

var maxCombinationsCountString = Convert.ToString(mask, 2);

foreach (var previousSolution in previousSolutions)
foreach (string previousSolution in previousSolutions)
{
var expectedOnesCount = row.Conditions.Sum() - row.Records.Count(ch => ch == '#') - previousSolution.Count(ch => ch == '1');
var reusedPreviousSolution = previousSolution?.Length == 0 ? "" : previousSolution[..^(previousSolutionLengthNotToUse)];

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-individually (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-individually (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / run

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / run

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-detailed (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-detailed (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-detailed (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-detailed (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-detailed (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-detailed (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-individually (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-individually (windows-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-individually (macOS-latest)

Dereference of a possibly null reference.

Check warning on line 574 in src/AoC_2023/Day_12.cs

View workflow job for this annotation

GitHub Actions / benchmark-individually (macOS-latest)

Dereference of a possibly null reference.
var expectedOnesCount = row.Conditions.Sum() - row.Records.Count(ch => ch == '#') - reusedPreviousSolution.Count(ch => ch == '1');

//Parallel.For(0, mask, n =>
for (long n = 0; n <= mask; n++)
Expand All @@ -589,7 +584,7 @@ private static List<string> ExtractPossibilities_Part2(Row row, int unknownPosit
//return;
}

var sb = new StringBuilder(previousSolution) { Capacity = maxCombinationsCountString.Length + previousSolution.Length };
var sb = new StringBuilder(reusedPreviousSolution) { Capacity = maxCombinationsCountString.Length + reusedPreviousSolution.Length };
for (int j = binaryString.Length; j < maxCombinationsCountString.Length; ++j)
{
sb.Append('0');
Expand Down

0 comments on commit 793aec3

Please sign in to comment.