From 6e7fd2e08ca393b7a691d6e13fd9413c8875611c Mon Sep 17 00:00:00 2001 From: KaterynaKateryna Date: Thu, 19 Dec 2024 18:23:40 +0100 Subject: [PATCH] day 19 WIP --- AdventOfCode/Day19.cs | 50 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/AdventOfCode/Day19.cs b/AdventOfCode/Day19.cs index 4049488..8146023 100644 --- a/AdventOfCode/Day19.cs +++ b/AdventOfCode/Day19.cs @@ -5,6 +5,8 @@ public class Day19 : BaseDay string[] _towels; string[] _patterns; + Dictionary> _towelsByFirstLetter = new Dictionary>(); + public Day19() { string[] lines = File.ReadAllLines(InputFilePath); @@ -15,6 +17,14 @@ public Day19() { _patterns[i-2] = lines[i].Trim(); } + + foreach (string towel in _towels) + { + if (!_towelsByFirstLetter.TryAdd(towel[0], new List { towel })) + { + _towelsByFirstLetter[towel[0]].Add(towel); + } + } } public override ValueTask Solve_1() @@ -26,7 +36,11 @@ public override ValueTask Solve_1() public override ValueTask Solve_2() { - return new("test"); + int result = _patterns.Sum(p => CountPatterns(p, 0)); + + //int result = CountPatterns(_patterns[0], 0); + + return new(result.ToString()); } private bool IsPatternPossible(string pattern, int position) @@ -58,4 +72,38 @@ private bool IsPatternPossible(string pattern, int position) return false; } + + private int CountPatterns(string pattern, int position) + { + if (position == pattern.Length) + { + return 1; + } + + int res = 0; + if (_towelsByFirstLetter.TryGetValue(pattern[position], out var towelsToCheck)) + { + foreach (string towel in towelsToCheck) + { + if (towel.Length <= pattern.Length - position) + { + bool matches = true; + for (int i = 0; i < towel.Length; ++i) + { + if (pattern[position + i] != towel[i]) + { + matches = false; + break; + } + } + if (matches) + { + res += CountPatterns(pattern, position + towel.Length); + } + } + } + } + + return res; + } }