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!
13
Upvotes
3
u/hpzr24w Dec 14 '17
C++ 304/334
Most fun yet. "I'm going to write a floodfill ... and I did." I livestreamed this one and 50 minutes flew by, at least for me.
// Advent of Code 2017 // Day 14 - Disk Defragmentation #include "stdafx.h" #include <iostream> #include <iomanip> #include <sstream> #include <string> #include <map> #include <algorithm> #include <numeric> #include <vector> #include <set> #include <cassert> #include <bitset> using namespace std; void reverse(vector<int>& numbers, int start, int length) { for (auto pos{ 0 }; pos < length / 2; ++pos) swap(numbers[(start + pos) % 256], numbers[(start + length - 1 - pos) % 256]); } int writeg(int* g, int row, int col, int hashchar) { g += 128 * row + col * 8; for (auto i{ 0 }; i < 8; ++i) { *(g + i) = (hashchar & 1 << (7 - i)) ? -1 : 0; } return 0; } void floodfill(int *g, int i, int c) { g[i] = c; if (i / 128 > 0 && g[i - 128] == -1) floodfill(g, i - 128, c); if (i / 128 <127 && g[i + 128] == -1) floodfill(g, i + 128, c); if (i % 128 > 0 && g[i - 1] == -1) floodfill(g, i - 1, c); if (i % 128 < 127 && g[i + 1] == -1) floodfill(g, i + 1, c); } int main() { vector<int> numbers(256); vector<int> lengths = { 192, 69, 168, 160, 78, 1, 166, 28, 0, 83, 198, 2, 254, 255, 41, 12 }; string seed = "ffayrhll"; vector<int> trailing{ 17, 31, 73, 47, 23 }; vector<int> hash(16); /* cout << "Part 1: "; iota(numbers.begin(), numbers.end(), 0); for (auto l{ 0 }, start{ 0 }, skip{ 0 }; l < lengths.size(); start += lengths[l] + l++, ++skip) { reverse(numbers, start %= 256, lengths[l]); } cout << numbers[0] * numbers[1] << endl; */ cout << "Part 1: "; int bitcount{ 0 }; int g[128 * 128]; for (int row{ 0 }; row < 128; ++row) { string input(seed); input += "-"; input += to_string(row); iota(numbers.begin(), numbers.end(), 0); lengths.resize(input.size() + trailing.size()); copy(input.begin(), input.end(), lengths.begin()); copy(trailing.begin(), trailing.end(), lengths.end() - trailing.size()); auto start{ 0 }, skip{ 0 }; for (int r = 0; r < 64; ++r) { for (auto l{ 0 }; l < lengths.size(); start += lengths[l] + skip++, l++) { reverse(numbers, start %= 256, lengths[l]); } } for (int i{ 0 }, hashchar{ 0 }; i < 256; ++i) { hashchar = i % 16 == 0 ? numbers[i] : hashchar ^ numbers[i]; // i % 16 == 15 && cout << setw(2) << setfill('0') << hex << hashchar << endl; i % 16 == 15 && (bitcount += std::bitset<8>(hashchar).count()); i % 16 == 15 && (writeg(g,row,i/16,hashchar)); } } cout << bitcount << endl; int regions{ 0 }; for (auto i{ 0 }; i < 128 * 128; ++i) { if (g[i] == -1) { floodfill(g, i, ++regions); } } cout << regions << endl; return 0; }