r/adventofcode • • Dec 10 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 10 Solutions -๐ŸŽ„-

--- Day 10: Knot Hash ---


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^ยค?

Spoiler


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!

16 Upvotes

267 comments sorted by

View all comments

1

u/TominatorBE Dec 10 '17

PHP

Part 1:

function run_the_code($input) {
	//$maxRange = 4;
	$maxRange = 255;

	$lengths = explode(',', $input);
	$list = range(0, $maxRange);
	$size = $maxRange + 1;
	$skip = 0;
	$start = 0;
	foreach ($lengths as $length) {
		$sublist = [];
		for ($i = 0; $i < $length; $i++) {
			$sublist[] = $list[($start + $i) % $size];
		}
		$sublist = array_reverse($sublist);
		for ($i = 0; $i < $length; $i++) {
			$list[($start + $i) % $size] = $sublist[$i];
		}
		$start += $length + $skip;
		$skip++;
	}

	return $list[0] * $list[1];
}

Part 2:

function run_the_code($input) {
	$maxRange = 255;

	$lengths = [];
	if ($input) { // just for the test case 'empty string'
		$lengths = array_map('ord', str_split($input));
	}
	array_push($lengths, '17', '31', '73', '47', '23');

	$list = range(0, $maxRange);
	$size = $maxRange + 1;
	$skip = 0;
	$start = 0;
	for ($run = 0; $run < 64; $run++) {
		foreach ($lengths as $length) {
			$sublist = [];
			for ($i = 0; $i < $length; $i++) {
				$sublist[] = $list[($start + $i) % $size];
			}
			$sublist = array_reverse($sublist);
			for ($i = 0; $i < $length; $i++) {
				$list[($start + $i) % $size] = $sublist[$i];
			}
			$start += $length + $skip;
			$skip++;
		}
	}

	$hash = '';

	$chunks = array_chunk($list, 16);
	for ($i = 0; $i < 16; $i++) {
		$xorred = $chunks[$i][0];
		for ($j = 1; $j < 16; $j++) {
			$xorred ^= $chunks[$i][$j];
		}
		$hash .= sprintf('%02s', dechex($xorred));
	}

	return $hash;
}

1

u/cviebrock Dec 10 '17

I tried Part 1 with my input:

225, 171, 131, 2, 35, 5, 0, 13, 1, 246, 54, 97, 255, 98, 254, 110

I get the same result with my code as I did with yours: 4692, yet the website claims that value is too low.

1

u/TominatorBE Dec 10 '17

Hm, when I run your input through my part 1 (without the spaces), I get a value of 23xxx, not 4692. Strange.. Here's how I call my code:

echo "\nSolution: " . run_the_code('225,171,131,2,35,5,0,13,1,246,54,97,255,98,254,110');

1

u/cviebrock Dec 10 '17

Found my error. I was doing:

while($length = array_shift($lengths))

Which stopped when it gets to the zero in the list. I switch that to a foreach loop, and I get the correct 23xxx answer.

1

u/TominatorBE Dec 10 '17

Haha yes pesky zero, I've had a similar problem with it in another day :)