[2023] Update Day1
Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
8c20f2ce7e
commit
0ddb0fd959
103
Year2023/Day1.cs
103
Year2023/Day1.cs
|
@ -2,80 +2,55 @@ namespace AdventOfCode.Year2023;
|
|||
|
||||
public class Day1
|
||||
{
|
||||
private static List<string> Input = File.ReadAllLines("inputs/Year2023/day1.txt").ToList();
|
||||
private static int FirstNum;
|
||||
private static int LastNum;
|
||||
private static string[] Input = File.ReadAllLines("inputs/Year2023/day1.txt");
|
||||
|
||||
private static Dictionary<string, int> Part1Numbers = new Dictionary<string, int>() {
|
||||
{"1", 1},
|
||||
{"2", 2},
|
||||
{"3", 3},
|
||||
{"4", 4},
|
||||
{"5", 5},
|
||||
{"6", 6},
|
||||
{"7", 7},
|
||||
{"8", 8},
|
||||
{"9", 9},
|
||||
};
|
||||
|
||||
private static Dictionary<string, int> Part2Numbers = new Dictionary<string, int>(Part1Numbers) {
|
||||
{"one", 1},
|
||||
{"two", 2},
|
||||
{"three", 3},
|
||||
{"four", 4},
|
||||
{"five", 5},
|
||||
{"six", 6},
|
||||
{"seven", 7},
|
||||
{"eight", 8},
|
||||
{"nine", 9},
|
||||
};
|
||||
|
||||
public static void Run()
|
||||
{
|
||||
Console.WriteLine($@"
|
||||
Day1 Solution
|
||||
Part1 Result: {Part1()}
|
||||
Part2 Result: {Part2()}
|
||||
Part1 Result: {Solve(Part1Numbers)}
|
||||
Part2 Result: {Solve(Part2Numbers)}
|
||||
|
||||
=============================");
|
||||
}
|
||||
|
||||
private static int Part1()
|
||||
{
|
||||
List<int> numbers = new List<int>();
|
||||
Input.ForEach(line =>
|
||||
private static int Solve(Dictionary<string, int> numbers)
|
||||
=> Input.Select(line =>
|
||||
{
|
||||
List<char> chars = line.ToList();
|
||||
numbers.Add(Convert.ToInt32($"{chars.First(c => Char.IsDigit(c))}{chars.Last(c => Char.IsDigit(c))}"));
|
||||
});
|
||||
int first = numbers.Select(num => (index: line.IndexOf(num.Key), val: num.Value))
|
||||
.Where(num => num.index >= 0)
|
||||
.MinBy(num => num.index)
|
||||
.val;
|
||||
|
||||
return numbers.Sum();
|
||||
}
|
||||
int last = numbers.Select(num => (index: line.LastIndexOf(num.Key), val: num.Value))
|
||||
.Where(num => num.index >= 0)
|
||||
.MaxBy(num => num.index)
|
||||
.val;
|
||||
|
||||
private static int Part2()
|
||||
{
|
||||
string[] validStrNums = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
|
||||
|
||||
List<int> numbers = new List<int>();
|
||||
Input.ForEach(line =>
|
||||
{
|
||||
int min = line.Length;
|
||||
int max = -1;
|
||||
foreach (var str in validStrNums)
|
||||
{
|
||||
if (!line.Contains(str)) continue;
|
||||
|
||||
int firstIndex = line.IndexOf(str);
|
||||
if (firstIndex < min)
|
||||
{
|
||||
min = firstIndex;
|
||||
FirstNum = ParseInt(str);
|
||||
}
|
||||
|
||||
int lastIndex = line.LastIndexOf(str);
|
||||
if (lastIndex > max)
|
||||
{
|
||||
max = lastIndex;
|
||||
LastNum = ParseInt(str);
|
||||
}
|
||||
}
|
||||
|
||||
numbers.Add(Convert.ToInt32($"{FirstNum}{LastNum}"));
|
||||
});
|
||||
|
||||
return numbers.Sum();
|
||||
}
|
||||
|
||||
private static int ParseInt(string str)
|
||||
{
|
||||
return (str) switch
|
||||
{
|
||||
"one" => 1,
|
||||
"two" => 2,
|
||||
"three" => 3,
|
||||
"four" => 4,
|
||||
"five" => 5,
|
||||
"six" => 6,
|
||||
"seven" => 7,
|
||||
"eight" => 8,
|
||||
"nine" => 9,
|
||||
_ => Convert.ToInt32(str),
|
||||
};
|
||||
}
|
||||
return first * 10 + last;
|
||||
}).Sum();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue