AdventOfCode/Advent Of Code Library/2023/Day 01/Day01Part1.cs
2023-12-01 09:08:47 +01:00

33 lines
908 B
C#

namespace AdventOfCodeLibrary._2023
{
using AdventOfCodeLibrary.Shared;
using System.Text.RegularExpressions;
public class Day01Part1 : Answerable
{
public override int Year { get; set; } = 2023;
public override int Day { get; set; } = 1;
public override int Part { get; set; } = 1;
public override string GetAnswer(byte[] data)
{
return GetAsStringArray(data)
.Select(GetValueFromLine)
.Sum()
.ToString();
}
private int GetValueFromLine(string line)
{
// get all the numers
MatchCollection collection = Regex.Matches(line, @"\d");
// merge first and last index as one
string values = collection[0].Value + collection[^1].Value;
// make an int
return int.Parse(values);
}
}
}