Done 2023 day01
This commit is contained in:
parent
f9cb505e1d
commit
f1906f48c7
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
*.jar
|
||||
2022/day05/bin/Debug/net7.0
|
||||
2022/day05/obj
|
||||
2023/day01/bin/Debug/net7.0
|
||||
2023/day01/obj
|
||||
|
|
147
2023/day01/Program.cs
Normal file
147
2023/day01/Program.cs
Normal file
|
@ -0,0 +1,147 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace day01
|
||||
{
|
||||
class Program {
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string line;
|
||||
int sum = 0;
|
||||
int correctSum = 0;
|
||||
try
|
||||
{
|
||||
StreamReader sr = new StreamReader("./input.txt");
|
||||
line = sr.ReadLine()!;
|
||||
while (line != null)
|
||||
{
|
||||
int? first = FristDigit(line);
|
||||
int? last = LastDigit(line);
|
||||
string combinated = $"{first}{last}";
|
||||
int combinatedNum = Int32.Parse(combinated);
|
||||
sum += combinatedNum;
|
||||
System.Console.WriteLine($"String: {line}\t First Digit: {first}\t Last Digit: {last}");
|
||||
int[] nums = getNumbers(line);
|
||||
System.Console.WriteLine($"String: {line}\t First correct Digit: {nums[0]}\t Last correct Digit: {nums[1]}");
|
||||
string correctCombinated = $"{nums[0]}{nums[1]}";
|
||||
int correctCombinatedNum = Int32.Parse(correctCombinated);
|
||||
correctSum += correctCombinatedNum;
|
||||
line = sr.ReadLine()!;
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine("Exception: " + e.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.Console.WriteLine($"The sum is {sum}");
|
||||
System.Console.WriteLine($"The correct sum is {correctSum}");
|
||||
}
|
||||
}
|
||||
|
||||
public static int? FristDigit(string line)
|
||||
{
|
||||
int? number = null;
|
||||
foreach(char c in line)
|
||||
{
|
||||
if(char.IsNumber(c))
|
||||
{
|
||||
number = (int)(c - '0');
|
||||
return number;
|
||||
}
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
public static int? LastDigit(string line)
|
||||
{
|
||||
string reverted = Reverse(line);
|
||||
int? number = null;
|
||||
foreach(char c in reverted)
|
||||
{
|
||||
if(char.IsNumber(c))
|
||||
{
|
||||
number = (int)(c - '0');
|
||||
return number;
|
||||
}
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
public static string Reverse(string s)
|
||||
{
|
||||
char[] charArray = s.ToCharArray();
|
||||
Array.Reverse(charArray);
|
||||
return new string(charArray);
|
||||
}
|
||||
|
||||
public static int[] getNumbers(string line)
|
||||
{
|
||||
int[] retVal = new int[2];
|
||||
Match match1 = Regex.Match(line, @"(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine)|(zero)|\d");
|
||||
Match match2 = Regex.Match(line, @"(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine)|(zero)|\d", RegexOptions.RightToLeft);
|
||||
if(match1.Value.Length == 1){
|
||||
retVal[0] = Int32.Parse(match1.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal[0] = NumberWordToInt(match1.Value);
|
||||
}
|
||||
if(match2.Value.Length == 1) {
|
||||
retVal[1] = Int32.Parse(match2.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal[1] = NumberWordToInt(match2.Value);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public static int NumberWordToInt(string word)
|
||||
{
|
||||
int? retVal = null;
|
||||
if (word == "one")
|
||||
{
|
||||
retVal = 1;
|
||||
}
|
||||
else if (word == "two")
|
||||
{
|
||||
retVal = 2;
|
||||
}
|
||||
else if (word == "three")
|
||||
{
|
||||
retVal = 3;
|
||||
}
|
||||
else if (word == "four")
|
||||
{
|
||||
retVal = 4;
|
||||
}
|
||||
else if (word == "five")
|
||||
{
|
||||
retVal = 5;
|
||||
}
|
||||
else if (word == "six")
|
||||
{
|
||||
retVal = 6;
|
||||
}
|
||||
else if (word == "seven")
|
||||
{
|
||||
retVal = 7;
|
||||
}
|
||||
else if (word == "eight")
|
||||
{
|
||||
retVal = 8;
|
||||
}
|
||||
else if (word == "nine")
|
||||
{
|
||||
retVal = 9;
|
||||
}
|
||||
return retVal == null ? default(int): retVal.Value;
|
||||
}
|
||||
}
|
||||
}
|
10
2023/day01/day01.csproj
Normal file
10
2023/day01/day01.csproj
Normal file
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
1000
2023/day01/input.txt
Normal file
1000
2023/day01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
6
2023/day01/testinput.txt
Normal file
6
2023/day01/testinput.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
2qlljdqcbeight
|
||||
eight47srvbfive
|
||||
slconeightfoureight557m38
|
||||
xvqeightwosixnine61eightsn2tdczfhx
|
||||
msixonexch1twokjbdlhchqk1
|
||||
112ninejlhhjmjzkzgdsix
|
Loading…
Reference in a new issue