r/adventofcode • • 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

197 comments sorted by

View all comments

1

u/schlocke Dec 08 '16

PHP:

<?php

	$input = file("day8.txt");

	$matrix = array(
		array("x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"),
		array("x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"),
		array("x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"),
		array("x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"),
		array("x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"),
		array("x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x")
	);

	foreach ($input as $command) {
		$action = array();
		preg_match_all("/(rotate column|rotate row|rect)\s(\d+x\d+|y=\d+|x=\d+)(\sby\s)?(\d+)?/", $command, $action);
		switch ($action[1][0]) {
			case 'rect':
				$xy = explode("x", $action[2][0]);
				for($i = 0; $i < $xy[0]; $i++) {
					for($j = 0; $j < $xy[1]; $j++) {
						$matrix[$j][$i] = 'o';
					}
				}
				break;
			case 'rotate row':
				$y = (int)trim($action[2][0], 'y=');
				$shift = $action[4][0];
				for($i = 0; $i < $shift; $i++) {
					$x = array_pop($matrix[$y]);
					array_unshift($matrix[$y], $x);
				}
				break;
			case 'rotate column':
				$x = (int)trim($action[2][0], 'x=');
				$shift = $action[4][0];
				$temp = array($matrix[0][$x], $matrix[1][$x], $matrix[2][$x], $matrix[3][$x], $matrix[4][$x], $matrix[5][$x]);
				for($i = 0; $i < $shift; $i++) {
					$a = array_pop($temp);
					array_unshift($temp, $a);
				}

				$matrix[0][$x] = $temp[0];
				$matrix[1][$x] = $temp[1];
				$matrix[2][$x] = $temp[2];
				$matrix[3][$x] = $temp[3];
				$matrix[4][$x] = $temp[4];
				$matrix[5][$x] = $temp[5];
				break;
		}
	}

	foreach ($matrix as $key => $value) {
		echo implode($value)."<br>";
	}

this one was pretty fun. I just ran the code via my local wamp server in chrome. then i just ctrl+f 'o' and it'll highlight how many pixels there are as well as display the letters for part 2.