61 lines
No EOL
1.6 KiB
C#
61 lines
No EOL
1.6 KiB
C#
using AdventOfCode2025.Inputs;
|
|
|
|
namespace AdventOfCode2025;
|
|
|
|
public class Day3 : Solution
|
|
{
|
|
private readonly Input input = new(3);
|
|
|
|
public override string SolvePart1() => input.GetLines(x => ParseLine(x, 2)).Sum().ToString();
|
|
|
|
public override string SolvePart2() => input.GetLines(x => ParseLine(x, 12)).Sum().ToString();
|
|
|
|
private static long GetLargestJolts(int[] bank, int numberOfJolts = 2)
|
|
{
|
|
var total = 0L;
|
|
var from = 0;
|
|
int to = bank.Length - numberOfJolts;
|
|
for (int i = numberOfJolts - 1; i >= 0; i--)
|
|
{
|
|
int maxIndexI = GetIndexOfMax(bank, from, to);
|
|
total += bank[maxIndexI] * (long)Math.Pow(10, i);
|
|
from = maxIndexI + 1;
|
|
to = bank.Length - i;
|
|
}
|
|
|
|
return total;
|
|
}
|
|
|
|
// private int GetLargestJolts(int[] bank)
|
|
// {
|
|
// var maxIndex10 = GetIndexOfMax(bank, 0, bank.Length-1);
|
|
// var maxIndex1 = GetIndexOfMax(bank, maxIndex10+1, bank.Length);
|
|
// var t = bank[maxIndex10] * 10 + bank[maxIndex1];
|
|
// return t;
|
|
// }
|
|
|
|
private static int GetIndexOfMax(int[] array, int from, int to)
|
|
{
|
|
int maxIndex = from;
|
|
if (from + 1 > to)
|
|
{
|
|
return maxIndex;
|
|
}
|
|
|
|
for (int i = from + 1; i <= to; i++)
|
|
{
|
|
if (array[i] > array[maxIndex])
|
|
{
|
|
maxIndex = i;
|
|
}
|
|
}
|
|
|
|
return maxIndex;
|
|
}
|
|
|
|
private static long ParseLine(string line, int numberOfJolts)
|
|
{
|
|
int[] bank = line.Select(c => c - '0').ToArray();
|
|
return GetLargestJolts(bank, numberOfJolts);
|
|
}
|
|
} |