Skip to content

Commit acf1229

Browse files
author
Stéphane FRACHE
committed
Done up to Day Five part two
1 parent 464b15c commit acf1229

File tree

11 files changed

+1884
-6
lines changed

11 files changed

+1884
-6
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using FluentAssertions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace AdventOfCode2025.Tests
7+
{
8+
internal class DayThreeTests
9+
{
10+
[TestCase("987654321111111", 98)]
11+
[TestCase("198765432111111", 98)]
12+
[TestCase("108365432111117", 87)]
13+
[TestCase("102365432111187", 87)]
14+
[TestCase("811111111111119", 89)]
15+
[TestCase("234234234234278", 78)]
16+
[TestCase("818181911112111", 92)]
17+
public void BatteryBankJoltageChecker_Should_Find_Highest_Joltage(string batteryBank, int highestJoltage)
18+
{
19+
BatteryBankJoltageChecker checker = new BatteryBankJoltageChecker();
20+
21+
checker.FindHighestJoltage(batteryBank).Should().Be(highestJoltage);
22+
}
23+
24+
[TestCase("987654321111111", 987654321111)]
25+
[TestCase("811111111111119", 811111111119)]
26+
[TestCase("234234234234278", 434234234278)]
27+
[TestCase("818181911112111", 888911112111)]
28+
public void BatteryBankHeavyJoltageChecker_Should_Find_Highest_Joltage(string batteryBank, long highestJoltage)
29+
{
30+
BatteryBankHeavyJoltageChecker checker = new BatteryBankHeavyJoltageChecker();
31+
32+
checker.FindHighestJoltage(batteryBank).Should().Be(highestJoltage);
33+
}
34+
}
35+
}

AdventOfCode2025.Tests/DayTwoTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,32 @@ public void IDCrawler_Should_Return_All_Invalid_IDs_In_Range()
9393

9494
result.Should().BeEquivalentTo(new List<int> { 11, 22 });
9595
}
96+
97+
[TestCase("12")]
98+
[TestCase("23")]
99+
[TestCase("34")]
100+
[TestCase("341")]
101+
[TestCase("34534")]
102+
public void SpecialIDValidityChecker_Should_Declare_Valid_ID_As_Valid(string id)
103+
{
104+
SpecialIDValidityChecker checker = new SpecialIDValidityChecker();
105+
106+
checker.Check(id).Should().BeTrue();
107+
}
108+
109+
[TestCase("11")]
110+
[TestCase("3232")]
111+
[TestCase("123123")]
112+
[TestCase("111")]
113+
[TestCase("121212")]
114+
[TestCase("446446")]
115+
[TestCase("824824824")]
116+
[TestCase("2121212121")]
117+
public void SpecialIDValidityChecker_Should_Detect_Invalid_IDs(string id)
118+
{
119+
SpecialIDValidityChecker checker = new SpecialIDValidityChecker();
120+
121+
checker.Check(id).Should().BeFalse();
122+
}
96123
}
97124
}

AdventOfCode2025/AdventOfCode2025.csproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<Folder Include="Inputs\" />
16-
</ItemGroup>
17-
18-
<ItemGroup>
15+
<None Update="Inputs\DayFive.txt">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Inputs\DayFour.txt">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
1921
<None Update="Inputs\DayOne.txt">
2022
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2123
</None>
24+
<None Update="Inputs\DayThree.txt">
25+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26+
</None>
2227
<None Update="Inputs\DayTwo.txt">
2328
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2429
</None>

AdventOfCode2025/DayFive.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using AdventOfCode.Elves.IOHelpers;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace AdventOfCode2025
7+
{
8+
internal class DayFive(IPuzzleInputHelper inputHelper)
9+
{
10+
public void PartOne()
11+
{
12+
List<List<String>> lines = inputHelper.GetInputLinesBatched("DayFive.txt", String.Empty);
13+
List<(long min, long max)> freshIdsRanges = lines[0].Select(l => {
14+
string[] parts = l.Split('-');
15+
return (long.Parse(parts[0]), long.Parse(parts[1]));
16+
}).ToList();
17+
18+
int freshCount = 0;
19+
foreach (var idStr in lines[1])
20+
{
21+
long id = long.Parse(idStr);
22+
if (freshIdsRanges.Any(r => id >= r.min && id <= r.max))
23+
{
24+
freshCount++;
25+
}
26+
}
27+
28+
Console.WriteLine($"Part One: Count of fresh IDs: {freshCount}");
29+
}
30+
31+
public void PartTwo()
32+
{
33+
List<List<String>> lines = inputHelper.GetInputLinesBatched("DayFive.txt", String.Empty);
34+
List<(long min, long max)> freshIdsRanges = lines[0].Select(l => {
35+
string[] parts = l.Split('-');
36+
return (long.Parse(parts[0]), long.Parse(parts[1]));
37+
}).ToList();
38+
39+
List<string> mergedRanges = new List<string>();
40+
freshIdsRanges = freshIdsRanges.OrderBy(r => r.min).ToList();
41+
(long min, long max) currentRange = freshIdsRanges[0];
42+
for (int i = 1; i < freshIdsRanges.Count; i++)
43+
{
44+
var range = freshIdsRanges[i];
45+
if (range.min <= currentRange.max + 1)
46+
{
47+
currentRange.max = Math.Max(currentRange.max, range.max);
48+
}
49+
else
50+
{
51+
mergedRanges.Add($"{currentRange.min}-{currentRange.max}");
52+
currentRange = range;
53+
}
54+
}
55+
mergedRanges.Add($"{currentRange.min}-{currentRange.max}");
56+
freshIdsRanges = mergedRanges.Select(l => {
57+
string[] parts = l.Split('-');
58+
return (long.Parse(parts[0]), long.Parse(parts[1]));
59+
}).ToList();
60+
long possibleFreshCount = freshIdsRanges.Sum(f => f.max - f.min + 1);
61+
62+
Console.WriteLine($"Part Two: Count of possible fresh Ids: {possibleFreshCount}");
63+
}
64+
}
65+
}

AdventOfCode2025/DayFour.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using AdventOfCode.Elves.IOHelpers;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace AdventOfCode2025
7+
{
8+
9+
internal class DayFour(IPuzzleInputHelper inputHelper)
10+
{
11+
public void PartOne()
12+
{
13+
char[][] paperRolls = inputHelper.GetInputMatrix("DayFour.txt");
14+
int accessibleRolls = 0;
15+
16+
for (int y = 0; y < paperRolls.Count(); y++)
17+
{
18+
for (int x = 0; x < paperRolls[y].Count(); x++)
19+
{
20+
if (paperRolls[y][x] == '@')
21+
{
22+
int surroundingRolls = CountSurroundingRolls(paperRolls, x, y);
23+
if (surroundingRolls < 4)
24+
{
25+
accessibleRolls++;
26+
}
27+
}
28+
}
29+
}
30+
31+
Console.WriteLine($"Part One: Accessible rolls {accessibleRolls}");
32+
}
33+
34+
private int IsRollAtPosition(char[][] paperRolls, int x, int y)
35+
{
36+
if (x < 0 || y < 0
37+
|| y >= paperRolls.Count()
38+
|| x >= paperRolls[y].Count())
39+
return 0;
40+
41+
return paperRolls[y][x] == '@' ? 1 : 0;
42+
}
43+
44+
private int CountSurroundingRolls(char[][] paperRolls, int x, int y)
45+
{
46+
int rolls = 0;
47+
rolls += IsRollAtPosition(paperRolls, x - 1, y - 1);
48+
rolls += IsRollAtPosition(paperRolls, x - 1, y);
49+
rolls += IsRollAtPosition(paperRolls, x - 1, y + 1);
50+
51+
rolls += IsRollAtPosition(paperRolls, x, y - 1);
52+
rolls += IsRollAtPosition(paperRolls, x, y + 1);
53+
54+
rolls += IsRollAtPosition(paperRolls, x + 1, y - 1);
55+
rolls += IsRollAtPosition(paperRolls, x + 1, y);
56+
rolls += IsRollAtPosition(paperRolls, x + 1, y + 1);
57+
58+
return rolls;
59+
}
60+
61+
public void PartTwo()
62+
{
63+
char[][] paperRolls = inputHelper.GetInputMatrix("DayFour.txt");
64+
int removedRolls = 0;
65+
List<(int x, int y)> rollsToRemove;
66+
67+
do
68+
{
69+
rollsToRemove = new List<(int x, int y)>();
70+
for (int y = 0; y < paperRolls.Count(); y++)
71+
{
72+
for (int x = 0; x < paperRolls[y].Count(); x++)
73+
{
74+
if (paperRolls[y][x] == '@')
75+
{
76+
int surroundingRolls = CountSurroundingRolls(paperRolls, x, y);
77+
if (surroundingRolls < 4)
78+
{
79+
rollsToRemove.Add((x, y));
80+
}
81+
}
82+
}
83+
}
84+
foreach (var roll in rollsToRemove)
85+
{
86+
paperRolls[roll.y][roll.x] = '.';
87+
removedRolls++;
88+
}
89+
}
90+
while (rollsToRemove.Count != 0);
91+
92+
Console.WriteLine($"Part One: Removed rolls {removedRolls}");
93+
}
94+
}
95+
}

AdventOfCode2025/DayThree.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using AdventOfCode.Elves.IOHelpers;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace AdventOfCode2025
7+
{
8+
internal class BatteryBankJoltageChecker
9+
{
10+
internal int FindHighestJoltage(string batteryBank)
11+
{
12+
int bestBatteryIndex = FindHighestJoltageIndexFromIndex(batteryBank, 0, 1);
13+
char ten = batteryBank[bestBatteryIndex];
14+
bestBatteryIndex = FindHighestJoltageIndexFromIndex(batteryBank, bestBatteryIndex + 1, 0);
15+
char unit = batteryBank[bestBatteryIndex];
16+
17+
return int.Parse($"{ten}{unit}");
18+
}
19+
20+
private static int FindHighestJoltageIndexFromIndex(string batteryBank, int start, int endBufferSize)
21+
{
22+
int bestBatteryIndex = start;
23+
for (int i = start; i < batteryBank.Length - endBufferSize; i++)
24+
{
25+
if (batteryBank[i] > batteryBank[bestBatteryIndex])
26+
{
27+
bestBatteryIndex = i;
28+
}
29+
}
30+
31+
return bestBatteryIndex;
32+
}
33+
}
34+
35+
internal class BatteryBankHeavyJoltageChecker
36+
{
37+
internal long FindHighestJoltage(string batteryBank)
38+
{
39+
char[] highestJoltageString = new char[12];
40+
int bestBatteryIndex = 0;
41+
int endBufferSize = 11;
42+
for (int i = 0; i < 12; i++)
43+
{
44+
bestBatteryIndex = FindHighestJoltageIndexFromIndex(batteryBank, bestBatteryIndex, endBufferSize);
45+
highestJoltageString[i] = batteryBank[bestBatteryIndex];
46+
bestBatteryIndex++;
47+
endBufferSize--;
48+
}
49+
50+
return long.Parse(String.Join("", highestJoltageString));
51+
}
52+
53+
private static int FindHighestJoltageIndexFromIndex(string batteryBank, int start, int endBufferSize)
54+
{
55+
int bestBatteryIndex = start;
56+
for (int i = start; i < batteryBank.Length - endBufferSize; i++)
57+
{
58+
if (batteryBank[i] > batteryBank[bestBatteryIndex])
59+
{
60+
bestBatteryIndex = i;
61+
}
62+
}
63+
64+
return bestBatteryIndex;
65+
}
66+
}
67+
68+
internal class DayThree(IPuzzleInputHelper inputHelper)
69+
{
70+
public void PartOne()
71+
{
72+
List<String> lines = inputHelper.GetInputLines("DayThree.txt");
73+
long sum = 0;
74+
BatteryBankJoltageChecker checker = new BatteryBankJoltageChecker();
75+
76+
foreach (string line in lines)
77+
{
78+
int bestJoltage = checker.FindHighestJoltage(line);
79+
sum += bestJoltage;
80+
}
81+
82+
Console.WriteLine($"Part One: Sum of best joltages: {sum}");
83+
}
84+
85+
public void PartTwo()
86+
{
87+
List<String> lines = inputHelper.GetInputLines("DayThree.txt");
88+
long sum = 0;
89+
BatteryBankHeavyJoltageChecker checker = new BatteryBankHeavyJoltageChecker();
90+
91+
foreach (string line in lines)
92+
{
93+
long bestJoltage = checker.FindHighestJoltage(line);
94+
sum += bestJoltage;
95+
}
96+
97+
Console.WriteLine($"Part Two: Sum of best joltages: {sum}");
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)