feat(2015-01): Add day 1 solution

This commit is contained in:
Josh Creek
2022-10-12 20:56:25 +01:00
parent 688338ceeb
commit fba2ad5032
4 changed files with 120 additions and 0 deletions
+7
View File
@@ -8,6 +8,13 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Remove="Y2015\Data\**" />
<Content Include="Y2015\Data\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Remove="Y2021\Data\**" />
<Content Include="Y2021\Data\**">
File diff suppressed because one or more lines are too long
+82
View File
@@ -0,0 +1,82 @@
using System.Reflection;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day01
{
protected string GetThisClassName() { return this.GetType().Name; }
private string realData;
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"))[0];
}
[TestCase("(())", 0)] // Example data
[TestCase("()()", 0)] // Example data
[TestCase("(((", 3)] // Example data
[TestCase("(()(()(", 3)] // Example data
[TestCase("))(((((", 3)] // Example data
[TestCase("())", -1)] // Example data
[TestCase("))(", -1)] // Example data
[TestCase(")))", -3)] // Example data
[TestCase(")())())", -3)] // Example data
[TestCase(null, 232)] // The actual answer
public void Part1(string input, int? expected)
{
var lines = input != null ? input : realData;
int floorsUp = lines.Count(x => x == '(');
int floorsDown = lines.Count(x => x == ')');
var result = floorsUp - floorsDown;
if (expected != null)
{
Assert.That(result, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {result}");
}
[TestCase(")", 1)] // Example data
[TestCase("()())", 5)]
[TestCase(null, 1783)] // The actual answer
public void Part2(string input, int? expected)
{
var lines = input != null ? input : realData;
var currentFloor = 0;
int? result = null;
foreach (var (instruction, index) in lines.WithIndex())
{
if (instruction is '(')
{
currentFloor += 1;
}
else if (instruction is ')')
{
currentFloor -= 1;
}
if (currentFloor is -1)
{
// Zero-indexed, so need to increment
result = index + 1;
break;
}
}
if (expected != null)
{
Assert.That(result, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {result}");
}
}
}
@@ -0,0 +1,30 @@
--- Day 1: Not Quite Lisp ---
Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.
Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
Here's an easy puzzle to warm you up.
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.
The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.
For example:
(()) and ()() both result in floor 0.
((( and (()(()( both result in floor 3.
))((((( also results in floor 3.
()) and ))( both result in floor -1 (the first basement level).
))) and )())()) both result in floor -3.
To what floor do the instructions take Santa?
--- Part Two ---
Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
For example:
) causes him to enter the basement at character position 1.
()()) causes him to enter the basement at character position 5.
What is the position of the character that causes Santa to first enter the basement?