From e9be810bbfc83469c86f91b35f6d124a7b2a96dd Mon Sep 17 00:00:00 2001 From: Jake Charman Date: Fri, 2 Dec 2022 15:07:35 +0000 Subject: [PATCH] Completed 2015 Day 1 --- 2015/Day-01/Day01-1.cs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2015/Day-01/Day01-1.cs diff --git a/2015/Day-01/Day01-1.cs b/2015/Day-01/Day01-1.cs new file mode 100644 index 0000000..a149141 --- /dev/null +++ b/2015/Day-01/Day01-1.cs @@ -0,0 +1,35 @@ +// Input is one line so read it in as a string. +string input = File.ReadAllText(@"input.txt").ToString(); + +// Start on floor 0. +int currentFloor = 0; + +// Counters for Part 2. +int currentChar = 0; +int part2 = 0; + +// Loop through each char in the string and go up/down as directed. +foreach(char direction in input){ + switch(direction){ + case '(': + currentFloor++; + break; + case ')': + currentFloor--; + break; + default: + throw new InvalidDataException("Input did not match expected format!"); + } + + // Increment our counter. + currentChar++; + + // If we are on -1 and part2 is still 0, update it. + if(currentFloor == -1 && part2 == 0){ + part2 = currentChar; + } +} + +// Print out the answers. +Console.WriteLine("Part 1: " + currentFloor); +Console.WriteLine("Part 2: " + part2);