Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day05 #6

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
420 changes: 420 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions y.2023/.idea/.idea.AoC_2023/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions y.2023/.idea/.idea.AoC_2023/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions y.2023/.idea/.idea.AoC_2023/.idea/projectSettingsUpdater.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions y.2023/.idea/.idea.AoC_2023/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions y.2023/AoC_2023.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day01", "Day01\Day01.csproj", "{1B746D77-79D5-4C47-8E72-D300F1883E9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day02", "Day02\Day02.csproj", "{EAFB72C3-97D5-40ED-919E-8C444D3E0BDD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day03", "Day03\Day03.csproj", "{17172171-086A-4858-97F7-DF9833B56A3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day04", "Day04\Day04.csproj", "{9F89D6F5-EBD5-4377-89B8-9CDC9DAC5858}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day05", "Day05\Day05.csproj", "{B7EFE93E-5A41-419A-A9C3-0344E974CD22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1B746D77-79D5-4C47-8E72-D300F1883E9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B746D77-79D5-4C47-8E72-D300F1883E9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B746D77-79D5-4C47-8E72-D300F1883E9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B746D77-79D5-4C47-8E72-D300F1883E9D}.Release|Any CPU.Build.0 = Release|Any CPU
{EAFB72C3-97D5-40ED-919E-8C444D3E0BDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAFB72C3-97D5-40ED-919E-8C444D3E0BDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAFB72C3-97D5-40ED-919E-8C444D3E0BDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EAFB72C3-97D5-40ED-919E-8C444D3E0BDD}.Release|Any CPU.Build.0 = Release|Any CPU
{17172171-086A-4858-97F7-DF9833B56A3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17172171-086A-4858-97F7-DF9833B56A3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17172171-086A-4858-97F7-DF9833B56A3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17172171-086A-4858-97F7-DF9833B56A3E}.Release|Any CPU.Build.0 = Release|Any CPU
{9F89D6F5-EBD5-4377-89B8-9CDC9DAC5858}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F89D6F5-EBD5-4377-89B8-9CDC9DAC5858}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F89D6F5-EBD5-4377-89B8-9CDC9DAC5858}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F89D6F5-EBD5-4377-89B8-9CDC9DAC5858}.Release|Any CPU.Build.0 = Release|Any CPU
{B7EFE93E-5A41-419A-A9C3-0344E974CD22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7EFE93E-5A41-419A-A9C3-0344E974CD22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7EFE93E-5A41-419A-A9C3-0344E974CD22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7EFE93E-5A41-419A-A9C3-0344E974CD22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions y.2023/Day01/Day01.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
4 changes: 4 additions & 0 deletions y.2023/Day01/InputExample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
20 changes: 20 additions & 0 deletions y.2023/Day01/InputExample_Carles.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
2hdivyjone42
7pqrstsixteen
v4
one
two
three
four
five
six
seven
eight
nine
twoone
twone
eightwo
nineight
eighthree
nineeight
eeeight
oooneeone
7 changes: 7 additions & 0 deletions y.2023/Day01/InputExample_Part2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
115 changes: 115 additions & 0 deletions y.2023/Day01/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
namespace Day01;

internal abstract class Program
{
private static void Main(string[] args)
{
try
{
if (args.Length < 1)
{
Console.WriteLine("No calibration input file provided. Please provide one.");
return;
}

var filePath = args[0];

if (!File.Exists(filePath))
{
Console.WriteLine($"File not found at path: {filePath}");
return;
}

var calibratorLine = File.ReadAllLines(filePath);

var calibrationValue = calibratorLine.Sum(GetCalibration);

Console.WriteLine($"The Calibration value is: {calibrationValue}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}

private static int GetCalibration(string lineCalibration)
{
var numbersFormattedText = GetAllNumbers(lineCalibration);

var firstDigit = numbersFormattedText.FirstOrDefault();
var lastDigit = numbersFormattedText.LastOrDefault();

var calibrationValue = firstDigit ?? firstDigit;

if (numbersFormattedText.Count > 1)
{
calibrationValue += lastDigit ?? lastDigit;
}

var isResultParsedCorrectly = int.TryParse(calibrationValue, out var result);

Console.WriteLine($"The encrypted calibration line {lineCalibration} has a calibration value of: {result}");

return isResultParsedCorrectly ? result : 0;
}

private static List<string> GetAllNumbers(string lineCalibration)
{
var spelledNumbers = new List<string>
{
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};

var allowedDigits = new List<string>
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
};

var numbersFound = spelledNumbers
.SelectMany(number =>
lineCalibration
.Select((c, index) => new { Char = c, Index = index })
.Where(item => lineCalibration.IndexOf(number, item.Index, StringComparison.OrdinalIgnoreCase) == item.Index)
.Select(item => new { Number = number, PositionOnText = item.Index })
)
.Where(x => x.PositionOnText != -1)
.ToList();

var digitsFound = allowedDigits
.SelectMany(number =>
lineCalibration
.Select((c, index) => new { Char = c, Index = index })
.Where(item => lineCalibration.IndexOf(number, item.Index, StringComparison.OrdinalIgnoreCase) == item.Index)
.Select(item => new { Number = number, PositionOnText = item.Index })
)
.Where(x => x.PositionOnText != -1)
.ToList();

var allNumbers = numbersFound.Concat(digitsFound)
.OrderBy(x => x.PositionOnText)
.Select(x => x.Number)
.ToList();

// Repeat Digit When Only One Exists
if (allNumbers.Count == 1 && digitsFound.Count == 1)
{
allNumbers.Add(digitsFound.FirstOrDefault().Number);
}

ReplaceNumbersToDigits(allNumbers, spelledNumbers);

return allNumbers;
}

private static void ReplaceNumbersToDigits(List<string> allNumbers, List<string> spelledNumbers)
{
for (var i = 0; i < allNumbers.Count; i++)
{
var number = spelledNumbers.IndexOf(allNumbers[i]);
if (number > 0)
{
allNumbers[i] = number.ToString();
}
}
}
}
Binary file added y.2023/Day01/README.md
Binary file not shown.
Loading