Skip to content

Commit fd7dfcd

Browse files
committed
2024 day 2 part 2 complete
1 parent 493f3cf commit fd7dfcd

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

2024/AdventOfCode2024.Tests/Day2Tests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ public void Part1_Sample_IsCorrect()
2121

2222
Assert.Equal("2", result);
2323
}
24+
25+
[Fact]
26+
public void Part2_Sample_IsCorrect()
27+
{
28+
var day = new Day2();
29+
var result = day.Part2(ExampleInput);
30+
31+
Assert.Equal("4", result);
32+
}
2433
}

2024/AdventOfCode2024.sln.DotSettings.user

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEnumerable_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Ffcf0ec63a5154dda96b25a127f4b25649d400_003F7f_003F7f9cb70a_003FEnumerable_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
33
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANumber_002EParsing_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F663a09bb4626f9a993344296223a5407b8ccd7fba6d13c4abbf5b7161bbd1f_003FNumber_002EParsing_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
4+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AReadOnlySpan_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd0d96f40fdd9bc395fad59e4a8a6539e4eecce4865809ef4be1a473f911eceaa_003FReadOnlySpan_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
45
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AString_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F22a6757d52404266b970fa51436fd5a3e24e00_003F6b_003F0d56d9c1_003FString_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
5-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=706dc741_002D2c60_002D4647_002D868b_002D68cfbf1b3b5a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Part1_Sample_IsCorrect #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
6+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=706dc741_002D2c60_002D4647_002D868b_002D68cfbf1b3b5a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Part1_Sample_IsCorrect #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
67
&lt;TestAncestor&gt;
78
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day1Tests.Part1_Sample_IsCorrect&lt;/TestId&gt;
89
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day1Tests.Part2_Sample_IsCorrect&lt;/TestId&gt;
910
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day2Tests.Part1_Sample_IsCorrect&lt;/TestId&gt;
11+
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day2Tests.Part2_Sample_IsCorrect&lt;/TestId&gt;
12+
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day2Tests&lt;/TestId&gt;
1013
&lt;/TestAncestor&gt;
1114
&lt;/SessionState&gt;</s:String>
1215
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=89c8c185_002D52f0_002D4e30_002Da90e_002D0d2394c51e03/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Part1_Sample_IsCorrect" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
@@ -23,4 +26,9 @@
2326
&lt;TestAncestor&gt;
2427
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day1Tests.Part1_Sample_IsCorrect&lt;/TestId&gt;
2528
&lt;/TestAncestor&gt;
29+
&lt;/SessionState&gt;</s:String>
30+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=f098b2c7_002D4678_002D461b_002D9daf_002D30b8b2cd8316/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Part2_Sample_IsCorrect" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
31+
&lt;TestAncestor&gt;
32+
&lt;TestId&gt;xUnit::3622859B-381B-48D3-B71A-932824B5A500::net9.0::AdventOfCode2024.Tests.Day2Tests&lt;/TestId&gt;
33+
&lt;/TestAncestor&gt;
2634
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>

2024/AdventOfCode2024/Days/Day2.cs

+43-26
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public string Part1(string input)
2828

2929
foreach (var lineRange in lines)
3030
{
31-
if (AnalyseReport(inputSpan[lineRange]) == ReportResult.Safe)
31+
var line = inputSpan[lineRange];
32+
33+
if (AnalyseReport(line) == (ReportResult.Safe, null))
3234
{
3335
safeCount++;
3436
}
@@ -37,53 +39,66 @@ public string Part1(string input)
3739
return safeCount.ToString();
3840
}
3941

40-
private ReportResult AnalyseReport(ReadOnlySpan<char> report, bool withDampener = false)
42+
private (ReportResult result, int? failedIndex) AnalyseReport(ReadOnlySpan<char> report, int? ignoreIndex = null)
4143
{
4244
var values = report.Split(" ");
4345
var currentDirection = Direction.Unknown;
46+
int currentIndex = 0;
4447
int? lastValue = null;
45-
bool dampenerTriggered = !withDampener; // Consider the dampener triggered if there is no dampener
4648

4749
foreach (var valueRange in values)
4850
{
4951
int currentValue = Number.FastParseInt(report[valueRange]);
5052

51-
if (lastValue is null)
53+
if (lastValue is null && ignoreIndex != currentIndex)
5254
{
5355
lastValue = currentValue;
56+
currentIndex++;
5457
continue; // Skip the first value
5558
}
5659

57-
int change = Math.Abs((int)(currentValue - lastValue));
58-
var pendingResult = ReportResult.Safe;
59-
60-
if (change < 1 || change > 3)
60+
if (currentIndex != ignoreIndex)
6161
{
62-
pendingResult = ReportResult.Unsafe;
62+
int change = Math.Abs((int)(currentValue - lastValue)!);
63+
64+
if (change < 1 || change > 3)
65+
{
66+
return (ReportResult.Unsafe, currentIndex);
67+
}
68+
69+
var nextDirection = currentValue > lastValue ? Direction.Ascending : Direction.Descending;
70+
71+
if (currentDirection == Direction.Unknown)
72+
{
73+
currentDirection = nextDirection;
74+
}
75+
else if (currentDirection != nextDirection)
76+
{
77+
return (ReportResult.Unsafe, currentIndex);
78+
}
79+
80+
lastValue = currentValue;
6381
}
82+
83+
currentIndex++;
84+
}
6485

65-
var nextDirection = currentValue > lastValue ? Direction.Ascending : Direction.Descending;
66-
67-
if (currentDirection == Direction.Unknown)
68-
{
69-
currentDirection = nextDirection;
70-
}
71-
else if (currentDirection != nextDirection)
72-
{
73-
pendingResult = ReportResult.Unsafe;
74-
}
86+
return (ReportResult.Safe, null);
87+
}
7588

76-
if (pendingResult == ReportResult.Unsafe && dampenerTriggered)
89+
private ReportResult AnalyseReportWithDampening(ReadOnlySpan<char> report)
90+
{
91+
foreach (var ignoreIndex in new int?[] { null, 0, 1, 2, 3, 4, 5, 6, 7})
92+
{
93+
if (AnalyseReport(report, ignoreIndex) == (ReportResult.Safe, null))
7794
{
78-
return pendingResult;
95+
return ReportResult.Safe;
7996
}
80-
81-
lastValue = currentValue;
8297
}
8398

84-
return ReportResult.Safe;
99+
return ReportResult.Unsafe;
85100
}
86-
101+
87102
public string Part2(string input)
88103
{
89104
var inputSpan = input.AsSpan();
@@ -93,7 +108,9 @@ public string Part2(string input)
93108

94109
foreach (var lineRange in lines)
95110
{
96-
if (AnalyseReport(inputSpan[lineRange], true) == ReportResult.Safe)
111+
var line = inputSpan[lineRange];
112+
113+
if (AnalyseReportWithDampening(line) == ReportResult.Safe)
97114
{
98115
safeCount++;
99116
}

0 commit comments

Comments
 (0)