AdventOfCode 2023 Day12

This commit is contained in:
2023-12-24 05:43:00 +01:00
parent edc24dcb65
commit 9ac947fcc1
3 changed files with 1331 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
namespace AdventOfCode2023.Tests;
public class Day12_Tests
{
private readonly string[] _example1 = {
"???.### 1,1,3",
".??..??...?##. 1,1,3",
"?#?#?#?#?#?#?#? 1,3,1,6",
"????.#...#... 4,1,1",
"????.######..#####. 1,6,5",
"?###???????? 3,2,1",
};
[Fact]
public void ResolvePart1__Example1()
{
Day12 day = new();
string result = day.ResolvePart1(_example1);
Assert.Equal("21", result);
}
[Fact]
public void ResolvePart2__Example1()
{
Day12 day = new();
string result = day.ResolvePart2(_example1);
Assert.Equal("525152", result);
}
private static void HotSpring_CountPossibleArrangements__Test(string input, int expected, bool unFold = false)
{
Day12.HotSpring hotSpring = new(input, unFold);
long count = hotSpring.CountPossibleArrangements();
Assert.Equal(expected, count);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests1()
{
HotSpring_CountPossibleArrangements__Test("???.### 1,1,3", 1);
HotSpring_CountPossibleArrangements__Test(".??..??...?##. 1,1,3", 4);
HotSpring_CountPossibleArrangements__Test("?#?#?#?#?#?#?#? 1,3,1,6", 1);
HotSpring_CountPossibleArrangements__Test("????.#...#... 4,1,1", 1);
HotSpring_CountPossibleArrangements__Test("????.######..#####. 1,6,5", 4);
HotSpring_CountPossibleArrangements__Test("?###???????? 3,2,1", 10);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests2_1()
{
HotSpring_CountPossibleArrangements__Test("???.### 1,1,3", 1, true);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests2_2()
{
HotSpring_CountPossibleArrangements__Test(".??..??...?##. 1,1,3", 16384, true);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests2_3()
{
HotSpring_CountPossibleArrangements__Test("?#?#?#?#?#?#?#? 1,3,1,6", 1, true);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests2_4()
{
HotSpring_CountPossibleArrangements__Test("????.#...#... 4,1,1", 16, true);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests2_5()
{
HotSpring_CountPossibleArrangements__Test("????.######..#####. 1,6,5", 2500, true);
}
[Fact]
public void HotSpring_CountPossibleArrangements__Tests2_6()
{
HotSpring_CountPossibleArrangements__Test("?###???????? 3,2,1", 506250, true);
}
}

244
AdventOfCode2023/Day12.cs Normal file
View File

@@ -0,0 +1,244 @@
using System.Text;
namespace AdventOfCode2023;
/*
--- Day 12: Hot Springs ---
You finally reach the hot springs! You can see steam rising from secluded areas attached to the primary, ornate building.
As you turn to enter, the researcher stops you. "Wait - I thought you were looking for the hot springs, weren't you?" You indicate that this definitely looks like hot springs to you.
"Oh, sorry, common mistake! This is actually the onsen! The hot springs are next door."
You look in the direction the researcher is pointing and suddenly notice the massive metal helixes towering overhead. "This way!"
It only takes you a few more steps to reach the main gate of the massive fenced-off area containing the springs. You go through the gate and into a small administrative building.
"Hello! What brings you to the hot springs today? Sorry they're not very hot right now; we're having a lava shortage at the moment." You ask about the missing machine parts for Desert Island.
"Oh, all of Gear Island is currently offline! Nothing is being manufactured at the moment, not until we get more lava to heat our forges. And our springs. The springs aren't very springy unless they're hot!"
"Say, could you go up and see why the lava stopped flowing? The springs are too cold for normal operation, but we should be able to find one springy enough to launch you up there!"
There's just one problem - many of the springs have fallen into disrepair, so they're not actually sure which springs would even be safe to use! Worse yet, their condition records of which springs are damaged (your puzzle input) are also damaged! You'll need to help them repair the damaged records.
In the giant field just outside, the springs are arranged into rows. For each row, the condition records show every spring and whether it is operational (.) or damaged (#). This is the part of the condition records that is itself damaged; for some springs, it is simply unknown (?) whether the spring is operational or damaged.
However, the engineer that produced the condition records also duplicated some of this information in a different format! After the list of springs for a given row, the size of each contiguous group of damaged springs is listed in the order those groups appear in the row. This list always accounts for every damaged spring, and each number is the entire size of its contiguous group (that is, groups are always separated by at least one operational spring: #### would always be 4, never 2,2).
So, condition records with no unknown spring conditions might look like this:
#.#.### 1,1,3
.#...#....###. 1,1,3
.#.###.#.###### 1,3,1,6
####.#...#... 4,1,1
#....######..#####. 1,6,5
.###.##....# 3,2,1
However, the condition records are partially damaged; some of the springs' conditions are actually unknown (?). For example:
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
Equipped with this information, it is your job to figure out how many different arrangements of operational and broken springs fit the given criteria in each row.
In the first line (???.### 1,1,3), there is exactly one way separate groups of one, one, and three broken springs (in that order) can appear in that row: the first three unknown springs must be broken, then operational, then broken (#.#), making the whole row #.#.###.
The second line is more interesting: .??..??...?##. 1,1,3 could be a total of four different arrangements. The last ? must always be broken (to satisfy the final contiguous group of three broken springs), and each ?? must hide exactly one of the two broken springs. (Neither ?? could be both broken springs or they would form a single contiguous group of two; if that were true, the numbers afterward would have been 2,3 instead.) Since each ?? can either be #. or .#, there are four possible arrangements of springs.
The last line is actually consistent with ten different arrangements! Because the first number is 3, the first and second ? must both be . (if either were #, the first number would have to be 4 or higher). However, the remaining run of unknown spring conditions have many different ways they could hold groups of two and one broken springs:
?###???????? 3,2,1
.###.##.#...
.###.##..#..
.###.##...#.
.###.##....#
.###..##.#..
.###..##..#.
.###..##...#
.###...##.#.
.###...##..#
.###....##.#
In this example, the number of possible arrangements for each row is:
???.### 1,1,3 - 1 arrangement
.??..??...?##. 1,1,3 - 4 arrangements
?#?#?#?#?#?#?#? 1,3,1,6 - 1 arrangement
????.#...#... 4,1,1 - 1 arrangement
????.######..#####. 1,6,5 - 4 arrangements
?###???????? 3,2,1 - 10 arrangements
Adding all of the possible arrangement counts together produces a total of 21 arrangements.
For each row, count all of the different arrangements of operational and broken springs that meet the given criteria. What is the sum of those counts?
--- Part Two ---
As you look out at the field of springs, you feel like there are way more springs than the condition records list. When you examine the records, you discover that they were actually folded up this whole time!
To unfold the records, on each row, replace the list of spring conditions with five copies of itself (separated by ?) and replace the list of contiguous groups of damaged springs with five copies of itself (separated by ,).
So, this row:
.# 1
Would become:
.#?.#?.#?.#?.# 1,1,1,1,1
The first line of the above example would become:
???.###????.###????.###????.###????.### 1,1,3,1,1,3,1,1,3,1,1,3,1,1,3
In the above example, after unfolding, the number of possible arrangements for some rows is now much larger:
???.### 1,1,3 - 1 arrangement
.??..??...?##. 1,1,3 - 16384 arrangements
?#?#?#?#?#?#?#? 1,3,1,6 - 1 arrangement
????.#...#... 4,1,1 - 16 arrangements
????.######..#####. 1,6,5 - 2500 arrangements
?###???????? 3,2,1 - 506250 arrangements
After unfolding, adding all of the possible arrangement counts together produces 525152.
Unfold your condition records; what is the new sum of possible arrangement counts?
*/
public class Day12 : IDay
{
public string ResolvePart1(string[] inputs)
{
long sum = inputs
.Select(input => new HotSpring(input))
.Select(hotSpring => hotSpring.CountPossibleArrangements())
.Sum();
return sum.ToString();
}
public string ResolvePart2(string[] inputs)
{
long sum = inputs
.Select(input => new HotSpring(input, true))
.Select(hotSpring => hotSpring.CountPossibleArrangements())
.Sum();
return sum.ToString();
}
public class HotSpring
{
private string _springsRecord;
private int[] _damagedSprings;
public HotSpring(string input, bool unFold = false)
{
string[] inputParts = input.Split(' ');
_springsRecord = inputParts[0];
_damagedSprings = inputParts[1].Split(',').Select(str => Convert.ToInt32(str)).ToArray();
if (unFold)
{
UnFold();
}
}
private void UnFold()
{
_springsRecord = string.Join('?', Enumerable.Repeat(_springsRecord, 5));
_damagedSprings = Enumerable.Repeat(_damagedSprings, 5).SelectMany(x => x).ToArray();
}
public long CountPossibleArrangements()
{
return CountPossibleArrangements(_springsRecord, _damagedSprings);
}
private readonly static Dictionary<string, long> DictMemory = new();
private long CountPossibleArrangements(string springsRecord, int[] damagedSprings)
{
string key = $"{springsRecord} {string.Join(',', damagedSprings)}";
if (DictMemory.TryGetValue(key, out long arrangements))
{
return arrangements;
}
long result = CountPossibleArrangements_Internal(springsRecord, damagedSprings);
DictMemory.Add(key, result);
return result;
}
private long CountPossibleArrangements_Internal(string springsRecord, int[] damagedSprings)
{
if (damagedSprings.Length == 0)
{
if (springsRecord.Contains('#'))
{
// Invalid match; There are more damaged springs on the record but no more damagedSprings groups
return 0;
}
return 1;
}
if (string.IsNullOrEmpty(springsRecord))
{
// Invalid match; There are no more data on the record but there are more damagedSprings groups
return 0;
}
if (springsRecord.StartsWith('.'))
{
// Skip operational springs
return CountPossibleArrangements(springsRecord.Trim('.'), damagedSprings);
}
if (springsRecord.StartsWith('?'))
{
// Both options
long countWithOperational = CountPossibleArrangements
(string.Concat(".", springsRecord.AsSpan(1)), damagedSprings);
long countWithDamaged = CountPossibleArrangements
(string.Concat("#", springsRecord.AsSpan(1)), damagedSprings);
return countWithOperational + countWithDamaged;
}
if (springsRecord.StartsWith('#') == false)
{
throw new Exception("Invalid input");
}
if (springsRecord.Length < damagedSprings[0])
{
// Invalid match; Not enough data on record for the current damagedSprings group
return 0;
}
if (springsRecord.Substring(0, damagedSprings[0]).Contains('.'))
{
// Invalid match; Damaged group cannot contain operational springs inside
return 0;
}
if (damagedSprings.Length == 1)
{
// Skip current damagedSprings group
return CountPossibleArrangements(springsRecord.Substring(damagedSprings[0]), damagedSprings.Skip(1).ToArray());
}
if (springsRecord.Length < (damagedSprings[0] + 1) || springsRecord[damagedSprings[0]] == '#')
{
// Invalid match; There must be an operational (or unknown) spring between damaged spring groups
return 0;
}
// Skip current damagedSprings group, and an extra spring operational or unknown
return CountPossibleArrangements(springsRecord.Substring(damagedSprings[0] + 1), damagedSprings.Skip(1).ToArray());
}
}
}

File diff suppressed because it is too large Load Diff