From 7c2d8dea6957c6c8cbaa70b4d8c7eb412dee22df Mon Sep 17 00:00:00 2001 From: Dimitrios Kaltzidis Date: Tue, 10 Dec 2024 14:30:51 +0200 Subject: [PATCH] Add day 10 --- AdventOfCode2024.sln | 6 ++ Day10/Day10.csproj | 16 +++++ Day10/Program.cs | 142 +++++++++++++++++++++++++++++++++++++++++++ Day10/input.txt | 48 +++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 Day10/Day10.csproj create mode 100644 Day10/Program.cs create mode 100644 Day10/input.txt diff --git a/AdventOfCode2024.sln b/AdventOfCode2024.sln index fabc066..a1e878e 100644 --- a/AdventOfCode2024.sln +++ b/AdventOfCode2024.sln @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day8", "Day8\Day8.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day9", "Day9\Day9.csproj", "{E343E757-5AAC-4CDC-9B78-A5FB2466DAE2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day10", "Day10\Day10.csproj", "{9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {E343E757-5AAC-4CDC-9B78-A5FB2466DAE2}.Debug|Any CPU.Build.0 = Debug|Any CPU {E343E757-5AAC-4CDC-9B78-A5FB2466DAE2}.Release|Any CPU.ActiveCfg = Release|Any CPU {E343E757-5AAC-4CDC-9B78-A5FB2466DAE2}.Release|Any CPU.Build.0 = Release|Any CPU + {9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DA97BBA-99AF-4FA7-97BB-C6EF3F8B24A4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Day10/Day10.csproj b/Day10/Day10.csproj new file mode 100644 index 0000000..e0c4c2e --- /dev/null +++ b/Day10/Day10.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + enable + enable + + + + + Always + + + + diff --git a/Day10/Program.cs b/Day10/Program.cs new file mode 100644 index 0000000..bc6d500 --- /dev/null +++ b/Day10/Program.cs @@ -0,0 +1,142 @@ +var rawTopographicMap = File.ReadLines("input.txt"); +var topographicMap = CreateTopographicMap(rawTopographicMap); + +var availableDirections = new List<(int, int)>() +{ + (-1, 0), // Up + (1, 0), // Down + (0, -1), // Left + (0, 1) // Right +}; + +Console.WriteLine($"Part1: {Part1(topographicMap, availableDirections)}\nPart2: {Part2(topographicMap, availableDirections)}"); + + +int Part2(int[,] mapInput, List<(int, int)> directions) +{ + var totalRating = 0; + for (var r = 0; r < mapInput.GetLength(0); r++) + { + for (var c = 0; c < mapInput.GetLength(1); c++) + { + if (mapInput[r, c] == 0) + { + totalRating += FindDistinctTrails(r, c, mapInput, directions); + } + } + } + + return totalRating; +} + +int FindDistinctTrails(int r, int c, int[,] mapInput, List<(int, int)> directions) +{ + var stack = new Stack>(); + stack.Push(new List<(int, int)> { (r, c) }); + var trails = 0; + + while (stack.Count > 0) + { + var currentPath = stack.Pop(); + var (cr, cc) = currentPath[^1]; + + if (mapInput[cr, cc] == 9) + { + trails++; + continue; + } + + foreach (var (dr, dc) in directions) + { + var nr = cr + dr; + var nc = cc + dc; + + if (nr >= 0 && nr < mapInput.GetLength(0) && nc >= 0 && nc < mapInput.GetLength(1) && + mapInput[nr, nc] == mapInput[cr, cc] + 1) + { + var nextPath = new List<(int, int)>(currentPath); + nextPath.Add((nr, nc)); + stack.Push(nextPath); + } + } + } + + return trails; +} + +int Part1(int[,] map, List<(int, int)> directions) +{ + var totalScore = 0; + + for (var row = 0; row < map.GetLength(0); row++) + { + for (var column = 0; column < map.GetLength(1); column++) + { + if (map[row, column] == 0) + { + totalScore += TrailHead(row, column, map, directions); + } + } + } + + return totalScore; +} + +int TrailHead(int startRow, int startColumn, int[,] map, List<(int, int)> directions) +{ + var rows = map.GetLength(0); + var cols = map.GetLength(1); + var queue = new Queue<(int, int)>(); + var visited = new HashSet<(int, int)>(); + var reachableNines = new HashSet<(int, int)>(); + + queue.Enqueue((startRow, startColumn)); + + while (queue.Count > 0) + { + var (r, c) = queue.Dequeue(); + + if (visited.Contains((r, c))) + continue; + + visited.Add((r, c)); + + if (map[r, c] == 9) + { + reachableNines.Add((r, c)); + continue; + } + + foreach (var dir in directions) + { + var nr = r + dir.Item1; + var nc = c + dir.Item2; + + if (nr >= 0 && nr < rows && nc >= 0 && nc < cols) + { + if (map[nr, nc] == map[r, c] + 1) + { + queue.Enqueue((nr, nc)); + } + } + } + } + + return reachableNines.Count; +} + +int[,] CreateTopographicMap(IEnumerable lines) +{ + var lineList = lines.Select(line => line.Trim()).ToList(); + var map = new int[lineList.Count, lineList[0].Length]; + + for (var r = 0; r < map.GetLength(0); r++) + { + for (var c = 0; c < map.GetLength(1); c++) + { + map[r, c] = lineList[r][c] - '0'; + } + } + + return map; +} \ No newline at end of file diff --git a/Day10/input.txt b/Day10/input.txt new file mode 100644 index 0000000..84df185 --- /dev/null +++ b/Day10/input.txt @@ -0,0 +1,48 @@ +101987698323656762989721011010987456780121243210 +234012567210547891078892012101072345891030984569 +142103454345432012367743423232161654322345855678 +051054965236701103456656894343450783210496764789 +962167850149896543245765765456980892108587053298 +873076701056787600130876554967821876549652142103 +954987432165634512021989478876782965678703234012 +765876541074328763145672369805493454302212547654 +896968953981019854236981054012349854211243478943 +657857762876567610765100340101238763210798545692 +765446891001498723834232215293045678189877632781 +802335432312387654923341004382167669012368701020 +911124701421019870110056905673458978975459345210 +320099876598943065232167810569898963287561276349 +456787756787854104343054321678107854196510389458 +329878941012763210454988934521256981075431343367 +019561030323678923567867141010345410987123454321 +878432321410589854543252012367894322346001067800 +965678798523470189630141043454387011055432108912 +234309657631063238745632658965296545765443297983 +103210545432154127030745667870101239812344589854 +123451236943061032121836789654320149801256678765 +018760147858978945412921078761012456789107967810 +569676546567167656503832099908901327898798856921 +678989432100016987434512187819832010145610765430 +567010498761221498729603456520743016234431254321 +154321343254330327018777895431654327652342341232 +034501656189945012310689765432343678541056540765 +127652783076876763423676543087652589230767439856 +098743692105677894534589832198101450121858921045 +769878901234786765109689101265012376543929012234 +878963210121099889278770125654321289012210430123 +987654323452016776308765210787430100101121522434 +670122014560125655419654354896508921089037611078 +549831009871234534328323763011017632176548902369 +038742789965676548933214892182126543210423098454 +125653656234987837654300321099234304321012167823 +034564540165832921020311456788765015698143456910 +321078939874981021011232321001298923789874301501 +012981543403876120102345409876387654301565217652 +565470034512985433201456912345401543214321438943 +678321127621076894398567810126918754265410521032 +099128938902345765487656787637829662178345621001 +187019878213765496567678698540134543089210782789 +236520569347874385438569187687238956745432893610 +945431450956901276329430056796549875856721894523 +876532321876101301210321149823456721949890765434 +765321012765432340343434231012545430132101256545 \ No newline at end of file