-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01826eb
commit 7cf7597
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |