Completed day 01
This commit is contained in:
parent
2ad0676c49
commit
b2d8d4fc40
@ -1,17 +1,19 @@
|
||||
using System.Text;
|
||||
|
||||
using AdventOfCodeLibrary._2022;
|
||||
using AdventOfCodeLibrary._2023;
|
||||
using AdventOfCodeLibrary.Shared;
|
||||
|
||||
string _demoData = @"Sabqponm
|
||||
abcryxxl
|
||||
accszExk
|
||||
acctuvwj
|
||||
abdefghi";
|
||||
string _demoData = @"two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen";
|
||||
|
||||
Answerable answerable = new Day12Part1();
|
||||
Answerable answerable = new Day01Part2();
|
||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||
dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||
|
||||
Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}");
|
||||
Console.ReadKey(true);
|
||||
32
Advent Of Code Library/2023/Day 01/Day01Part1.cs
Normal file
32
Advent Of Code Library/2023/Day 01/Day01Part1.cs
Normal file
@ -0,0 +1,32 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
Advent Of Code Library/2023/Day 01/Day01Part2.cs
Normal file
79
Advent Of Code Library/2023/Day 01/Day01Part2.cs
Normal file
@ -0,0 +1,79 @@
|
||||
namespace AdventOfCodeLibrary._2023
|
||||
{
|
||||
using AdventOfCodeLibrary.Shared;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
public class Day01Part2 : Answerable
|
||||
{
|
||||
public override int Year { get; set; } = 2023;
|
||||
public override int Day { get; set; } = 1;
|
||||
public override int Part { get; set; } = 2;
|
||||
|
||||
public override string GetAnswer(byte[] data)
|
||||
{
|
||||
return GetAsStringArray(data)
|
||||
.Select(GetValueFromLine)
|
||||
.Sum()
|
||||
.ToString();
|
||||
}
|
||||
|
||||
private int GetValueFromLine(string line)
|
||||
{
|
||||
MatchCollection matchCollection = Regex.Matches(line, @"(\d)");
|
||||
List<Match> captures = matchCollection.ToList();
|
||||
|
||||
// replace the text with numbers
|
||||
captures.AddRange(Regex.Matches(line, @"(one)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(two)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(three)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(four)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(five)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(six)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(seven)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(eight)"));
|
||||
captures.AddRange(Regex.Matches(line, @"(nine)"));
|
||||
captures = captures.OrderBy(x => x.Index).ToList();
|
||||
|
||||
// merge first and last index as one
|
||||
string values = GetStringAsNumber(captures.First().Value) + GetStringAsNumber(captures.Last().Value);
|
||||
// make an int
|
||||
return int.Parse(values);
|
||||
}
|
||||
|
||||
private string GetStringAsNumber(string value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case "one":
|
||||
case "1":
|
||||
return "1";
|
||||
case "two":
|
||||
case "2":
|
||||
return "2";
|
||||
case "three":
|
||||
case "3":
|
||||
return "3";
|
||||
case "four":
|
||||
case "4":
|
||||
return "4";
|
||||
case "five":
|
||||
case "5":
|
||||
return "5";
|
||||
case "six":
|
||||
case "6":
|
||||
return "6";
|
||||
case "seven":
|
||||
case "7":
|
||||
return "7";
|
||||
case "eight":
|
||||
case "8":
|
||||
return "8";
|
||||
case "nine":
|
||||
case "9":
|
||||
return "9";
|
||||
default:
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1000
Advent Of Code Library/2023/Day 01/day-01-input.txt
Normal file
1000
Advent Of Code Library/2023/Day 01/day-01-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user