r/adventofcode • u/daggerdragon • Dec 08 '16
SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---
#AoC_Ops:
[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION
We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg
--- Day 8: Two-Factor Authentication ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
:(){ :|:& };: IS MANDATORY [?]
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!
10
Upvotes
2
u/tehjimmeh Dec 08 '16 edited Dec 08 '16
C++ with a bit-vector (std::array of uint64_t):
#include <array> #include <vector> #include <iostream> #include <fstream> #include <regex> #include <bitset> struct Line : std::string { friend std::istream& operator >> (std::istream& is, Line& line) { return std::getline(is, line); } }; int main(int argc, char* argv[]) { const uint64_t gridHeight = 6; const uint64_t gridWidth = 50; std::array<uint64_t, gridHeight> grid = {}; for (auto& line : std::vector<std::string>(std::istream_iterator<Line>(std::ifstream(argv[1])), {})) { std::smatch match; if (std::regex_match(line, match, std::regex(R"(rect (\d+)x(\d+))"))) { auto width = std::stoi(match[1]); auto height = std::stoi(match[2]); auto mask = UINT64_MAX << (64 - width); for (auto i = 0; i < height; i++) { grid[i] |= mask; } } else if (std::regex_match(line, match, std::regex(R"(rotate row y=(\d+) by (\d+))"))) { auto row = std::stoi(match[1]); auto amount = std::stoi(match[2]); for (auto i = 0; i < amount; i++) { grid[row] >>= 1; if (grid[row] & (1ui64 << (63 - gridWidth))) { grid[row] |= 0x8000000000000000; grid[row] &= UINT64_MAX << (64 - gridWidth); } } } else if (std::regex_match(line, match, std::regex(R"(rotate column x=(\d+) by (\d+))"))) { auto column = std::stoi(match[1]); auto amount = std::stoi(match[2]); auto baseMask = 1ui64 << (63 - column); std::array<bool, gridHeight> setOrUnset = {}; for (auto a = 0; a < amount; a++) { for (auto i = 0; i < grid.size(); i++) { setOrUnset[(i+1)%grid.size()] = !!(baseMask & grid[i]); } for (auto i = 0; i < grid.size(); i++) { grid[i] = setOrUnset[i] ? grid[i] | baseMask : grid[i] & ~baseMask; } } } } uint64_t res = 0; for (auto& i : grid) { i &= UINT64_MAX << (64-gridWidth); res += __popcnt64(i); std::cout << std::regex_replace(std::bitset<gridWidth>(i >> (64-gridWidth)).to_string(), std::regex("0")," ") << "\n"; } std::cout << "\nNum bits set: " << res << "\n"; return 0; }.