using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections.Immutable;
public static Dictionary<(string, ImmutableList<int>), long> cache = new Dictionary<(string, ImmutableList<int>), long>();
public static void Main()
string[] lines = File.ReadAllLines("test.txt");
foreach (var line in lines)
var split = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
var symbols = string.Join("?", Enumerable.Repeat(split.First(), 1));
var numbers = string.Join(",", Enumerable.Repeat(split.Last(), 1))
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToImmutableList();
var springInfo = new SpringInformation(symbols, numbers);
total += Count(springInfo.Symbols, springInfo.Numbers);
Console.WriteLine($"Part 1 result: {total}");
public static long Count(string symbols, ImmutableList<int> numbers)
if(string.IsNullOrEmpty(symbols))
return numbers.IsEmpty ? 1 : 0;
return symbols.Contains("#") ? 0 : 1;
var key = (symbols, numbers);
if(cache.ContainsKey(key))
if((".?").Contains(symbols[0]))
result += Count(symbols.Substring(1), numbers);
if(("#?").Contains(symbols[0]))
numbers[0] <= symbols.Length
&& !symbols.Substring(0,numbers[0]).Contains(".")
&& (numbers[0] == symbols.Length || symbols[numbers[0]] != '#'))
if(numbers[0] == symbols.Length)
result += Count(symbols.Substring(numbers[0]), numbers.Skip(1).ToImmutableList());
result += Count(symbols.Substring(numbers[0] + 1), numbers.Skip(1).ToImmutableList());
public class SpringInformation
public SpringInformation(string symbols, ImmutableList<int> numbers)
public string Symbols { get; set; }
public ImmutableList<int> Numbers { get; set; }
public static void WriteFile(string path)
????.######..#####. 1,6,5