From 496975da67e0f1575b8514c432db5b28663bacf9 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Sat, 2 Dec 2023 01:39:47 +0000 Subject: [PATCH] Advent of Code 2023 started :D Signed-off-by: TiagoRG --- .gitignore | 4 +-- Makefile | 6 ++++ Program.cs | 6 ++-- Year2023/Day1.cs | 81 ++++++++++++++++++++++++++++++++++++++++++ Year2023/Loader2023.cs | 16 +++++++++ 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 Makefile create mode 100644 Year2023/Day1.cs create mode 100644 Year2023/Loader2023.cs diff --git a/.gitignore b/.gitignore index 6ee8d6f..137761b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore -# Rider configuration directory -.idea/ +# AdventOfCode input files +inputs/ # User-specific files *.rsuser diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f6a094 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +.PHONY: build + +build: + @dotnet build + @echo -e "\033c\nAdvent of Code\n\nBy: @TiagoRG\nGitHub: https://github.com/TiagoRG\n\n=============================================================" + @dotnet run diff --git a/Program.cs b/Program.cs index c9316af..1e1c9e7 100644 --- a/Program.cs +++ b/Program.cs @@ -1,9 +1,9 @@ -using AdventOfCode.Year2022; +using AdventOfCode.Year2023; namespace AdventOfCode; internal static class Program { public static void Main(string[] args) - => Loader2022.Load(); -} \ No newline at end of file + => Loader2023.Load(); +} diff --git a/Year2023/Day1.cs b/Year2023/Day1.cs new file mode 100644 index 0000000..b86edcf --- /dev/null +++ b/Year2023/Day1.cs @@ -0,0 +1,81 @@ +namespace AdventOfCode.Year2023; + +public class Day1 +{ + private static List Input = File.ReadAllLines("inputs/Year2023/day1.txt").ToList(); + private static int FirstNum; + private static int LastNum; + + public static void Run() + { + Console.WriteLine($@" +Day1 Solution +Part1 Result: {Part1()} +Part2 Result: {Part2()} + +============================="); + } + + private static int Part1() + { + List numbers = new List(); + Input.ForEach(line => + { + List chars = line.ToList(); + numbers.Add(Convert.ToInt32($"{chars.First(c => Char.IsDigit(c))}{chars.Last(c => Char.IsDigit(c))}")); + }); + + return numbers.Sum(); + } + + private static int Part2() + { + string[] validStrNums = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; + + List numbers = new List(); + Input.ForEach(line => + { + int min = line.Length; + int max = -1; + foreach (var str in validStrNums) + { + if (!line.Contains(str)) continue; + + int firstIndex = line.IndexOf(str); + if (firstIndex < min) + { + min = firstIndex; + FirstNum = ParseInt(str); + } + + int lastIndex = line.LastIndexOf(str); + if (lastIndex > max) + { + max = lastIndex; + LastNum = ParseInt(str); + } + } + + numbers.Add(Convert.ToInt32($"{FirstNum}{LastNum}")); + }); + + return numbers.Sum(); + } + + private static int ParseInt(string str) + { + return (str) switch + { + "one" => 1, + "two" => 2, + "three" => 3, + "four" => 4, + "five" => 5, + "six" => 6, + "seven" => 7, + "eight" => 8, + "nine" => 9, + _ => Convert.ToInt32(str), + }; + } +} diff --git a/Year2023/Loader2023.cs b/Year2023/Loader2023.cs new file mode 100644 index 0000000..d56d0b5 --- /dev/null +++ b/Year2023/Loader2023.cs @@ -0,0 +1,16 @@ +namespace AdventOfCode.Year2023; + +public static class Loader2023 +{ + public static void Load() + { + /* Year 2023 + * + * Solutions in C# for the 2023 Advent of Code + * Slower solutions are commented + */ + + Day1.Run(); + } +} +