Skip to content

Commit 815a5e9

Browse files
committed
2024 day 2 part 2 improved efficiency slightly
1 parent fd7dfcd commit 815a5e9

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

2024/AdventOfCode2024/Days/Day1.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Frozen;
2-
using System.Collections.Immutable;
31
using AdventOfCode2024.Lib;
42

53
namespace AdventOfCode2024.Days;

2024/AdventOfCode2024/Days/Day2.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Frozen;
2-
using System.Collections.Immutable;
31
using AdventOfCode2024.Lib;
42

53
namespace AdventOfCode2024.Days;
@@ -88,8 +86,22 @@ public string Part1(string input)
8886

8987
private ReportResult AnalyseReportWithDampening(ReadOnlySpan<char> report)
9088
{
91-
foreach (var ignoreIndex in new int?[] { null, 0, 1, 2, 3, 4, 5, 6, 7})
89+
var (result, failedIndex) = AnalyseReport(report);
90+
91+
if (result == ReportResult.Safe)
92+
{
93+
return ReportResult.Safe;
94+
}
95+
96+
var ignoreIndexes = new int[] { (int)(failedIndex - 1)!, (int)failedIndex!, (int)(failedIndex + 1) };
97+
98+
foreach (var ignoreIndex in ignoreIndexes)
9299
{
100+
if (ignoreIndex < 0 || ignoreIndex > 7)
101+
{
102+
continue;
103+
}
104+
93105
if (AnalyseReport(report, ignoreIndex) == (ReportResult.Safe, null))
94106
{
95107
return ReportResult.Safe;

0 commit comments

Comments
 (0)