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