Day 1 - Part 2

This commit is contained in:
Kenneth Lorthioir 2023-12-04 12:34:10 -05:00
parent bbfb7017f3
commit 628afe69ac
4 changed files with 1096 additions and 0 deletions

61
day1/part2/Program.cs Normal file
View File

@ -0,0 +1,61 @@
using System.Globalization;
namespace part2;
class Program
{
static void Main(string[] args)
{
// The description does not say this (had to go to reddit to find this)
// but the strings can combine the same letter for 2 unique numbers
// i.e. sevenine is "79" not "7ine"
int sum = 0;
string letters = "abcdefghijklmnopqrstuvwxyz";
Dictionary<string, int> written_numbers = new() {
{"one", 1},
{"two", 2},
{"three", 3},
{"four", 4},
{"five", 5},
{"six", 6},
{"seven", 7},
{"eight", 8},
{"nine", 9}
};
StreamReader sr = new StreamReader("input");
String line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
line = line.ToLower();
List<int> numbers_in_line = [];
for(int i = 0; i < line.Length; i++) {
foreach(var number in written_numbers) {
if(line[i] == number.Key[0]) {
if(string.Compare(line, i, number.Key, 0, number.Key.Length) == 0) {
numbers_in_line.Add(number.Value);
}
}
if(char.IsNumber(line[i])) {
numbers_in_line.Add(Int32.Parse(line[i].ToString()));
}
}
}
sum += numbers_in_line.First() * 10;
sum += numbers_in_line.Last();
Console.WriteLine($"Line: {line} - Sum: {sum}");
line = sr.ReadLine();
}
//close the file
sr.Close();
Console.WriteLine($"Total Sum: {sum}");
}
}

1000
day1/part2/input Normal file

File diff suppressed because it is too large Load Diff

10
day1/part2/part2.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

25
day1/part2/part2.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "part2", "part2.csproj", "{77D1FD2B-3DFB-4114-B145-CE3B20D4EE36}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{77D1FD2B-3DFB-4114-B145-CE3B20D4EE36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77D1FD2B-3DFB-4114-B145-CE3B20D4EE36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77D1FD2B-3DFB-4114-B145-CE3B20D4EE36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77D1FD2B-3DFB-4114-B145-CE3B20D4EE36}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DA83D587-7910-47BE-BE85-B085901ED607}
EndGlobalSection
EndGlobal