Skip to content

Commit

Permalink
day(19): Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 19, 2023
1 parent c570c9b commit fcbe385
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
31 changes: 17 additions & 14 deletions src/AdventOfCode/Day19.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public long Part2(string[] input)
Dictionary<string, PartRule> rules = input.TakeWhile(line => !string.IsNullOrEmpty(line))
.Select(PartRule.Parse)
.ToDictionary(r => r.Id);
rules["A"] = new PartRule("A", Array.Empty<PartPredicate>());
rules["R"] = new PartRule("R", Array.Empty<PartPredicate>());

// 171210436954933152 -- too high

Expand All @@ -70,24 +72,25 @@ public long Part2(string[] input)

private long PossibleCombinations(PartRule current, PartBounds bounds, Dictionary<string, PartRule> rules)
{
long total = 0;
if (current.Id == "R")
{
return 0;
}

foreach (PartPredicate predicate in current.Predicates)
if (current.Id == "A")
{
if (predicate.Destination == "R")
{
return 0;
}
return bounds.Possibilities;
}

if (predicate.Destination == "A")
{
return bounds.Possibilities;
}
long total = 0;

PartRule destination = rules[predicate.Destination];
foreach (PartPredicate predicate in current.Predicates)
{
// reduce the valid bounds according to the current predicate
total += PossibleCombinations(rules[predicate.Destination], bounds.MatchedBounds(predicate), rules);

total += PossibleCombinations(destination, bounds.MatchedBounds(predicate), rules);
total += PossibleCombinations(destination, bounds.NotMatchedBounds(predicate), rules);
// next predicate didn't match this one, so reduce the bounds accordingly in the opposite direction
bounds = bounds.NotMatchedBounds(predicate);
}

return total;
Expand Down Expand Up @@ -196,7 +199,7 @@ private record PartBounds(int MinX, int MaxX, int MinM, int MaxM, int MinA, int
{
public static readonly PartBounds Default = new(1, 4000, 1, 4000, 1, 4000, 1, 4000);

public long Possibilities => (long)(MaxX - MinX) * (MaxM - MinM) * (MaxA - MinA) * (MaxS - MinS);
public long Possibilities => (long)(MaxX - MinX + 1) * (MaxM - MinM + 1) * (MaxA - MinA + 1) * (MaxS - MinS + 1);

public PartBounds MatchedBounds(PartPredicate predicate) => (predicate.Property, predicate.Operation) switch
{
Expand Down
24 changes: 20 additions & 4 deletions tests/AdventOfCode.Tests/Day19Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,30 @@ private static string[] GetSampleInput()
{
return new string[]
{

"px{a<2006:qkq,m>2090:A,rfg}",
"pv{a>1716:R,A}",
"lnx{m>1548:A,A}",
"rfg{s<537:gd,x>2440:R,A}",
"qs{s>3448:A,lnx}",
"qkq{x<1416:A,crn}",
"crn{x>2662:A,R}",
"in{s<1351:px,qqz}",
"qqz{s>2770:qs,m<1801:hdj,R}",
"gd{a>3333:R,R}",
"hdj{m>838:A,pv}",
"",
"{x=787,m=2655,a=1222,s=2876}",
"{x=1679,m=44,a=2067,s=496}",
"{x=2036,m=264,a=79,s=2244}",
"{x=2461,m=1339,a=466,s=291}",
"{x=2127,m=1623,a=2188,s=1013}",
};
}

[Fact]
public void Part1_SampleInput_ProducesCorrectResponse()
{
var expected = -1;
var expected = 19114;

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

Expand All @@ -54,7 +70,7 @@ public void Part1_RealInput_ProducesCorrectResponse()
[Fact]
public void Part2_SampleInput_ProducesCorrectResponse()
{
var expected = -1;
var expected = 167_409_079_868_000;

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

Expand All @@ -64,7 +80,7 @@ public void Part2_SampleInput_ProducesCorrectResponse()
[Fact]
public void Part2_RealInput_ProducesCorrectResponse()
{
var expected = -1;
var expected = 125_455_345_557_345;

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

0 comments on commit fcbe385

Please sign in to comment.