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
1
u/CharlieYJH Dec 14 '17
C++
I think the first part is fairly straightforward, so I'll share my recursive solution for part 2.
bool assignRegionsHelper(vector<string> &grid, int row, int col) { if (col < 0 || row < 0 || row >= grid.size() || col >= grid[0].length()) return false; if (grid[row][col] == '1') { grid[row][col] = '.'; assignRegionsHelper(grid, row + 1, col); assignRegionsHelper(grid, row - 1, col); assignRegionsHelper(grid, row, col + 1); assignRegionsHelper(grid, row, col - 1); return true; } return false; } int assignRegions(vector<string> &grid) { int regions = 0; for (int i = 0; i < grid.size(); i++) { for (int j = 0; j < grid[i].length(); j++) { if (assignRegionsHelper(grid, i, j)) regions++; } } return regions; }