From fba2ad5032d277be6bd1e962c843a902bbf64b0a Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Wed, 12 Oct 2022 20:56:25 +0100 Subject: [PATCH] feat(2015-01): Add day 1 solution --- AdventOfCode/AOC.Tests/AOC.Tests.csproj | 7 ++ AdventOfCode/AOC.Tests/Y2015/Data/Day01.dat | 1 + AdventOfCode/AOC.Tests/Y2015/Day01.cs | 82 ++++++++++++++++++++ AdventOfCode/AOC.Tests/Y2015/Tasks/Day01.txt | 30 +++++++ 4 files changed, 120 insertions(+) create mode 100644 AdventOfCode/AOC.Tests/Y2015/Data/Day01.dat create mode 100644 AdventOfCode/AOC.Tests/Y2015/Day01.cs create mode 100644 AdventOfCode/AOC.Tests/Y2015/Tasks/Day01.txt diff --git a/AdventOfCode/AOC.Tests/AOC.Tests.csproj b/AdventOfCode/AOC.Tests/AOC.Tests.csproj index 599b3d1..88c5d56 100644 --- a/AdventOfCode/AOC.Tests/AOC.Tests.csproj +++ b/AdventOfCode/AOC.Tests/AOC.Tests.csproj @@ -8,6 +8,13 @@ false + + + + Always + + + diff --git a/AdventOfCode/AOC.Tests/Y2015/Data/Day01.dat b/AdventOfCode/AOC.Tests/Y2015/Data/Day01.dat new file mode 100644 index 0000000..73e014e --- /dev/null +++ b/AdventOfCode/AOC.Tests/Y2015/Data/Day01.dat @@ -0,0 +1 @@ +(((())))()((((((((())()(()))(()((((()(()(((()((()((()(()()()()()))(((()(()((((((((((())(()()((())()(((())))()(()(()((()(()))(()()()()((()((()(((()()(((((((()()())()((((()()(((((()(())()(())((())()()))()(((((((())(()())(()(((())(()))((())))(()((()())))()())((((())))(()(((((()(())(((()()((()((()((((((((((())(()())))))()))())()()((((()()()()()()((((((())())(((()())()((()()(((()()()))(((((()))(((()(()()()(()(()(((())()))(()(((()((())()(()())())))((()()()(()()(((()))(((()((((()(((((()()(()())((()())())(()((((((()(()()))((((()))))())((())()()((()(()))))((((((((()))(()()(((())())(())()((()()()()((()((()((()()(((())))(()((())()((((((((()((()(()()(((())())())))(())())))()((((()))))))())))()()))()())((()())()((()()()))(()()(((()(())((((())())((((((((()()()()())))()()()((((()()))))))()((((()(((()))(()()())))((()()(((()))()()())())(((())((()()(())()()()(((())))))()())((()))()))((())()()())()())()()(()))())))())()))(())((()(())))(()(())(()))))(()(())())(()(())(()(()))))((()())()))()((((()()))))())))()()())((())()((()()()))()(((()(()))))(())()()))(((()())))))))))(((())))()))())()))))()()(((())))))))()(()()(()))((()))))((())))((()((())))())))()()(()))())()(()((()())(()(()()())())(()()))()))))(()())()()))()()()()))(()(()(()))))))()(()))()))()()(()((())(()(())))()(((())(())())))))()(()(()))))()))(()()()(())()(()(())))()))))()()(((((())))))())()())())())()())()))))()))))))))())()()()()()()())))()))((())()))())))()((())()))))()))())))))))())()()()))()()(()((((()(((((((()(())((()())((()()))()))))(())))()()()(())((())()())))(())))(())))(((()()))()(())(((()(()))((())))())()))((((()))())()))))))))()(())())))(()))()(()()))())()()(())())))())()()(()())))()((()())(()(())(())))))))))))))(()))))()))))))()()())(()(((((()(()())))())()))(()))()))(()()))()())(()))())()(())((()()))))))())))())()(((())))(()(()))()()))()(()))))))((()())(()))))))()())))()()))))))))((((((((()()()(()))))))()())))())))()()((())()))((())(())))())())))()()()((()((()(())))())()(())))))))))()())))()()()()()()))()))((())())(()(()))))))(()()))()))(())))()))))))))))))(()))))))))()))))()))()())()))()()))))))()))))((()))))(()))())()(())))(()())((((()())))()))))(()))()(()()(())))))())))))()))))))())))())))))())))())())))())(()))))(())()(())))())()))((()()))))))())))((())))))))())))(())))))()()())))))())))))()))))))()))()()()(()(((()())())())(()))())))))((()(())(()))))))))(())))()()()())())(()))))()()()))()))())())())()(())))()(((()((((())))))))()))))))))))))))))))))((())()())(()))))()()))))))(()()(())())))())))((())))((())))))))))))))()))))()(()))))))())))))()))(()()())(()())))))))))()))))))(())))))()()))()())(((())))()))(()))))))))(())())))())))())())())()()))((())()(())()())()))()())(())(()))))()())))(()(((()))))))()(()())()()()))()))))))))()()()(())()())()(((((()))()())())(()))))()()()(())))())))()((()())))(()))())()(()())())(()))()()))((()()))((()()()()())))(())()))(()(())))((()()))))))))())))))))())()()))))))))))))))))(())()(())(())()())())()))()(()))))())())))))()())()(()))()()(())))(())())))))(()))))))))))))))())())(())(())))(((()))()))))())((())(()))())))))))())))))())))()))()))))))))))))())()))))()))))((()))(())))()(())))(())()))()))())))())))))))()(()())())))()()())))(())))))(()))))))))))))(()))()))()))())))(((()()()(())((()())))()())(((()))(())()))((()()()())))())(())(()))))()(((((())))(()))())())))))))((((()()()))())())()(()(()())))))))))()())())))(())))()())(((()(())())()()))())())))))))((()())((()()(()))(()(())))()))()))(()))(()))()()(()(((())((((()))()(()))((())()(()(()())()(()))()())))))(()))()))())()())))())))(())))((())(()())))))()))(())(()))()())()(()()((()(()))))))()(())(()())(())()))(((())()))(()()(()()()))))(()(())))()))))())))))())(()()()()()()(((())))(()()))()((())(((((()()())))(()))(()))()()))(((())())()(((()()()()))))(()))(())())))()())(()()())())))))))()))))((())))()())(()))(()(()))())))))())(())))))()()())())()))()()(())))(()))(())((((((())(()))(()))())()))(()()(())))()))(()()))()))()(())))(())))((()(()))(())()()())())))(((()()())(())()))))))()(((()(((((()()(((())(())))())()((()))))((()())()(())(((())))(((()((()(()(()))(()()))())(()))(())(())))()))))))((((()))()((((()(()))()))()()))))()(()(()))()(()((()(((()(()()(((()))))()(((()(()(()(((()(()())())()()(()(()())())(()((((())(()))()))(((((()()())(())()((()()())))()()(((()()))()((((((((()(())))())((()))))(())))(()))))((()((((()()(())(((((()))(((((((((((((()())))((((()(((()((())())()))((()))()(()()((()()()()(()()(()(()(((())()(()((((((()((()()((())()((((()((()()(()()())((()()()((()((())()(()(((()((())((((())(()))((()(()))(()())()((((((((()(((((((((((()))(()(((()(()()()((((())((())()())()))(())((())(()))(((()((()(())))(()))))((()()))))((((()(()(()())(()(())((((((((()((((()((()(((((()))())()(()))(()()((()(())(((((()(())()(((((()()))))))()(((())()(()()((((())()((())((()(((())(((()))((()()((((()(())))))((()((((()((()((()(((())((()))(((((((()(((()((((((((())()))((((())(((((()((((((((()(((()((()(((()()(((()((((((()()(()((((((((()()(()(()(())((((()())()))))(((()))((((())((((()())((()(())()((()((((((()((((((()(())))()())(((())())())()(())()(()())((()()((((())((((((())(()(((((()((((())()((((()(()(())(()())(((())()((())((((()))()((((((())(()(((()(((()((((((()(((()))(()()())())((()((()())()((((())(((()(()(((((((((())(())))()((()()()()(())((()))(((((((()(((((((((()(()))))(()((((((((()((((()((()()((((((()()(((((((()(()(())()(())((()()()((()(((((()())()(((((()())()()((()(()())(()()()(((()()(((((()((((((()()((()(()()()((((((((((((()((((((((()()(((()())))()(((()()(())())((((()((((()((((()()()(())(())((()(()(((((((((((((((()(())(())))))()()))((()(((()(())((()(((()(()()((((()()(((()(((()(((((()()((()(()(((()))((((((()((((((((()((()((())(((((()(((())(())())((()()))((((())()()((()(((()(((((()()(((()))(((()(()(((((((((((((()))((((((((()(((()))))())((((((((((((())((())((()())(((())((())(()((((((((((()(((())((()()(()((())(((((((((((()))((((((((((((()(()())((()((()((()(()(((()((((((((()()(()((()(()(((()))((()))(((((((((((((()(())((((((())(((()(())(()(()(()((()()))((((()((((()((((())))())((((()((((()))((((((()((((((()((()(((())))((())(()))(()((()((((()((()(((()()))((((()()()(((((((())(((())(()))())((((()())(((()(((((((((((()(()(()((()(((((((((((((((()()((((()((((((((()(((()()((()((((()))(((()(())((((((()((((())()((((()((()))(())()(()(((()((())())((((((()(()(())())(((())(()(()())(((((()((()((())()())(())))(((()(())))))))(((()(((()))()((()(((()()((()())()()))())))(((()))(()(((()(((((((((()(()(((((()()(((()())()()))))()(((()))(((()(()(()(()(()))()(())()))(()(((())))(()))))))))))(())((()((())((()(())()(())((()()((((()()((()()))((())(((()((()(())(())))()(()(((((()((()))())()(((((()()(((()(()((((((())(()))(())()))((()(()()))(())())()))(((())))(()((()(((())(())())))((()()((((((((((((((()((()(()()(()(((()))())()()((()()()(())(()))(()())(((())((())()(())()()(()()(())))((()(((()))))(((()()(()()))())((()((())()))((((()()()())((())))(((()(())(((((()(((((()((()(()((((()()(((()()()(((()())(((()()((((())(()))(((()))(())())((()))(((()((()))(((()()((())((()(((((()((((()()())((()))()((((()((()(()()()( \ No newline at end of file diff --git a/AdventOfCode/AOC.Tests/Y2015/Day01.cs b/AdventOfCode/AOC.Tests/Y2015/Day01.cs new file mode 100644 index 0000000..a89d303 --- /dev/null +++ b/AdventOfCode/AOC.Tests/Y2015/Day01.cs @@ -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}"); + } + } +} \ No newline at end of file diff --git a/AdventOfCode/AOC.Tests/Y2015/Tasks/Day01.txt b/AdventOfCode/AOC.Tests/Y2015/Tasks/Day01.txt new file mode 100644 index 0000000..361b3b5 --- /dev/null +++ b/AdventOfCode/AOC.Tests/Y2015/Tasks/Day01.txt @@ -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? \ No newline at end of file