Skip to content

Commit

Permalink
2018 Day12 refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 14, 2023
1 parent a5de33e commit 23efa32
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 60 deletions.
46 changes: 12 additions & 34 deletions Solutions/2018/Day12.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text;

namespace AdventOfCode.Solutions._2018;
namespace AdventOfCode.Solutions._2018;

/// <summary>
/// Day 12: Subterranean Sustainability
Expand All @@ -15,39 +13,19 @@ public sealed partial class Day12 {
private static readonly char NO_PLANT = '.';
private static readonly char PLANT = '#';

private static int Solution1(string[] input) {
const int noOfGenerations = 20;
string currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{input[0][15..]}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}";
int offset = 4;

HashSet<string> plantPatterns = input[2..]
.Where(i => i.EndsWith(PLANT))
.Select(i => i[..5])
.ToHashSet();

for (int gen = 1; gen <= noOfGenerations; gen++) {
char[] newState = new char[currentState.Length - 4];
for (int pot = 0; pot < currentState.Length - 4; pot++) {
char plant = plantPatterns.Contains(currentState[pot..(pot + 5)]) ? PLANT : NO_PLANT;
newState[pot] = plant;
}
string state = new(newState);
offset = offset + 2 - state.IndexOf(PLANT);
currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{state.Trim(NO_PLANT)}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}";
}

int potPlantSum = 0;
for (int pot = 0; pot < currentState.Length - 4; pot++) {
if (currentState[pot] == PLANT) {
potPlantSum += pot - offset;
}
}

return potPlantSum;
private static long Solution1(string[] input) {
const long noOfGenerations = 20;
return GrowPlantsAndReturnSum(input, noOfGenerations);
}

private static long Solution2(string[] input) {
private static long Solution2(string[] input)
{
const long noOfGenerations = 50_000_000_000;
return GrowPlantsAndReturnSum(input, noOfGenerations);
}

private static long GrowPlantsAndReturnSum(string[] input, long noOfGenerations)
{
string currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{input[0][15..]}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}";
long offset = 4;

Expand All @@ -68,7 +46,7 @@ private static long Solution2(string[] input) {
string newState = new(newStateArray);
offset = offset + 2 - newState.IndexOf(PLANT);
currentState = $"{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}{newState.Trim(NO_PLANT)}{NO_PLANT}{NO_PLANT}{NO_PLANT}{NO_PLANT}";
if (state.TryGetValue(currentState, out long stateOffset)) {
if (state.TryGetValue(currentState, out long stateOffset)) {
// Turns out this is the previous state that repeats so we have a simple calculation for the remaining iterations
offset += (noOfGenerations - gen) * (offset - stateOffset);
break;
Expand Down
26 changes: 0 additions & 26 deletions Tests/2018/Tests_12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,4 @@ public void Part1(string input, int expected)
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART1, input), out int actual);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("""
initial state: #..#.#..##......###...###
...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #
"""
, 325)]
public void Part2(string input, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, 32), out int actual);
Assert.Equal(expected, actual);
}
}

0 comments on commit 23efa32

Please sign in to comment.