AdventOfCode2023/AdvendOfCode2023/Day1.cs
2023-12-06 20:54:25 +01:00

130 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvendOfCode2023
{
public class Day1
{
List<string> input = FileReader.Input("day1");
public int StringCheck(string input)
{
int first = 0;
int second = 0;
for (int i = 0; i < input.Length; i++)
{
int result;
{
if (int.TryParse(input[i].ToString(), out result) && first == 0)
{
first = result;
}
else if (int.TryParse(input[i].ToString(), out result) && first > 0)
{
second = result;
}
}
}
if (second == 0)
{
second = first;
}
var concat = first.ToString() + second.ToString();
return Convert.ToInt32(concat);
}
public int StringCheckPartTwo(string input)
{
if (input.Contains("one"))
{
input = input.Replace("one", "o1ne");
}
if(input.Contains("two"))
{
input = input.Replace("two", "t2wo");
}
if (input.Contains("three"))
{
input = input.Replace("three", "t3hree");
}
if (input.Contains("four"))
{
input = input.Replace("four", "f4our");
}
if (input.Contains("five"))
{
input = input.Replace("five", "f5ive");
}
if (input.Contains("six"))
{
input = input.Replace("six", "s6ix");
}
if (input.Contains("seven"))
{
input = input.Replace("seven", "s7even");
}
if (input.Contains("eight"))
{
input = input.Replace("eight", "e8ight");
}
if (input.Contains("nine"))
{
input = input.Replace("nine", "n9ine");
}
int first = 0;
int second = 0;
for (int i = 0; i < input.Length; i++)
{
int result;
{
if (int.TryParse(input[i].ToString(), out result) && first == 0)
{
first = result;
}
else if (int.TryParse(input[i].ToString(), out result) && first > 0)
{
second = result;
}
}
}
if (second == 0)
{
second = first;
}
var concat = first.ToString() + second.ToString();
return Convert.ToInt32(concat);
}
public void Day1PartOne()
{
int result = 0;
foreach (string inputItem in input)
{
result += StringCheck(inputItem);
}
Console.WriteLine("Part one: " + result);
}
public void Day1PartTwo()
{
int result = 0;
foreach (string inputItem in input)
{
result += StringCheckPartTwo(inputItem);
}
Console.WriteLine("Part two: " + result);
}
}
}