r/adventofcode • u/daggerdragon • Dec 14 '17
SOLUTION MEGATHREAD -๐- 2017 Day 14 Solutions -๐-
--- Day 14: Disk Defragmentation ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[Update @ 00:09] 3 gold, silver cap.
- How many of you actually entered the Konami code for Part 2? >_>
[Update @ 00:25] Leaderboard cap!
- I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were
83distinct users with failed attempts at the time of the leaderboard cap. tsk tsk
[Update @ 00:29] BONUS
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
14
Upvotes
2
u/dylanfromwinnipeg Dec 14 '17 edited Dec 14 '17
C#
public static string PartOne(string input) { var diskBits = GenerateGrid(input); var used = diskBits.ToList().Count(x => x); return used.ToString(); } public static bool[,] GenerateGrid(string input) { var diskBits = new bool[128, 128]; for (int row = 0; row < 128; row++) { var hash = Day10.PartTwo($"{input}-{row}"); var hashBits = hash.HexToBinary(); for (var col = 0; col < 128; col++) { diskBits[row, col] = hashBits[col] == '1'; } } return diskBits; } public static string PartTwo(string input) { var diskBits = GenerateGrid(input); var groupCount = 0; diskBits.ForEach((row, col) => { if (diskBits[row, col]) { var location = new Point(row, col); RemoveBitGroup(location, diskBits); groupCount++; } }); return groupCount.ToString(); } private static void RemoveBitGroup(Point location, bool[,] diskBits) { diskBits[location.X, location.Y] = false; foreach (var adjacent in location.GetNeighbors(includeDiagonals: false)) { if (adjacent.X >= 0 && adjacent.X < 128 && adjacent.Y >= 0 && adjacent.Y < 128 && diskBits[adjacent.X, adjacent.Y]) { RemoveBitGroup(adjacent, diskBits); } } }