Initial Commit
This commit is contained in:
14
2023/Day01CSharp/Day01CSharp.csproj
Normal file
14
2023/Day01CSharp/Day01CSharp.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdventCommon\AdventCommon.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
129
2023/Day01CSharp/Program.cs
Normal file
129
2023/Day01CSharp/Program.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using AdventCommon;
|
||||
|
||||
/** P1 **/
|
||||
var p1exampleSln = Part01(new AdventCommon.PuzzleInput("example-input.txt"));
|
||||
var p1puzzleSln = Part01(new AdventCommon.PuzzleInput("puzzle-input.txt"));
|
||||
|
||||
Console.WriteLine("Part01 Example Sum: " + p1exampleSln);
|
||||
Console.WriteLine("Part01 Puzzle Sum: " + p1puzzleSln);
|
||||
|
||||
/** P2 **/
|
||||
var p2exampleSln = Part02(new AdventCommon.PuzzleInput("example-input-2.txt"));
|
||||
var p2puzzleSln = Part02(new AdventCommon.PuzzleInput("puzzle-input.txt"));
|
||||
|
||||
Console.WriteLine("Part02 Example Sum: " + p2exampleSln);
|
||||
Console.WriteLine("Part02 Puzzle Sum: " + p2puzzleSln);
|
||||
|
||||
int Part01(PuzzleInput input)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
foreach (var line in input.Lines)
|
||||
{
|
||||
int num = 0;
|
||||
|
||||
// Left digit
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
if (Char.IsDigit(line[i]))
|
||||
{
|
||||
num += 10 * (line[i] - '0');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Right digit
|
||||
for (int i = line.Length-1; i >= 0; i--)
|
||||
{
|
||||
if (Char.IsDigit(line[i]))
|
||||
{
|
||||
num += line[i] - '0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sum += num;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int Part02(PuzzleInput input)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
foreach (var line in input.Lines)
|
||||
{
|
||||
int num = 0;
|
||||
|
||||
// Left digit
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
var val = ReadInteger(line, i);
|
||||
|
||||
if (val != -1)
|
||||
{
|
||||
num += 10 * val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Right digit
|
||||
for (int i = line.Length - 1; i >= 0; i--)
|
||||
{
|
||||
var val = ReadInteger(line, i);
|
||||
|
||||
if (val != -1)
|
||||
{
|
||||
num += val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sum += num;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int ReadInteger(string line, int index)
|
||||
{
|
||||
// If its a digit, we done
|
||||
if (Char.IsDigit(line[index]))
|
||||
{
|
||||
return line[index] - '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ok read string
|
||||
int number_index = 1;
|
||||
var number = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
|
||||
|
||||
foreach (var num in number)
|
||||
{
|
||||
if (index + num.Length <= line.Length)
|
||||
{
|
||||
bool found = true;
|
||||
|
||||
for (int i = 0; i < num.Length && (index + i) < line.Length; i++)
|
||||
{
|
||||
if (num[i] != line[index + i])
|
||||
{
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
return number_index;
|
||||
}
|
||||
}
|
||||
|
||||
number_index++;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
8
2023/Day01CSharp/Properties/launchSettings.json
Normal file
8
2023/Day01CSharp/Properties/launchSettings.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Day01CSharp": {
|
||||
"commandName": "Project",
|
||||
"workingDirectory": "C:\\dev\\DevSandbox\\AdventOfCode\\2023\\Day01CSharp"
|
||||
}
|
||||
}
|
||||
}
|
||||
7
2023/Day01CSharp/example-input-2.txt
Normal file
7
2023/Day01CSharp/example-input-2.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
4
2023/Day01CSharp/example-input.txt
Normal file
4
2023/Day01CSharp/example-input.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
1000
2023/Day01CSharp/puzzle-input.txt
Normal file
1000
2023/Day01CSharp/puzzle-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user