Completed day 2
This commit is contained in:
parent
2c0ba40a24
commit
9e3c218ffe
@ -18,76 +18,47 @@ namespace AdventOfCode.Solutions._2024
|
||||
}
|
||||
|
||||
//2
|
||||
//
|
||||
public async Task<string> GetSolutionPart1()
|
||||
{
|
||||
//_inputReader.SetSampleInput(true);
|
||||
string[] data = await _inputReader.ReadAsArrayString();
|
||||
int save = data.Length;
|
||||
foreach (string line in data)
|
||||
{
|
||||
int[] intData = line.Split(' ').Select(int.Parse).ToArray();
|
||||
bool isIncrease = intData[1] < intData[0];
|
||||
for (int index = 1; index < intData.Length; index++)
|
||||
{
|
||||
int trueDiff = intData[index - 1] - intData[index];
|
||||
int diff = Math.Abs(trueDiff);
|
||||
if (diff < 1 || diff > 3)
|
||||
{
|
||||
save--;
|
||||
break;
|
||||
}
|
||||
//230
|
||||
public async Task<string> GetSolutionPart1()
|
||||
=> (await _inputReader.ReadAsArrayString())
|
||||
.Select(ParselineToReports)
|
||||
.Where(reports => AreReportsSave(reports, false))
|
||||
.Count()
|
||||
.ToString();
|
||||
|
||||
if (isIncrease && trueDiff < 0)
|
||||
{
|
||||
save--;
|
||||
break;
|
||||
}
|
||||
else if (!isIncrease && trueDiff > 0)
|
||||
{
|
||||
save--;
|
||||
break;
|
||||
}
|
||||
//4
|
||||
//301
|
||||
public async Task<string> GetSolutionPart2() => (await _inputReader.ReadAsArrayString())
|
||||
.Select(ParselineToReports)
|
||||
.Where(reports => AreReportsSave(reports, true))
|
||||
.Count()
|
||||
.ToString();
|
||||
|
||||
private static bool AreReportsSave(int[] reports, bool tolarance = false)
|
||||
{
|
||||
bool isIncrease = reports[1] < reports[0];
|
||||
for (int index = 1; index < reports.Length; index++)
|
||||
{
|
||||
int trueDiff = reports[index - 1] - reports[index];
|
||||
int diff = Math.Abs(trueDiff);
|
||||
if (diff < 1 || diff > 3)
|
||||
{
|
||||
return tolarance && HasSafeCombo(reports);
|
||||
}
|
||||
|
||||
if ((isIncrease && trueDiff < 0) || (!isIncrease && trueDiff > 0))
|
||||
{
|
||||
return tolarance && HasSafeCombo(reports);
|
||||
}
|
||||
}
|
||||
|
||||
return save.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
string[] data = await _inputReader.ReadAsArrayString();
|
||||
int save = data.Length;
|
||||
foreach (string line in data)
|
||||
{
|
||||
int[] intData = line.Split(' ').Select(int.Parse).ToArray();
|
||||
bool isIncrease = intData[1] < intData[0];
|
||||
for (int index = 1; index < intData.Length; index++)
|
||||
{
|
||||
int trueDiff = intData[index - 1] - intData[index];
|
||||
int diff = Math.Abs(trueDiff);
|
||||
if (diff < 1 || diff > 3)
|
||||
{
|
||||
save--;
|
||||
break;
|
||||
}
|
||||
private static bool HasSafeCombo(int[] reports)
|
||||
=> reports.Select((_, i) => SkipReport(reports, i)).Any(reports => AreReportsSave(reports));
|
||||
|
||||
if (isIncrease && trueDiff < 0)
|
||||
{
|
||||
save--;
|
||||
break;
|
||||
}
|
||||
else if (!isIncrease && trueDiff > 0)
|
||||
{
|
||||
save--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return save.ToString();
|
||||
}
|
||||
private static int[] SkipReport(int[] reports, int index) => reports.Where((_, i) => i != index).ToArray();
|
||||
private static int[] ParselineToReports(string line) => line.Split(' ').Select(int.Parse).ToArray();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user