From 8cb4a71a516284b1ebaf686cdf9e4712fcd799af Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Fri, 22 Dec 2023 12:23:41 -0500 Subject: [PATCH] day 12 part 1 --- AdventOfCode/AdventOfCode.csproj | 1 + .../Problems/AOC2023/Day12/HotSprings.cs | 39 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/AdventOfCode/AdventOfCode.csproj b/AdventOfCode/AdventOfCode.csproj index b34d532..a96ed50 100644 --- a/AdventOfCode/AdventOfCode.csproj +++ b/AdventOfCode/AdventOfCode.csproj @@ -45,6 +45,7 @@ + diff --git a/AdventOfCode/Problems/AOC2023/Day12/HotSprings.cs b/AdventOfCode/Problems/AOC2023/Day12/HotSprings.cs index ebaf0e7..3e3faf7 100644 --- a/AdventOfCode/Problems/AOC2023/Day12/HotSprings.cs +++ b/AdventOfCode/Problems/AOC2023/Day12/HotSprings.cs @@ -15,21 +15,22 @@ internal class HotSprings : Problem public override void CalculatePart1() { - Console.WriteLine(); - Console.WriteLine(Record.CheckValidity(new Record(".###...##...", [3,2,1]))); - - var a = CountPossiblilites(_data[5]); + Part1 = _data.Select(r => CountPossiblilites(r)).Sum(); } 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) return record.IsValid ? 1 : 0; if (!record.IsValid) @@ -44,18 +45,15 @@ internal class HotSprings : Problem return CountPossiblilites(r1, pos + 1) + CountPossiblilites(r2, pos + 1); } - - public override void LoadInput() { - var data = ReadInputLines("sample.txt"); + var data = ReadInputLines(); _data = data.Select(x => x.Split(' ')) .Select(x => new Record(x[0], x[1].Split(',').Select(int.Parse).ToArray())) .ToArray(); } - public record Record(string Data, int[] Pattern) { public bool IsValid => CheckValidity(this); @@ -67,7 +65,10 @@ internal class HotSprings : Problem for (int i = 0; i < record.Data.Length; 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) { @@ -75,6 +76,7 @@ internal class HotSprings : Problem if (inSection && i - start > len) return false; return true; + case '.': if (inSection) { @@ -94,11 +96,16 @@ internal class HotSprings : Problem break; } } + if (inSection) + { + if (record.Pattern[section] != (record.Data[start..].Length)) + return false; + else + section++; + } if (section != record.Pattern.Length) return false; - if (inSection && record.Pattern[section] < (record.Data.Length - 1 - start)) - return false; return true; } }; -} +} \ No newline at end of file