Skip to content

Commit

Permalink
day(19): Linen Layout 🧦
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 19, 2024
1 parent 01826eb commit 7cf7597
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/AdventOfCode/Day19.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace AdventOfCode
{
/// <summary>
/// Solver for Day 19
/// </summary>
public class Day19
{
public int Part1(string[] input)
{
string[] allowed = input[0].Split(", ");
var cache = new Dictionary<string, bool>();
return input.Skip(2).Count(i => Possible(i, allowed, cache));
}

public long Part2(string[] input)
{
string[] allowed = input[0].Split(", ");
var cache = new Dictionary<string, long>();
return input.Skip(2).Sum(i => Possible2(i, allowed, cache));
}

private static bool Possible(string check, string[] allowed, Dictionary<string, bool> cache)
{
if (check.Length == 0)
{
return true;
}

if (cache.TryGetValue(check, out bool result)) return result;

result = allowed.Where(a => a.Length <= check.Length)
.Where(check.StartsWith)
.Any(a => Possible(check[a.Length..], allowed, cache));

cache[check] = result;
return result;
}

private static long Possible2(string check, string[] allowed, Dictionary<string, long> cache)
{
if (check.Length == 0)
{
return 1;
}

if (cache.TryGetValue(check, out long result)) return result;

result = allowed.Where(a => a.Length <= check.Length)
.Where(check.StartsWith)
.Sum(a => Possible2(check[a.Length..], allowed, cache));

cache[check] = result;
return result;
}
}
}
Binary file added src/AdventOfCode/inputs/day19.txt
Binary file not shown.
84 changes: 84 additions & 0 deletions tests/AdventOfCode.Tests/Day19Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.IO;
using Xunit;
using Xunit.Abstractions;


namespace AdventOfCode.Tests
{
public class Day19Tests
{
private readonly ITestOutputHelper output;
private readonly Day19 solver;

public Day19Tests(ITestOutputHelper output)
{
this.output = output;
this.solver = new Day19();
}

private static string[] GetRealInput()
{
string[] input = File.ReadAllLines("inputs/day19.txt");
return input;
}

private static string[] GetSampleInput()
{
return new string[]
{
"r, wr, b, g, bwu, rb, gb, br",
"",
"brwrr",
"bggr",
"gbbr",
"rrbgbr",
"ubwu",
"bwurrg",
"brgr",
"bbrgwb",
};
}

[Fact]
public void Part1_SampleInput_ProducesCorrectResponse()
{
var expected = 6;

var result = solver.Part1(GetSampleInput());

Assert.Equal(expected, result);
}

[Fact]
public void Part1_RealInput_ProducesCorrectResponse()
{
var expected = 226;

var result = solver.Part1(GetRealInput());
output.WriteLine($"Day 19 - Part 1 - {result}");

Assert.Equal(expected, result);
}

[Fact]
public void Part2_SampleInput_ProducesCorrectResponse()
{
var expected = 16;

var result = solver.Part2(GetSampleInput());

Assert.Equal(expected, result);
}

[Fact]
public void Part2_RealInput_ProducesCorrectResponse()
{
var expected = 601201576113503;

var result = solver.Part2(GetRealInput());
output.WriteLine($"Day 19 - Part 2 - {result}");

Assert.Equal(expected, result);
}
}
}

0 comments on commit 7cf7597

Please sign in to comment.