day 12 part 1

This commit is contained in:
2023-12-22 12:23:41 -05:00
parent c84a4e8ffe
commit 8cb4a71a51
2 changed files with 24 additions and 16 deletions

View File

@@ -45,6 +45,7 @@
<None Remove="problems\aoc2023\day10\sample.txt" /> <None Remove="problems\aoc2023\day10\sample.txt" />
<None Remove="problems\aoc2023\day11\input.txt" /> <None Remove="problems\aoc2023\day11\input.txt" />
<None Remove="problems\aoc2023\day11\sample.txt" /> <None Remove="problems\aoc2023\day11\sample.txt" />
<None Remove="problems\aoc2023\day12\input.txt" />
<None Remove="problems\aoc2023\day12\sample.txt" /> <None Remove="problems\aoc2023\day12\sample.txt" />
<None Remove="problems\aoc2023\day1\input.txt" /> <None Remove="problems\aoc2023\day1\input.txt" />
<None Remove="problems\aoc2023\day2\input.txt" /> <None Remove="problems\aoc2023\day2\input.txt" />

View File

@@ -15,21 +15,22 @@ internal class HotSprings : Problem<int, int>
public override void CalculatePart1() public override void CalculatePart1()
{ {
Console.WriteLine(); Part1 = _data.Select(r => CountPossiblilites(r)).Sum();
Console.WriteLine(Record.CheckValidity(new Record(".###...##...", [3,2,1])));
var a = CountPossiblilites(_data[5]);
} }
public override void CalculatePart2() public override void CalculatePart2()
{ {
throw new NotImplementedException(); var unfolded = _data.Select(r =>
{
var unfoldData = string.Join("", Enumerable.Repeat(r.Data, 5));
var unfoldPattern = Enumerable.Repeat(r.Pattern, 5).SelectMany(x => x).ToArray();
return new Record(unfoldData, unfoldPattern);
}).ToArray();
Part2 = unfolded.Select(r => CountPossiblilites(r)).Sum();
} }
public int CountPossiblilites(Record record, int pos = 0) public static int CountPossiblilites(Record record, int pos = 0)
{ {
if(!record.Data.Contains('?'))
Console.WriteLine(record);
if (pos == -1 || pos >= record.Data.Length) if (pos == -1 || pos >= record.Data.Length)
return record.IsValid ? 1 : 0; return record.IsValid ? 1 : 0;
if (!record.IsValid) if (!record.IsValid)
@@ -44,18 +45,15 @@ internal class HotSprings : Problem<int, int>
return CountPossiblilites(r1, pos + 1) + CountPossiblilites(r2, pos + 1); return CountPossiblilites(r1, pos + 1) + CountPossiblilites(r2, pos + 1);
} }
public override void LoadInput() public override void LoadInput()
{ {
var data = ReadInputLines("sample.txt"); var data = ReadInputLines();
_data = data.Select(x => x.Split(' ')) _data = data.Select(x => x.Split(' '))
.Select(x => new Record(x[0], x[1].Split(',').Select(int.Parse).ToArray())) .Select(x => new Record(x[0], x[1].Split(',').Select(int.Parse).ToArray()))
.ToArray(); .ToArray();
} }
public record Record(string Data, int[] Pattern) public record Record(string Data, int[] Pattern)
{ {
public bool IsValid => CheckValidity(this); public bool IsValid => CheckValidity(this);
@@ -67,7 +65,10 @@ internal class HotSprings : Problem<int, int>
for (int i = 0; i < record.Data.Length; i++) for (int i = 0; i < record.Data.Length; i++)
{ {
var c = record.Data[i]; var c = record.Data[i];
var len = section >= record.Pattern.Length - 1 ? -1 : record.Pattern[section]; if (section >= record.Pattern.Length)
return !record.Data[i..].Contains('#');
var len = record.Pattern[section];
switch (c) switch (c)
{ {
@@ -75,6 +76,7 @@ internal class HotSprings : Problem<int, int>
if (inSection && i - start > len) if (inSection && i - start > len)
return false; return false;
return true; return true;
case '.': case '.':
if (inSection) if (inSection)
{ {
@@ -94,9 +96,14 @@ internal class HotSprings : Problem<int, int>
break; break;
} }
} }
if (section != record.Pattern.Length) if (inSection)
{
if (record.Pattern[section] != (record.Data[start..].Length))
return false; return false;
if (inSection && record.Pattern[section] < (record.Data.Length - 1 - start)) else
section++;
}
if (section != record.Pattern.Length)
return false; return false;
return true; return true;
} }