mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-12 18:53:47 +00:00
feat(*): Add 2021-10 part 2 files
This commit is contained in:
+101
-3
@@ -1,4 +1,7 @@
|
||||
namespace Day10
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace Day10
|
||||
{
|
||||
class Program
|
||||
{
|
||||
@@ -10,8 +13,8 @@
|
||||
string[] lines = File.ReadAllLines("input.txt");
|
||||
|
||||
|
||||
Part1(lines);
|
||||
//Part2(lines);
|
||||
//Part1(lines);
|
||||
Part2(lines);
|
||||
}
|
||||
|
||||
static void Part1(string[] lines)
|
||||
@@ -30,6 +33,64 @@
|
||||
Console.WriteLine($"Syntax error score: {syntaxErrorScore}");
|
||||
}
|
||||
|
||||
static void Part2(string[] lines)
|
||||
{
|
||||
List<string> uncorruptedLines = new List<string>();
|
||||
int syntaxErrorScore = 0;
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (!IsLineCorrupted(line, ref syntaxErrorScore))
|
||||
{
|
||||
uncorruptedLines.Add(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we just have the incomplete lines, work out how to complete them
|
||||
|
||||
List<BigInteger> autoCompleteLineScores = new List<BigInteger>();
|
||||
|
||||
foreach (string line in uncorruptedLines)
|
||||
{
|
||||
BigInteger totalLineScore = 0;
|
||||
string characters = GetCharactersToCompleteLine(line);
|
||||
|
||||
// Now calculate the score for the line
|
||||
foreach (char character in characters)
|
||||
{
|
||||
totalLineScore = totalLineScore * 5;
|
||||
|
||||
switch (character)
|
||||
{
|
||||
case ')':
|
||||
totalLineScore += 1;
|
||||
break;
|
||||
case ']':
|
||||
totalLineScore += 2;
|
||||
break;
|
||||
case '}':
|
||||
totalLineScore += 3;
|
||||
break;
|
||||
case '>':
|
||||
totalLineScore += 4;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
autoCompleteLineScores.Add(totalLineScore);
|
||||
|
||||
Console.WriteLine($"{characters} - {totalLineScore} total points.");
|
||||
}
|
||||
|
||||
// the winner is found by sorting all of the scores and then taking the middle score. (There will always be an odd number of scores to consider.)
|
||||
autoCompleteLineScores.Sort();
|
||||
BigInteger middleScore = autoCompleteLineScores.Skip(autoCompleteLineScores.Count / 2).Take(1).First();
|
||||
|
||||
Console.WriteLine($"Middle score: {middleScore}");
|
||||
}
|
||||
|
||||
static bool IsLineCorrupted(string line, ref int syntaxErrorScore)
|
||||
{
|
||||
Stack<char> stackOfExpectedClosingCharacters = new Stack<char>();
|
||||
@@ -85,6 +146,43 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
static string GetCharactersToCompleteLine(string line)
|
||||
{
|
||||
Stack<char> stackOfExpectedClosingCharacters = new Stack<char>();
|
||||
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
if (openingCharacters.Contains(line[i]))
|
||||
{
|
||||
// If it's an opening character then add the expected closing character to the stack
|
||||
stackOfExpectedClosingCharacters.Push(GetExpectedClosingCharacter(line[i]));
|
||||
}
|
||||
else if (closingCharacters.Contains(line[i]))
|
||||
{
|
||||
// If it's a closing character, then it must be the one we're expecting as we've already got rid of the corrupt lines
|
||||
// Make sure there's items in the stack so we don't get an InvalidOperationException, then pop it off the stack
|
||||
if (stackOfExpectedClosingCharacters.Count > 0)
|
||||
{
|
||||
stackOfExpectedClosingCharacters.Pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The stack should now contain all the closing characters needed, in order, so get them into a string
|
||||
StringBuilder charactersToCompleteLine = new StringBuilder();
|
||||
|
||||
while (stackOfExpectedClosingCharacters.Count > 0)
|
||||
{
|
||||
char character = stackOfExpectedClosingCharacters.Pop();
|
||||
charactersToCompleteLine.Append(character);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Complete by adding {charactersToCompleteLine.ToString()}");
|
||||
|
||||
return charactersToCompleteLine.ToString();
|
||||
}
|
||||
|
||||
|
||||
static char GetExpectedClosingCharacter(char openingCharacter)
|
||||
{
|
||||
int index = openingCharacters.FindIndex(x => x == openingCharacter);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
--- Part Two ---
|
||||
Now, discard the corrupted lines. The remaining lines are incomplete.
|
||||
|
||||
Incomplete lines don't have any incorrect characters - instead, they're missing some closing characters at the end of the line. To repair the navigation subsystem, you just need to figure out the sequence of closing characters that complete all open chunks in the line.
|
||||
|
||||
You can only use closing characters (), ], }, or >), and you must add them in the correct order so that only legal pairs are formed and all chunks end up closed.
|
||||
|
||||
In the example above, there are five incomplete lines:
|
||||
|
||||
[({(<(())[]>[[{[]{<()<>> - Complete by adding }}]])})].
|
||||
[(()[<>])]({[<{<<[]>>( - Complete by adding )}>]}).
|
||||
(((({<>}<{<{<>}{[]{[]{} - Complete by adding }}>}>)))).
|
||||
{<[[]]>}<{[{[{[]{()[[[] - Complete by adding ]]}}]}]}>.
|
||||
<{([{{}}[<[[[<>{}]]]>[]] - Complete by adding ])}>.
|
||||
Did you know that autocomplete tools also have contests? It's true! The score is determined by considering the completion string character-by-character. Start with a total score of 0. Then, for each character, multiply the total score by 5 and then increase the total score by the point value given for the character in the following table:
|
||||
|
||||
): 1 point.
|
||||
]: 2 points.
|
||||
}: 3 points.
|
||||
>: 4 points.
|
||||
So, the last completion string above - ])}> - would be scored as follows:
|
||||
|
||||
Start with a total score of 0.
|
||||
Multiply the total score by 5 to get 0, then add the value of ] (2) to get a new total score of 2.
|
||||
Multiply the total score by 5 to get 10, then add the value of ) (1) to get a new total score of 11.
|
||||
Multiply the total score by 5 to get 55, then add the value of } (3) to get a new total score of 58.
|
||||
Multiply the total score by 5 to get 290, then add the value of > (4) to get a new total score of 294.
|
||||
The five lines' completion strings have total scores as follows:
|
||||
|
||||
}}]])})] - 288957 total points.
|
||||
)}>]}) - 5566 total points.
|
||||
}}>}>)))) - 1480781 total points.
|
||||
]]}}]}]}> - 995444 total points.
|
||||
])}> - 294 total points.
|
||||
Autocomplete tools are an odd bunch: the winner is found by sorting all of the scores and then taking the middle score. (There will always be an odd number of scores to consider.) In this example, the middle score is 288957 because there are the same number of scores smaller and larger than it.
|
||||
|
||||
Find the completion string for each incomplete line, score the completion strings, and sort the scores. What is the middle score?
|
||||
Reference in New Issue
Block a user