Skip to content

Commit

Permalink
day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 10, 2024
1 parent 81578d1 commit 90a6b8a
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 16 deletions.
116 changes: 100 additions & 16 deletions src/aoc/Year2024/Day10.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,104 @@
using aoc.Common;
using MoreLinq;

namespace aoc.Year2024;

internal partial class Day10
internal partial class Day10 // todo: common bfs?
{
internal partial class Part1
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}

internal partial class Part2
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
internal partial class Part1
{
private readonly Example example = new Example(
"""
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
""", 36);

public int Solve(int[,] input)
{
var trailHeads = input.AsEnumerable().Where(x => x.element == 0).ToArray();

return trailHeads.Sum(head =>
{
var list = new HashSet<V<int>>();
FillVisited(input, head.point, list);
return list.Count;
});
}

private static void FillVisited(int[,] map, V<int> curPos, HashSet<V<int>> list)
{
if (map.At(curPos) == 9)
list.Add(curPos);
var adj = new[]
{
Directions.Down(),
Directions.Up(),
Directions.Right(),
Directions.Left(),
}.Select(d => d + curPos)
.Where(map.Inside);

adj
.Where(p => map.At(p) == map.At(curPos) + 1)
.ForEach(p => FillVisited(map, p, list));
}

public int[,] Parse(string input)
{
return Character.Digit.Select(c => int.Parse(c.ToString()))
.Map().Parse(input);
}
}

internal partial class Part2
{
private readonly Example example = new(
"""
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
""", 81);

public int Solve(int[,] input)
{
var trailHeads = input.AsEnumerable().Where(x => x.element == 0).ToArray();

return trailHeads.Sum(head => Score(input, head.point));
}

private static int Score(int[,] map, V<int> curPos)
{
if (map.At(curPos) == 9)
return 1;
var adj = new[]
{
Directions.Down(),
Directions.Up(),
Directions.Right(),
Directions.Left(),
}.Select(d => d + curPos)
.Where(map.Inside);

return adj
.Where(p => map.At(p) == map.At(curPos) + 1)
.Sum(p => Score(map, p));
}

public int[,] Parse(string input)
{
return Character.Digit.Select(c => int.Parse(c.ToString()))
.Map().Parse(input);
}
}
}
2 changes: 2 additions & 0 deletions tests/AoC.Tests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ private static IEnumerable<PartInputCaseData> GetCases()
yield return new PartInputCaseData(8, 2, "944");
yield return new PartInputCaseData(9, 1, "6279058075753");
yield return new PartInputCaseData(9, 2, "6301361958738");
yield return new PartInputCaseData(10, 1, "517");
yield return new PartInputCaseData(10, 2, "1116");
}
}
45 changes: 45 additions & 0 deletions tests/AoC.Tests/inputs/input10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
123454078104569871014321021321012349878967874
002965169245678562105489110456701256767456983
411873254324329453456976522343812323454305432
320984589010012306567887431098943210563216761
458976673407873210698790346787654329874109850
367887654356912348787891245497645698556780943
656792343105401059678712312338562107649891234
343001643234332769589600404329431236032321104
102198756121049878465521565010120345121430213
234567987012156912374437674320345034560345412
126898776103457809983398983011276127672126703
045678945434967618812287034567689018984035894
034989876325898507600176127698548109123445665
121070165016785216010165018345630678006510778
015676234245234345323234509216721565217329889
104389892104101059654965432101892674398456788
010210743233298128760870123438984583212105998
327805654654567637201256754347673294303614854
476912348723498542112349861243100185454783763
585210239010101443009659870652231256765692152
694332108212012356788778778701645899801087001
783445098302345653299894389614556732112396503
612956789401298764108763210543678945013765412
107810987567889655895654101012109876894894321
016721076101910346781565092325012345765765010
145432345234581232690478783234540332345654321
298703876501698701541089654129651231056310145
387212965012787650132108543098774340987234236
456903454323456043236719612012589656891105687
125876561210798101045898703343478797650112793
034329870325887232784387643296556788943289832
105610711456976545693211234187645019850176541
612789804567803456789800105098732134567654320
523898714326512101234764306789678325478956010
630145625613456780345605219812569210329547854
549234034702102395653216984503431678017632903
678432129899801234764567893689120589098701012
569562101234710109803891012778021432176512343
450691032105676541012764569865434501089400154
541782145694589132123453678345123671078321965
432679238785410016787652101254010982361267873
121018019856321125696545610763010973450398654
038967025657689434545236789892129887650367743
127652134768976521430129865430034796341459812
234543089867987010321010178921015601232378103

0 comments on commit 90a6b8a

Please sign in to comment.