Skip to content

Commit

Permalink
day(24): By hand
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 24, 2024
1 parent f187db3 commit 1cdeb50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
29 changes: 22 additions & 7 deletions src/AdventOfCode/Day24.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ public long Part1(string[] input)
return Check(state, rules);
}

public int Part2(string[] input)
public string Part2(string[] input)
{
foreach (string line in input)
{
throw new NotImplementedException("Part 2 not implemented");
}

return 0;
// did this by hand - see unit tests
return "cpm,ghp,gpr,krs,nks,z10,z21,z33";
}

/// <summary>
/// For part 2 - set x and y state to the given values and run the
/// rules (which may have been modified from original)
/// </summary>
/// <param name="x">Initial X</param>
/// <param name="y">Initial Y</param>
/// <param name="input">Input</param>
/// <returns>Resulting Z</returns>
public long Add(long x, long y, string[] input)
{
(Dictionary<string, bool> state, List<Rule> rules) = Parse(input);
Expand All @@ -46,6 +50,11 @@ public long Add(long x, long y, string[] input)
return result;
}

/// <summary>
/// Parse the input
/// </summary>
/// <param name="input">Input</param>
/// <returns>Parsed initial state and rules</returns>
private static (Dictionary<string, bool> State, List<Rule> Rules) Parse(string[] input)
{
Dictionary<string, bool> state = input.TakeWhile(l => !string.IsNullOrEmpty(l))
Expand All @@ -66,6 +75,12 @@ private static (Dictionary<string, bool> State, List<Rule> Rules) Parse(string[]
return (state, rules);
}

/// <summary>
/// Run the rules against the initial state
/// </summary>
/// <param name="state">State</param>
/// <param name="rules">Rules</param>
/// <returns>Resulting number in Z positions</returns>
private static long Check(Dictionary<string, bool> state, List<Rule> rules)
{
// this is topological sort, but I can't remember that and it's small enough to brute force
Expand Down
21 changes: 6 additions & 15 deletions tests/AdventOfCode.Tests/Day24Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,10 @@ public void Part1_RealInput_ProducesCorrectResponse()
Assert.Equal(expected, result);
}

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

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

Assert.Equal(expected, result);
}

[Fact]
public void Part2_RealInput_ProducesCorrectResponse()
{
var expected = -1;
var expected = "cpm,ghp,gpr,krs,nks,z10,z21,z33";

var result = solver.Part2(GetRealInput());
output.WriteLine($"Day 24 - Part 2 - {result}");
Expand All @@ -131,7 +121,7 @@ public void Part2_RealInput_ProducesCorrectResponse()
[InlineData(128, 0, 128)]
[InlineData(256, 0, 256)]
[InlineData(512, 0, 512)]
[InlineData(1 << 10, 0, 1 << 10)] // broken - htv,z10
[InlineData(1 << 10, 0, 1 << 10)] // broken - htv,gpr,z10
[InlineData(1 << 11, 0, 1 << 11)]
[InlineData(1 << 12, 0, 1 << 12)]
[InlineData(1 << 13, 0, 1 << 13)]
Expand Down Expand Up @@ -160,7 +150,7 @@ public void Part2_RealInput_ProducesCorrectResponse()
[InlineData(1L << 36, 0, 1L << 36)]
[InlineData(1L << 37, 0, 1L << 37)]
[InlineData(1L << 38, 0, 1L << 38)]
[InlineData(1L << 39, 0, 1L << 39)] // broken - cpm,z39
[InlineData(1L << 39, 0, 1L << 39)] // broken - cpm,z39,krs
[InlineData(1L << 40, 0, 1L << 40)]
[InlineData(1L << 41, 0, 1L << 41)]
[InlineData(1L << 42, 0, 1L << 42)]
Expand All @@ -169,12 +159,13 @@ public void Part2_RealInput_ProducesCorrectResponse()
public void Add_WhenCalled_AddsInputs(long x, long y, long expected)
{
string[] input = File.ReadAllLines("inputs/day24.txt");
SwapOutputs(input, "z10", "htv");
SwapOutputs(input, "z10", "gpr");
SwapOutputs(input, "z21", "nks");
SwapOutputs(input, "z33", "ghp");
SwapOutputs(input, "z39", "cpm");
SwapOutputs(input, "krs", "cpm");

// cpm,ghp,htv,nks,z10,z21,z33,z39
// cpm,ghp,gpr,krs,nks,z10,z21,z33

long result = this.solver.Add(x, y, input);

Expand Down

0 comments on commit 1cdeb50

Please sign in to comment.