aoc2023/AdventOfCode/Day01.cs
2023-12-05 20:14:41 +00:00

103 lines
4.0 KiB
C#

using System.Collections;
using System.Text;
namespace AdventOfCode;
public class Day01 : BaseDay
{
private readonly string _input;
private int total;
private int total2;
public Day01()
{
_input = File.ReadAllText(InputFilePath);
Part1();
Part2();
}
private void Part1()
{
StringReader reader = new(_input); //read text file
string line = reader.ReadLine(); //read first line
while(line != null) //loop thru file
{
StringBuilder build = new();
string num = ""; //container for ints
foreach(char c in line)
{
if(char.IsDigit(c)) //store each int found in each line
num += c;
}
line = reader.ReadLine(); //go to next line
if(num == "") //don't append anything if no ints were found
continue;
build.Append(num[0]); //take first digit and put into string
build.Append(num[^1]); //take second digit and put together with first int
total += int.Parse(build.ToString()); //add the resulting two digits to the total
}
}
private void Part2()
{
StringReader reader = new(_input);
string line = reader.ReadLine();
string[] word2num = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
//int j = 0;
while(line != null)
{
StringBuilder build = new();
StringBuilder num = new();
ArrayList nI = new();
for (int i = 0; i < line.Length; i++) //look for numbers on line but only ACTUAL digits (not words)
{
char c = line.ElementAt(i);
if(char.IsDigit(c))
{
num.Append(c); //add any digits found to num string
//numInd.Append(i); //record index of those digits
nI.Add(i);
}
}
int earlyNum = (int)Char.GetNumericValue(num.ToString().ElementAt(0)); //store earliest num found
int lateNum = (int)Char.GetNumericValue(num.ToString().ElementAt(^1)); //store latest num found
int earlyNumInd = (int)nI[0]; //store index of earliest num
int lateNumInd = (int)nI[^1]; //store index of latest num
foreach (string number in word2num) //look for numbers as words on line (one, two, etc.)
{
int currInd = 0;
while(line.IndexOf(number, currInd) != -1) //while the number can still be found on the line...
{
if(line.IndexOf(number, currInd) < earlyNumInd) //if the number comes before the earliest number
{
earlyNumInd = line.IndexOf(number, currInd); //replace early number index
earlyNum = Array.IndexOf(word2num, number) + 1; //replace early number
}
if(line.IndexOf(number, currInd) > lateNumInd) //if the number comes after the latest number
{
lateNumInd = line.IndexOf(number, currInd); //replace the latest number index
lateNum = Array.IndexOf(word2num, number) + 1; //replace latest number
}
currInd = line.IndexOf(number, currInd) + number.Length; //store current index, continue moving through the line
}
//check for next number
}
line = reader.ReadLine();
build.Append(earlyNum);
build.Append(lateNum);
if(build.ToString() == "")
continue;
total2 += int.Parse(build.ToString());
// Console.WriteLine("Combined numbers: "+build.ToString());
// Console.WriteLine(total2);
//j++;
}
}
public override ValueTask<string> Solve_1() => new(total.ToString());
public override ValueTask<string> Solve_2() => new(total2.ToString());
}